12.2 多层继承方式派生

程序头部注释开始
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生
* All rights reserved.
* 文件名称: 第十二周
* 作 者:      杨森
* 完成日期: 2012年 5 月 8 日
* 版 本 号: V 1.0

 

源程序:

#include <iostream>   
#include <string>   
using namespace std;  
class Teacher  
{  
public:  
    Teacher( string nam, int ag, char s, string addr, string tel, string ti);  
    void display();  
    ~Teacher(){}  
private:  
    string name;  
    int age;  
    char sex;  
    string address;  
    string tel;  
    string title;  
};  
class Cadre  
{  
public:  
    Cadre( string nam, int ag, char s, string addr, string tel, string po);  
    void display1();  
    ~Cadre(){}  
protected:  
    string name;  
    int age;  
    char sex;  
    string address;  
    string tel;  
    string post;  
};  
class Teacher_Cadre: public Teacher, public Cadre   
{  
public:  
    Teacher_Cadre( string nam, int ag, char s, string addr, string tel, string ti, string po, int w):Teacher( nam, ag, s, addr, tel, ti),Cadre( nam, ag, s, addr, tel, po){ wages = w;}  
    void show();  
    ~Teacher_Cadre(){}  
private:  
    int wages;  
};  
  
  
           
Teacher::Teacher( string nam, int ag, char s, string addr, string te, string ti)  
{  
    name = nam;  
    age = ag;  
    sex = s;  
    address = addr;  
    tel = te;  
    title = ti;  
}  
  
void Teacher::display()  
{  
    cout << "name: " << name << endl;  
    cout << "tellphone: " << tel << endl;  
    cout << "sex: " << sex << endl;  
    cout << "age: " << age << endl;  
    cout << "address: " << address << endl;  
    cout << "title: " << title <<endl;      
}  
Cadre::Cadre( string nam, int ag, char s, string addr, string te, string po)  
{  
    name = nam;  
    age = ag;  
    sex = s;  
    address = addr;  
    tel = te;  
    post = po;  
}  
  
void Cadre::display1()  
{  
    cout << "name: " << name << endl;  
    cout << "tellphone: " << tel << endl;  
    cout << "sex: " << sex << endl;  
    cout << "age: " << age << endl;  
    cout << "address: " << address << endl;  
    cout << "post: " << post <<endl;  
}  
  
void Teacher_Cadre::show()  
{  
    Teacher::display ();  
    cout << "post: " << post <<endl;  
    cout << "wages: " << wages << endl;  
}  
  
int main()  
{  
    Teacher_Cadre tc("Wang-li",19,'f',"115 Beijing Road,Shanghai", "12345678902", "teacher", "ksjdbh", 9999);  
    tc.show();  
    system("pause");  
    return 0;  
}  


运行结果:

name: Wang-li
tellphone: 12345678902
sex: f
age: 19
address: 115 Beijing Road,Shanghai
title: teacher
post: ksjdbh
wages: 9999
请按任意键继续. . .


小感:同一个派生类中有两个同名的数据成员,在引用数据成员是应指明作用域

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值