1,统计一公用于人事管理的People(人员类)” 基类。 考虑到通用性,仅抽象出各类人员都县有的属性:编号、姓名、性别、出生日期 (Date对象)、身份证号等; 2,从People(人员类)派生出Student(学生类),并添加属性:班号classNO; 3,从People类派生出Teacher (教师类),并添加属性:职务principalship、部门门department; 4,从Student类中派生出Graduate(研究生)类,并添加属性:专业subject、导师adviser(该属性是Teacher类对 象); 5,从Graduate类和Teacher类派生出助教生 类TA,无新的属性。设计该类时注意虛基类的使用,注意重载相应的成员 函数。 6,编写main函数测试这些类。在main函数中设计测试用例 时,注意考虑如何体现成员函数的覆盖。 int main() { Date t_d(1970,1,1); People t_p(14007,"李四","man", "420107197001012020",t_d); Teacher t_t(t_p,"教授","计算机科学与技术"); Date s_d(1998,2,2); People s_p(2020001,"张三","man", "420107199802022046",s_d); Student s_s(s_p,"2002"); Graduate s_g(s_s,"计算机科学与技术",t_t); Teacher s_t(s_s,"讲师","计算机科学与技术"); TA ta(s_g,s_t); ta.show(); },给出具体c++代码,要求有复制构造函数和delete·
时间: 2023-06-24 12:08:26 浏览: 240
以下是具体的C++代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Date {
private:
int year;
int month;
int day;
public:
Date(int y, int m, int d): year(y), month(m), day(d) {}
void show() const {
cout << year << '-' << month << '-' << day;
}
};
class People {
private:
int id;
string name;
string gender;
string idCard;
Date birth;
public:
People(int i, const string& n, const string& g, const string& c, const Date& b):
id(i), name(n), gender(g), idCard(c), birth(b) {}
People(const People& p):
id(p.id), name(p.name), gender(p.gender), idCard(p.idCard), birth(p.birth) {}
virtual ~People() = default;
virtual void show() const {
cout << "编号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << gender << endl;
cout << "身份证号:" << idCard << endl;
cout << "出生日期:";
birth.show();
cout << endl;
}
};
class Student : virtual public People {
private:
string classNo;
public:
Student(const People& p, const string& c): People(p), classNo(c) {}
void show() const override {
People::show();
cout << "班号:" << classNo << endl;
}
};
class Teacher : virtual public People {
private:
string principalship;
string department;
public:
Teacher(const People& p, const string& ps, const string& d): People(p), principalship(ps), department(d) {}
void show() const override {
People::show();
cout << "职务:" << principalship << endl;
cout << "部门:" << department << endl;
}
};
class Graduate : public Student, public Teacher {
private:
string subject;
Teacher* adviser;
public:
Graduate(const Student& s, const string& sub, Teacher* a): People(s), Student(s), Teacher(*a), subject(sub), adviser(a) {}
void show() const override {
People::show();
cout << "班号:" << classNo << endl;
cout << "专业:" << subject << endl;
cout << "导师:";
adviser->show();
}
};
class TA : public Graduate, public Teacher {
public:
TA(const Graduate& g, const Teacher& t): People(g), Student(g), Graduate(g), Teacher(t) {}
void show() const override {
Graduate::show();
}
};
int main() {
Date t_d(1970, 1, 1);
People t_p(14007, "李四", "man", "420107197001012020", t_d);
Teacher t_t(t_p, "教授", "计算机科学与技术");
Date s_d(1998, 2, 2);
People s_p(2020001, "张三", "man", "420107199802022046", s_d);
Student s_s(s_p, "2002");
Graduate s_g(s_s, "计算机科学与技术", &t_t);
Teacher s_t(s_s, "讲师", "计算机科学与技术");
TA ta(s_g, s_t);
ta.show();
return 0;
}
```
其中,为了实现虚基类的特性,我们在派生类中使用了虚拟继承。同时,为了避免出现二义性问题,我们在派生类的构造函数中通过明确调用基类的构造函数来初始化虚基类的成员。在派生类中重载基类的成员函数时,需要加上 `override` 关键字。最后,为了防止内存泄漏,我们在类的析构函数中使用了 `default` 关键字。
阅读全文