将程序修改、补充,写成一个完整、正确的程序,用保护继承方式。在程序中应包括输入数据的函数。
时间: 2024-02-09 10:12:10 浏览: 20
好的,下面是修改、补充的程序:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
void display() const {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
}
protected:
int num;
string name;
char sex;
};
class Student1 : protected Student {
public:
void get_value() {
cout << "Please enter num, name, sex, age, address: ";
cin >> num >> name >> sex >> age >> addr;
}
void display_1() const {
display();
cout << "age: " << age << endl;
cout << "address: " << addr << endl;
}
protected:
int age;
string addr;
};
int main() {
Student1 stud;
stud.get_value();
stud.display_1();
return 0;
}
```
修改的地方主要是将`display()`方法和成员变量改为了`const`,表示它们不会修改对象的状态。同时,将`Student1`类的访问控制符改为了`protected`,以实现保护继承。另外,加入了缺少的`protected`关键字。