定义一个基类teacher,它有公有成员函数showinfo(),函数打印数据成员信息,有私有数据成员int num,string name,char sex。派生类professor是teacher
时间: 2023-04-23 09:03:18 浏览: 127
C抽象基类和纯虚成员函数-C教程共3页.pdf.zip
的子类,它有私有数据成员string major和公有成员函数teach(),函数打印教授专业信息。在派生类中重载showinfo()函数,打印教授的全部信息。
class teacher {
private:
int num;
string name;
char sex;
public:
void showinfo() {
cout << "num: " << num << endl;
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
}
};
class professor : public teacher {
private:
string major;
public:
void teach() {
cout << "teaching " << major << endl;
}
void showinfo() {
teacher::showinfo();
cout << "major: " << major << endl;
}
};
阅读全文