📄 p383 例11.9 虚基类简单应用.cpp
字号:
#include<iostream>
#include<string>
using namespace std;
class Person
{
protected:
string name;
int age;
char sex;
public:
Person(string n,int a,char s):name(n),age(a),sex(s){}
void display();
};
void Person::display()
{
cout<<"the name: "<<name<<endl;
cout<<"the age: "<<age<<endl;
cout<<"the sex: "<<sex<<endl;
}
class Teacher:virtual public Person
{
protected:
string title;
public:
Teacher(string n,int a,char s,string t):Person(n,a,s),title(t) {}
void display();
};
void Teacher::display()
{
Person::display();
cout<<"the position: "<<title<<endl;
}
class Student:virtual public Person
{
protected:
float score;
public:
Student(string n,int a,char s,float sc):Person(n,a,s),score(sc){}
void display();
};
void Student::display()
{
Person::display();
cout<<"the score: "<<score<<endl;
}
class Graduate:public Teacher,public Student
{
private:
float wage;
public:
Graduate(string n,int a,char s,string t,float sc,float w)
:Person(n,a,s),Student(n,a,s,sc),Teacher(n,a,s,t),wage(w){}
void display();
};
void Graduate::display()
{
Teacher::display();
cout<<"the score: "<<score<<endl;
cout<<"the wage: "<<wage<<endl;
}
int main()
{
Graduate gra("wang li",34,'f',"assistant",89.5,1234);
Student st("wang li",34,'f',78.9);
Teacher te("wang li",34,'f',"professor");
cout<<endl<<"the graduate information: "<<endl;
gra.display();
cout<<endl<<"the student information: "<<endl;
st.display();
cout<<endl<<"the teacher information: "<<endl;
te.display();
system("pause");
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -