标签:
一、问题及代码
/* 文件名称:b52.cpp
* 作者:胡铭坤
* 完成日期:2016 年 5 月 6 日
* 版本号:v1.0
* 对任务及求解方法的描述部分:
* 输入描述:曾辉信息
* 问题描述:分别定义Teacher(教师)类和Cadre(干部)类,采用多重继承方式由
这两个类派生出新类Teacher_Cadre(教师兼干部)。
* 程序输出:在派生类Teacher_Cadre的成员函数show中调用Teacher类中的display函数,输出姓名、
年龄、性别、职称,然后再用cout语句输出职务与工资。
* 问题分析:两个基类
* 算法设计:多继承
*/
#include <iostream>
#include <string>
using namespace std;
class Teacher {
string name;
int age;
bool sex;
string title;
public:
Teacher(string, int, bool, string);
void display();
};
Teacher::Teacher(string _name, int _age, bool _sex, string _title) {
name = _name;
age = _age;
sex = _sex;
title = _title;
}
void Teacher::display() {
cout << "姓名:" << name << endl << "年龄:" << age << endl
<< "性别:" << (sex?"男":"女") << endl <<
"职称:" << title << endl;
}
class Cadre {
string name;
int age;
bool sex;
string post;
public:
Cadre(string, int, bool, string);
void setpost(string);
string getpost();
};
Cadre::Cadre(string _name, int _age, bool _sex, string _post) {
name = _name;
age = _age;
sex = _sex;
post = _post;
}
void Cadre::setpost(string _post) {
post = _post;
}
string Cadre::getpost() {
return post;
}
class Teacher_Cadre: public Teacher, public Cadre {
float wages;
public:
Teacher_Cadre(string, int, bool, string, string, float);
void show();
};
Teacher_Cadre::Teacher_Cadre(string _name, int _age, bool _sex, string _title,
string _post, float _wages):
Teacher(_name, _age, _sex, _title),
Cadre(_name, _age, _sex, _post) {
wages = _wages;
}
void Teacher_Cadre::show() {
Teacher::display();
cout << "职务:" << Cadre::getpost() << endl << "工职:" << wages << endl;
}
int main() {
Teacher_Cadre zh("曾辉", 42, 1, "副教授", "主任", 1534.5);
zh.show();
return 0;
}
三、心得体会:
多继承并不复杂
四、知识点总结:
调用基类成员切记加上限定符 ::
标签:
原文地址:http://blog.csdn.net/index42/article/details/51330046