虚基类的简单应用

#include <iostream>
#include <string>
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:"<<name<<endl;
		cout<<"sex:"<<sex<<endl;
		cout<<"age:"<<age<<endl;
		cout<<"title:"<<title<<endl;
		cout<<"score:"<<score<<endl;
	}
};
int main(void)
{
	Graduate grad1("晓明",'f',25,"视频压缩",6666);
	grad1.show();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值