虚基类的简单应用

#include<iostream>
#include<string>
using namespace std;
class Person
{
public:
	Person(string nam,char s,int a):name(nam),sex(s),age(a){}
protected:
	string name;
	char sex;
	int age;
};
class Teacher:virtual public Person
{
public:
	Teacher(string nam,char s,int a,string t):Person(nam,s,a),title(t) {}
protected:
	string title;
};
class Student:virtual public Person
{
public:
	Student(string nam,char s,int a,double sco):Person(nam,s,a),score(sco) {}
protected:
	double score;
};
class Graduate:public Teacher,public Student
{
public:
	Graduate(string nam,char s,int a,string t,double sco,int w):Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco),wage(w) {}
	void show()
	{
		cout<<"name:"<<name<<endl;
		cout<<"sex:"<<sex<<endl;
		cout<<"age:"<<age<<endl;
		cout<<"tltle:"<<title<<endl;
		cout<<"score: "<<score<<endl;
		cout<<"wage: "<<wage<<endl;
		cout<<endl;	
	}
protected:
	int wage;
};
int main()
{
	Graduate grad("wang",'f',18,"assitant",100,4000);
	grad.show();
	return 0;
}


 

虚基类的简单应用_第1张图片

你可能感兴趣的:(虚基类的简单应用)