```#include <iostream> #include <string> using namespace std; //基类People class People { public: People(string name, int age); void display(); protected: string m_name; int m_age; }; People::People(string name, int age) : m_name(name), m_age(age) {} void People::display() { cout << m_name << "今年" << m_age << "岁了,是个无业游民。" << endl; } //派生类Teacher class Teacher : public People { public: Teacher(string name, int age, int salary); void display(); private: int m_salary; }; Teacher::Teacher(string name, int age, int salary) : People(name, age), m_salary(salary) {} void Teacher::display() { cout << m_name << "今年" << m_age << "岁了,是一名教师,每月有" << m_salary << "元的收入。" << endl; } int main() { People* p = new People("王志刚", 23); p->display(); p = new Teacher("赵宏佳", 45, 8200); p->display(); return 0; }```
时间: 2024-03-26 19:34:22 浏览: 58
这段代码是一个简单的 C++ 多态示例,定义了一个基类 `People` 和一个派生类 `Teacher`,其中 `Teacher` 继承了 `People`。
在 `People` 类中,定义了一个构造函数 `People(string name, int age)` 和一个 `display()` 函数,用于显示人员的基本信息。
在 `Teacher` 类中,定义了一个构造函数 `Teacher(string name, int age, int salary)` 和一个 `display()` 函数,用于显示教师的信息。
在 `main()` 函数中,首先创建了一个基类指针 `p`,并将其指向一个 `People` 类型的对象,然后调用 `display()` 函数显示该对象的信息。接着,将 `p` 指向一个 `Teacher` 类型的对象,并再次调用 `display()` 函数显示该对象的信息。由于 `Teacher` 类继承了 `People` 类,因此可以通过基类指针来访问 `Teacher` 类的成员函数。
输出结果为:
```
王志刚今年23岁了,是个无业游民。
赵宏佳今年45岁了,是一名教师,每月有8200元的收入。
```
相关问题
R6-1 写出派生类构造方法(C++) 分数 10 全屏浏览题目 切换布局 作者 周强 单位 青岛大学 裁判测试程序样例中展示的是一段定义基类People、派生类Student以及测试两个类的相关C++代码,其中缺失了部分代码,请补充完整,以保证测试程序正常运行。 函数接口定义: 提示: 观察类的定义和main方法中的测试代码,补全缺失的代码。 裁判测试程序样例: 注意:真正的测试程序中使用的数据可能与样例测试程序中不同,但仅按照样例中的格式调用相关函数。 #include <iostream> using namespace std; class People{ private: string id; string name; public: People(string id, string name){ this->id = id; this->name = name; } string getId(){ return this->id; } string getName(){ return name; } }; class Student : public People{ private: string sid; int score; public: Student(string id, string name, string sid, int score) /** 你提交的代码将被嵌在这里(替换此行) **/ } friend ostream& operator <<(ostream &o, Student &s); }; ostream& operator <<(ostream &o, Student &s){ o << "(Name:" << s.getName() << "; id:" << s.getId() << "; sid:" << s.sid << "; score:" << s.score << ")"; return o; } int main(){ Student zs("370202X", "Zhang San", "1052102", 96); cout << zs << endl; return 0; } 输入样例: (无) 输出样例: (Name:Zhang San; id:370202X; sid:1052102; score:96) 代码长度限制 16 KB 时间限制 100 ms 内存限制 2 MB
class Student : public People{
private:
string sid;
int score;
public:
Student(string id, string name, string sid, int score) : People(id, name){
this->sid = sid;
this->score = score;
}
friend ostream& operator <<(ostream &o, Student &s);
};
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的代码,并添加必要的注释:
以下是代码实现:
```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
```
阅读全文