?? p383 例11.9 虛基類簡單應(yīng)用.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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -