前言
在写这篇之前特意搜了一下关于教师干部类的菱形继承,发现真有一个和我的题目要求相似的,我也不晓得原因,不过我这个还真是老师布置的作业,至于两篇的相同点与不同点,我给不出结果,问就是我没看!
特别要说明一点!上一篇中提到自定义toString() 函数关于编译器不同的区别,并且给出了Visual Studio的代码展示,这篇就给出Dev-cpp的编译代码 (上篇:用C++实现多态)
主函数和类中的成员变量可以自行定义,如果需要交互界面,可以查看以前文章用C++实现复数的四则运算
功能介绍
先看一下题目要求,差不多就是功能展示了吧 上面提到的多重继承,我想老师的意思是想让我们写一个菱形继承
结果展示
同样没有写交互界面,我直接在main函数中创建了四个对象,并display 了出来,最后一个对象用的show ,个人认为还是统一用display 比较好,因为代码风格相同,直接CV大法就好
代码区
员工类
#include<iostream>
#include<stdlib.h>
using namespace std;
class Staff {
public:
Staff();
Staff(string name, int age, char sex, string addr, string num);
string getName();
char getSex();
string toString();
void display();
private:
string m_name;
int m_age;
char m_sex;
string m_address;
string m_number;
};
Staff::Staff() { }
Staff::Staff(string name, int age, char sex, string addr, string num)
: m_name(name), m_age(age), m_sex(sex), m_address(addr), m_number(num) {
}
string Staff::toString() {
char age[10];
itoa(m_age, age, 10);
string ret = m_name + "的年龄是" + age;
string sex;
if (m_sex == 'F' || m_sex == 'f') {
sex = ",他的地址及电话分别是";
} else {
sex = ",她的地址及电话分别是";
}
string addr_num = m_address + "和" + m_number;
return ret + sex + addr_num + "。";
}
void Staff::display() {
cout << toString() << endl;
}
string Staff::getName() {
return m_name;
}
char Staff::getSex() {
return m_sex;
}
教师类
class Teacher : virtual public Staff {
public:
Teacher();
Teacher(string title);
Teacher(string title, string name, int age, char sex, string addr, string num);
string toString();
void display();
private:
string m_title;
};
Teacher::Teacher() { }
Teacher::Teacher(string title) : m_title(title) { }
Teacher::Teacher(string title, string name, int age, char sex, string addr, string num)
: m_title(title), Staff(name, age, sex, addr, num) { }
string Teacher::toString() {
string ret = Staff::toString();
string sex;
if (Staff::getSex() == 'F' || Staff::getSex() == 'f') {
sex = "\n并且他的职称是";
} else {
sex = "\n并且她的职称是";
}
return ret + sex + m_title + "。";
}
void Teacher::display() {
cout << toString() << endl;
}
干部类 可以发现干部和教师就是CV大法实现的
class Cadre : virtual public Staff {
public:
Cadre();
Cadre(string post);
Cadre(string post, string name, int age, char sex, string addr, string num);
string getPost();
string toString();
void display();
private:
string m_post;
};
Cadre::Cadre() { }
Cadre::Cadre(string post) : m_post(post) { }
Cadre::Cadre(string post, string name, int age, char sex, string addr, string num)
:m_post(post), Staff(name, age, sex, addr, num) { }
string Cadre::getPost() {
return m_post;
}
string Cadre::toString() {
string ret = Staff::toString();
string sex;
if (Staff::getSex() == 'F' || Staff::getSex() == 'f') {
sex = "\n并且他的职务是";
} else {
sex = "\n并且她的职务是";
}
return ret + sex + m_post + "。";
}
void Cadre::display() {
cout << toString() << endl;
}
教师干部类
class Teacher_Cadre :public Teacher, public Cadre {
public:
Teacher_Cadre();
Teacher_Cadre(string wages, string title, string post, string name, int age, char sex, string addr, string num);
void show();
private:
string m_wages;
};
Teacher_Cadre::Teacher_Cadre() { }
Teacher_Cadre::Teacher_Cadre(string wages, string title, string post, string name, int age, char sex, string addr, string num)
: m_wages(wages), Cadre(post), Teacher(title), Teacher::Staff(name, age, sex, addr, num) { }
void Teacher_Cadre::show() {
Teacher::display();
cout << Staff::getName() << "的职务是" << Cadre::getPost() << ",工资是" << m_wages << endl;
}
main函数
int main() {
Staff aa("小红", 25, 'M', "莱茵河", "15500001314");
Teacher bb("正级教师", "小明", 50, 'F', "流沙河", "12388486666");
Cadre cc("党委书记", "小华", 45, 'f', "石川河", "10166880000");
Teacher_Cadre dd("10k", "高级教师", "党委副书记", "小光", 30, 'm', "淮河", "11155558888");
aa.display(); cout << endl;
bb.display(); cout << endl;
cc.display(); cout << endl;
dd.show();
return 0;
}
|