虚基类的简单应用

#include 
#include 
using namespace std;

//声明公共基类Person
class Person
{
public:
	Person(string nam,char s,int a)
	{
		name=nam;
		sex=s;
		age=a;
	}
protected:
	string name;
	char sex;
	int age;
};

//声明Person的直接派生类Teacher
class Teacher:virtual public Person//声明Person为公用继承的虚基类
{
public:
	Teacher(string nam,char s,int a,string t):Person(nam,s,a)
	{
		title=t;
	}
protected:
	string title;
};

//声明Person的直接派生类Student
class Student:virtual public Person//声明Person为公用继承的虚基类
{
public:
	Student(string nam,char s,int a,float sco):Person(nam,s,a),score(sco)//参数初始化表对数据成员初始化
	{}
protected:
	float score;
};

//声明多重继承的派生类Graduate
class Graduate:public Teacher,public Student//Teacher和Student为直接基类
{
public:
	Graduate(string nam,char s,int a,string t,float sco):Person(nam,s,a),Teacher(nam,s,a,t),Student(nam,s,a,sco)
	{}
	void show()
	{
		cout<<"name:"<

你可能感兴趣的:(虚基类,编程基础)