实验目的:多重继承
* 程序头部注释开始
* 程序的版权和版本声明部分
* 烟台大学计算机学院学生
* 文件名称:
* 作
* 完成日期:
* 版本号:
#include <iostream>
#include<string>
using namespace std;
class Teacher
{
public:
Teacher(string nam,int a ,char s,string ad, int te,string t)
{
name = nam;
age = a;
sex = s;
add = ad;
tel = te;
title = t;
}
~Teacher(){};
void display();
protected:
string name;
int age;
char sex;
string add;
int tel;
string title;
};
class Cadre
{
public:
Cadre(string nam,int a ,char s,string ad, int te,string p)
{
name = nam;
age = a;
sex = s;
add = ad;
tel = te;
post = p;
}
~Cadre(){};
protected:
string name;
int age;
char sex;
string add;
int tel;
string post;
};
class Teacher_Cadre:public Teacher,public Cadre
{
public:
Teacher_Cadre(string nam,int a ,char s,string ad, int te,string t,string p,float w):Teacher(nam,a,s,ad,te,t),Cadre(nam,a,s,ad,te,p),wages(w){}
void show();
private:
float wages;
};
int main()
{
Teacher_Cadre T_C("hubin", 20, 'm', "yantaidaxue", 182531," tejijiaoshi"," gongwuyuan", 5000);
T_C.show();
system("pause");
return 0;
}
void Teacher::display()
{
cout<<"mane:"<<Teacher::name<<endl;
cout<<"age:"<<Teacher::age<<endl;
cout<<"sex:"<<Teacher::sex<<endl;
cout<<"add:"<<Teacher::add<<endl;
cout<<"tel:"<<Teacher::tel<<endl;
cout<<"title:"<<Teacher::title<<endl;
}
void Teacher_Cadre::show()
{
Teacher::display();
cout<<"post:";
cout<<post<<endl;
cout<<"wages:"<<wages<<endl;
}
截图:
mane:hubin
age:20
sex:m
add:yantaidaxue
tel:182531
title: tejijiaoshi
post: gongwuyuan
wages:5000
请按任意键继续. . .
继承多了,虽然有点不适应,但功能还是挺大的。
本文通过一个具体的实例展示了多重继承在C++中的使用,包括类Teacher、Cadre的定义及其子类Teacher_Cadre的实现。详细阐述了如何在类中定义属性和方法,并通过构造函数初始化数据成员。此外,还介绍了如何在子类中重用父类的功能,以及通过虚函数实现多态性。最后,通过主函数展示了类实例化和成员变量访问的过程。

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



