#include "stdafx.h"
#include
#include
using namespace std;
class person{//声明基类
protected:
int age;
char sex;
string name;
public:
person(int a,char s,string nam){
age=a;
sex=s;
name=nam;
}
};
class teacher:virtual public person
{
protected:
string title;
public:
teacher(int a,char s,string nam,string t):person(a,s,nam){
title=t;
}
};
class student:virtual public person
{
protected:
float score;
public:
student(int a,char s,string nam,float sc):person(a,s,nam){
score=sc;
}
};
class graduate:public teacher,public student
{
protected:
float wdge;
public:
graduate(int a,char s,string nam,string t,float sc,float wd):person(a,s,nam),teacher(a,s,nam,t),student(a,s,nam,sc){
wdge=wd;
}
void show(){
cout< cout< cout< cout<
本文通过一个具体的C++程序示例介绍了多重继承的概念及其使用方式。该程序定义了person、teacher、student和graduate等类,并展示了如何利用虚拟继承解决钻石问题,避免对象中出现多个相同基类实例的情况。
1609

被折叠的 条评论
为什么被折叠?



