#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;
}
虚基类的简单应用
最新推荐文章于 2022-03-24 14:49:19 发布