分别定义一个教师类Teacher和干部类Leader采用多重继承来派生一个新类-双职教师类DbTeacher。假设要管理下述几类人员的一些相关数据:教师类:姓名性别出生日期职称电话。干部类:姓名性别出生日期职务电话。双职教师类:姓名性别出生日期职务职称电话工资。提示:教师类和干部类都有姓名性别出生日期和电话属性请设计它们的基类职员类Staff。Staff为虚基类。
代码如下:
#include <iostream>
#include <string>
using namespace std;
class Staff {
protected:
string name;
char gender;
string birthday;
string phone;
public:
Staff(const string& name, char gender, const string& birthday, const string& phone)
: name(name), gender(gender), birthday(birthday), phone(phone) {}
virtual void display() const {
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "出生日期:" << birthday << endl;
cout << "电话:" << phone << endl;
}
};
class Teacher : virtual public Staff {
protected:
string title;
public:
Teacher(const string& name, char gender, const string& birthday, const string& phone, const string& title)
: Staff(name, gender, birthday, phone), title(title) {}
virtual void display() const {
Staff::display();
cout << "职称:" << title << endl;
}
};
class Leader : virtual public Staff {
protected:
string position;
public:
Leader(const string& name, char gender, const string& birthday, const string& phone, const string& position)
: Staff(name, gender, birthday, phone), position(position) {}
virtual void display() const {
Staff::display();
cout << "职务:" << position << endl;
}
};
class DbTeacher : public Teacher, public Leader {
protected:
double salary;
public:
DbTeacher(const string& name, char gender, const string& birthday, const string& phone, const string& title, const string& position, double salary)
: Staff(name, gender, birthday, phone), Teacher(name, gender, birthday, phone, title), Leader(name, gender, birthday, phone, position), salary(salary) {}
void display() const {
Staff::display();
cout << "职务:" << Leader::position << endl;
cout << "职称:" << Teacher::title << endl;
cout << "工资:" << salary << endl;
}
};
int main() {
DbTeacher t("张三", 'M', "1990-01-01", "13812345678", "副教授", "主任", 10000);
t.display();
return 0;
}
输出结果为:
姓名:张三
性别:M
出生日期:1990-01-01
电话:13812345678
职务:主任
职称:副教授
工资:10000
``
原文地址: https://www.cveoy.top/t/topic/fsWK 著作权归作者所有。请勿转载和采集!