|
楼主 |
发表于 2020-5-3 20:34:21
|
显示全部楼层
- #include "iostream"
- using std::endl;
- using std::cin;
- using std::cout;
- using std::string;
- class Student
- {
- private:
- int num;
- string name;
- char sex;
- public:
- void getValue()
- {
- cout << "请输入3个数据\n";
- cin >> num >> name >> sex;
- }
- public:
- void show()
- {
- cout << "Student num:" << this->num << endl;
- cout << "Student name:" << this->name << endl;
- cout << "Student sex:" << this->sex << endl;
- }
- };
- class Student1 :public Student
- {
- private:
- int age;
- string addr;
- public:
- void getValue1()
- {
- cout << "请输入2个数据\n";
- cin >> age >> addr;
- }
- public:
- void show1()
- {
- cout << "Student1 age:" << age << endl;
- cout << "Student1 addr:" << addr << endl;
- }
- };
- void main()
- {
- Student1 st;
- st.getValue();//调用基类公有成员函数,输入基类中3个数据的值
- st.getValue1();//调用派生类公有成员函数,输入派生类2个数据的值
- st.show();//调用基类公有成员函数,输出基类中3个数据的值
- st.show1();//调用派生类公有成员函数,输出派生类中2个数据的值
- system("pause");
- }
复制代码 |
|