程序头部注释开始
* 程序的版权和版本声明部分
* 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
请按任意键继续. . .
小感:同一个派生类中有两个同名的数据成员,在引用数据成员是应指明作用域