1.定义virtual base class:people,要求: 数据成员:编号id,姓名name,性别sex, 联系电话tel,邮箱e_mail,出生日期bir,bir是struct结构,包括年月日; 虚函数成员:显示show(),使用恰当格式显示全部数据成员; 2.定义derived class:teacher: public virtual people,要求: 增加数据成员:学位degree,部门dep; 成员函数:显示show(),使用恰当格式显示增加的数据成员; 3.定义derived class:student: public virtual people,要求: 增加数据成员:学号sno,专业major; 成员函数:显示show(),使用恰当格式显示增加的数据成员; 4.定义derived class:course: public teacher,public student,要求: 增加数据成员:课程信息cinfo,cinfo是struct结构,包括课程名称name,课程id,选课学生s[],任课教师t; 成员函数:显示show(),使用恰当格式显示增加的数据成员;画出5个class的继承派生关系图,使用C++语言设计各个class,在下面拷贝class的代码,并添加必要的注释:
时间: 2024-04-21 09:28:46 浏览: 120
以下是代码实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
// 定义人类基类
class People {
public:
// 构造函数
People(int id, string name, string sex, string tel, string e_mail, int year, int month, int day) {
this->id = id;
this->name = name;
this->sex = sex;
this->tel = tel;
this->e_mail = e_mail;
this->bir.year = year;
this->bir.month = month;
this->bir.day = day;
}
// 虚函数显示
virtual void show() {
cout << "编号:" << id << endl;
cout << "姓名:" << name << endl;
cout << "性别:" << sex << endl;
cout << "联系电话:" << tel << endl;
cout << "电子邮箱:" << e_mail << endl;
cout << "出生日期:" << bir.year << "-" << bir.month << "-" << bir.day << endl;
}
protected:
// 数据成员
int id;
string name;
string sex;
string tel;
string e_mail;
struct Birthday {
int year;
int month;
int day;
} bir;
};
// 定义教师派生类
class Teacher : public virtual People {
public:
// 构造函数
Teacher(int id, string name, string sex, string tel, string e_mail, int year, int month, int day, string degree, string dep)
: People(id, name, sex, tel, e_mail, year, month, day), degree(degree), dep(dep) {}
// 虚函数显示
virtual void show() {
People::show(); // 调用基类的show函数
cout << "学位:" << degree << endl;
cout << "部门:" << dep << endl;
}
protected:
// 增加数据成员
string degree;
string dep;
};
// 定义学生派生类
class Student : public virtual People {
public:
// 构造函数
Student(int id, string name, string sex, string tel, string e_mail, int year, int month, int day, int sno, string major)
: People(id, name, sex, tel, e_mail, year, month, day), sno(sno), major(major) {}
// 虚函数显示
virtual void show() {
People::show(); // 调用基类的show函数
cout << "学号:" << sno << endl;
cout << "专业:" << major << endl;
}
protected:
// 增加数据成员
int sno;
string major;
};
// 定义课程派生类
class Course : public Teacher, public Student {
public:
// 构造函数
Course(int id, string name, string sex, string tel, string e_mail, int year, int month, int day, string degree, string dep, int sno, string major, string cname, int cid, string tname)
: People(id, name, sex, tel, e_mail, year, month, day), Teacher(id, name, sex, tel, e_mail, year, month, day, degree, dep), Student(id, name, sex, tel, e_mail, year, month, day, sno, major), cname(cname), cid(cid), tname(tname) {}
// 虚函数显示
virtual void show() {
People::show(); // 调用基类的show函数
cout << "课程名称:" << cname << endl;
cout << "课程ID:" << cid << endl;
cout << "选课学生:";
for (int i = 0; i < sizeof(s) / sizeof(s[0]); i++) {
cout << s[i] << " ";
}
cout << endl;
cout << "任课教师:" << tname << endl;
}
protected:
// 增加数据成员
struct CourseInfo {
string name;
int id;
string s[100];
string tname;
} cinfo;
string cname;
int cid;
string tname;
};
int main()
{
People* p1 = new Teacher(1, "张三", "男", "1234567", "zhangsan@qq.com", 1990, 1, 1, "博士", "计算机科学与技术");
People* p2 = new Student(2, "李四", "女", "2345678", "lisi@qq.com", 2000, 2, 2, 20210001, "计算机科学与技术");
People* p3 = new Course(3, "王五", "男", "3456789", "wangwu@qq.com", 1980, 3, 3, "硕士", "数学系", 20210001, "计算机科学与技术", "C++语言", 1001, "张三");
p1->show();
cout << endl;
p2->show();
cout << endl;
p3->show();
cout << endl;
delete p1;
delete p2;
delete p3;
return 0;
}
```
以下是继承关系图:
```
People
|
|-------------|
Teacher Student
|-------------|
Course
```
阅读全文