C++第五次上机实验

一、问题及代码


/*  
* 文件名称:exe.cpp
* 作    者:黄会林 
* 完成日期:2016 年 5 月 10日  
* 版 本 号:v1.0  
* 对任务及求解方法的描述部分:使用多继承
* 输入描述: 无  
* 问题描述: 
      分别定义Teacher(教师)类和Cadre(干部)类,
  采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)
* 程序输出: 略 
* 问题分析: 无  
* 算法设计: 无  
*/    
#include <iostream>  
#include <string>  
using namespace std;
class Teacher
{
public:
Teacher(string n,int a,string s,string t):name(n),age(a),sex(s),title(t){}
    void display();
private:
    string name;
int age;
string sex;
string title;
};
void Teacher::display()
{
    cout<<"姓名:"<<name<<endl;
    cout<<"年龄:"<<age<<endl;
    cout<<"性别:"<<sex<<endl;
    cout<<"职称:"<<title<<endl;
}
class Cadre
{
public:
Cadre(string n,int a,string s,string p):name(n),age(a),sex(s),post(p){}
void showpost();
    void display();
private:
    string name;
int age;
string sex;
string post;
};
void Cadre::showpost()
{
    cout<<"职务:"<<post<<endl;
}
class Teacher_Cadre:public Teacher,public Cadre
{
public:
    Teacher_Cadre(string n1,int a1,string s1,string t,string n2,int a2,string s2,string p,double w):Teacher(n1,a1,s1,t),Cadre(n2,a2,s2,p),wages(w){}
void showwages();
void show(Teacher_Cadre &tc);
private:
double wages;
};
void Teacher_Cadre::showwages()
{
    cout<<"工资:"<<wages<<endl;
}
void Teacher_Cadre::show(Teacher_Cadre &tc)
{
    tc.Teacher::display();
tc.Cadre::showpost();
tc.showwages();
}
int main()
{
    Teacher_Cadre tc("曾辉",42,"男","职称","曾辉",42,"男","职务",1534.5);
tc.show(tc);
return 0;
}

二、运行结果


三、心得体会

多继承要注意使用构造函数时的一些注意实项和继承顺序。

四、知识点总结

多继承构造函数使用。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值