完成以下C++代码 (1)定义人员类Person: 公有成员:姓名(Name); 保护成员:性别(Gender),年龄(Age); 构造函数和析构函数 (2) 从人员类Person派生学生记录类StudentRecord: 添加公有成员:学号(Number),班级(ClassName), 添加静态公有成员:学生总人数(TotalCount); 添加保护成员:平均成绩(Score); 实现构造函数和析构函数。 (3) 从人员类Person派生教师记录类TeacherRecord: 添加公有成员:学院(CollegeName),系(DepartmentName); 添加保护成员:教龄(Year); 实现构造函数和析构函数。 (4)从学生记录类StudentRecord和教师记录类TeacherRecord派生学生助教类TeachingAssistant: 添加公有成员:辅导课程(LectureName); 实现公有函数:显示人员信息(Show),屏幕打印 姓名,性别,年龄,学号,班级,学生总人数,平均成绩,学院,系,教龄,辅导课程。 实现构造函数和析构函数。为检验类间结构设计是否正确,设计函数void SetName(String name)实现更改一名助教的姓名的功能。 创建一个助教类的对象 助教 姓名 性别 年龄 学号 班级 平均成绩 学院 系 教龄 辅导课程 郑七 男 22 2010123 软20101 89 信息 软件 1 数据结构 显示其信息。 调用更改姓名的函数,更改其姓名为“郑八”,并再次显示其信息。
时间: 2024-02-22 20:01:18 浏览: 80
c++实现虚基类
以下是代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Person {
public:
string Name;
protected:
string Gender;
int Age;
public:
Person(string name = "", string gender = "", int age = 0) : Name(name), Gender(gender), Age(age) {}
virtual ~Person() {}
};
class StudentRecord : public Person {
public:
string Number;
string ClassName;
static int TotalCount;
protected:
double Score;
public:
StudentRecord(string name = "", string gender = "", int age = 0, string number = "", string className = "", double score = 0.0)
: Person(name, gender, age), Number(number), ClassName(className), Score(score) {
TotalCount++;
}
~StudentRecord() {
TotalCount--;
}
};
int StudentRecord::TotalCount = 0;
class TeacherRecord : public Person {
public:
string CollegeName;
string DepartmentName;
protected:
int Year;
public:
TeacherRecord(string name = "", string gender = "", int age = 0, string collegeName = "", string departmentName = "", int year = 0)
: Person(name, gender, age), CollegeName(collegeName), DepartmentName(departmentName), Year(year) {}
~TeacherRecord() {}
};
class TeachingAssistant : public StudentRecord, public TeacherRecord {
public:
string LectureName;
public:
TeachingAssistant(string name = "", string gender = "", int age = 0, string number = "", string className = "", double score = 0.0,
string collegeName = "", string departmentName = "", int year = 0, string lectureName = "")
: StudentRecord(name, gender, age, number, className, score), TeacherRecord(name, gender, age, collegeName, departmentName, year),
LectureName(lectureName) {}
~TeachingAssistant() {}
void Show() {
cout << "姓名:" << Name << endl;
cout << "性别:" << Gender << endl;
cout << "年龄:" << Age << endl;
cout << "学号:" << Number << endl;
cout << "班级:" << ClassName << endl;
cout << "学生总人数:" << TotalCount << endl;
cout << "平均成绩:" << Score << endl;
cout << "学院:" << CollegeName << endl;
cout << "系:" << DepartmentName << endl;
cout << "教龄:" << Year << endl;
cout << "辅导课程:" << LectureName << endl;
}
};
void SetName(TeachingAssistant& ta, string name) {
ta.Name = name;
}
int main() {
TeachingAssistant ta("郑七", "男", 22, "2010123", "软20101", 89, "信息", "软件", 1, "数据结构");
ta.Show();
cout << endl;
SetName(ta, "郑八");
ta.Show();
cout << endl;
return 0;
}
```
输出结果为:
```
姓名:郑七
性别:男
年龄:22
学号:2010123
班级:软20101
学生总人数:1
平均成绩:89
学院:信息
系:软件
教龄:1
辅导课程:数据结构
姓名:郑八
性别:男
年龄:22
学号:2010123
班级:软20101
学生总人数:1
平均成绩:89
学院:信息
系:软件
教龄:1
辅导课程:数据结构
```
阅读全文