习题 11. 4 改例11.3的程序,改为公用继承方式。上机调试程序,使之能正确运行并得到正确结果。对这两种继承方式做分析比较,考虑在什么情况下二者不能互相替换。
在类外访问时,protected继承的public和protected成员不可被访问,而public继承的可以
#include <iostream>
using namespace std;
class Student
{
public:
protected:
int num;
string name;
char sex;
};
class Student1 : public Student
{
public:
void get_value_1();
void display1();
private:
int age;
string addr;
};
void Student1::get_value_1()
{
cout << "请输入学号、姓名、性别、年龄、地址:" << endl;
cin >> num >> name >> sex;
cin >> age >> addr;
}
void Student1::display1()
{
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
cout << "age: " << age << endl;
cout << "address: " << addr << endl;
}
int main()
{
Student1 stud1;
stud1.get_value_1();
stud1.display1();
return 0;
}