#include <iostream>
using namespace std;
class Student{
public:
void get_value()
{cin>>num>>name>>sex;}
void display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
private:
int num;
char name[10];
char sex;
};
class Student1:private Student{
public:
void getvalue()
{
get_value();
cin>>age>>addr;
}
void show()
{
display();
cout<<"age:"<<age<<endl;
cout<<"address:"<<addr<<endl;
}
private:
int age;
char addr[10];
};
int main()
{
Student1 stud;
stud.getvalue();
stud.show();
return 0;
}
5-2
最新推荐文章于 2018-10-13 08:06:41 发布
本文通过一个具体的C++程序示例介绍了类的继承和封装的基本概念。示例中定义了两个类:基本类Student用于存储学生的编号、姓名和性别;派生类Student1从Student类私有继承,并增加了年龄和地址属性。主函数中演示了如何使用这些类来获取和显示学生信息。
4513

被折叠的 条评论
为什么被折叠?



