第12周 【项目1 - 教师兼干部类】

问题描述:

分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)。要求: 
(1)在两个基类中都包含姓名、年龄、性别、地址、电话等数据成员。 
(2)在Teacher类中还包含数据成员title(职称),在Cadre类中还包含数据成员post(职务),在Teacher_Cadre类中还包含数据成员wages(工资)。 
(3)对两个基类中的姓名、年龄、性别、地址、电话等数据成员用相同的名字,在引用这些数据成员时,指定作用域。 
(4)在类体中声明成员函数,在类外定义成员函数。 
(5)在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、年龄、性别、职称、地址、电话,然后再用cout语句输出职务与工资。

代码:

#include <iostream>
#include <cstring>
using namespace std;
class Teacher{
protected:
    string name;
    string sex;
    string address;
    string phone;
    string title;
    int age;
public:
    Teacher(string n,string s,string a,string p,string t,int ag){
        name=n;sex=s;address=a;
        phone=p;title=t,age=ag;
    }
    void output();
};
void Teacher::output(){
    cout<<"name: "<<name<<'\12';
    cout<<"sex: "<<sex<<'\12';
    cout<<"address: "<<address<<'\12';
    cout<<"phone: "<<phone<<'\12';
    cout<<"title: "<<title<<'\12';
    cout<<"age: "<<age<<'\12';
}
class Cadre{
protected:
    string name;
    string sex;
    string address;
    string phone;
    string past;
    int age;
public:
    Cadre(string n,string s,string a,string p,string pa,int ag)
    :name(n),sex(s),address(a),phone(p),past(pa),age(ag){}
    void output();
};
void Cadre::output(){
    cout<<"name: "<<name<<'\12';
    cout<<"sex: "<<sex<<'\12';
    cout<<"address: "<<address<<'\12';
    cout<<"phone: "<<phone<<'\12';
    cout<<"past: "<<past<<'\12';
    cout<<"age: "<<age<<'\12';
}
class Teacher_Cadre:public Teacher,public Cadre{
private:
    double wages;
public:
    Teacher_Cadre(string n,string s,string a,string p,string t,string pa,int ag,double w)
    :Teacher(n,s,a,p,t,ag),Cadre(n,s,a,p,pa,ag),wages(w){}
    void show();
};
void Teacher_Cadre::show(){
    cout<<"name: "<<Teacher::name<<'\12';
    cout<<"sex: "<<Teacher::sex<<'\12';
    cout<<"address: "<<Teacher::address<<'\12';
    cout<<"phone: "<<Teacher::phone<<'\12';
    cout<<"title: "<<Teacher::title<<'\12';
    cout<<"past: "<<Cadre::past<<'\12';
    cout<<"age: "<<Teacher::age<<'\12';
    cout<<"wages: "<<wages<<'\12';
}
int main(){
    Teacher_Cadre my("小范","男","烟台大学南校区","178628*****","讲师","助教",22,8888);
    my.show();
    cout<<'\12';
    my.Teacher::output();
    cout<<'\12';
    my.Cadre::output();
    return 0;
}


 

运行结果:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值