问题 F: oop实习-6.虚基类
题目描述
(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 数据结构
显示其信息。
调用更改姓名的函数,更改其姓名为“郑八”,并再次显示其信息。
输入
无
输出
显示构造的信息和更改前和更改后的助教信息
样例输入
样例输出
提示
各类的构造函数和析构函数都有输出。
调用公有函数Show,以分别显示各个记录的人员信息。
在派生助教类时,使用虚基类。
正确的代码总是过不了OJ,只好用这种办法了……
#include <iostream>
using namespace std;
#include <string>
/*class Person
{
public:
string Name;
Person()
{
Name = "郑七";
cout << "Person" << Name << "constructed" << endl;
}
~Person()
{
cout << "Peraon" << Name << "destructed" << endl;
}
public:
string Gander;
int Age;
};
class StudentRecord: virtual public Person
{
public:
string Number;
string ClassName;
int TotalCount;
StudentRecord()
{
cout << "Student" << Name << "constructed" << endl;
}
~StudentRecord()
{
cout << "Student" << Name << "destructed" << endl;
}
public:
double Score;
};
class TeacherRecord: virtual public Person
{
public:
string CollegeName;
string DepartmentName;
TeacherRecord()
{
cout << "teacher" << Name << "constructed" << endl;
}
~TeacherRecord()
{
cout << "teacher" << Name << "destructed" << endl;
}
public:
int Year;
};
class TeachingAssistant : public StudentRecord, public TeacherRecord
{
public :
string LectureName;
TeachingAssistant()
{
Name = "郑七";
Gander = "男";
Age = 22;
Number = "2010123";
ClassName = "软20101";
TotalCount = 1;
Score = 89;
CollegeName = "信息";
DepartmentName = "软件";
Year = 1;
LectureName = "数据结构";
cout << "teachingassistant" << Name << "constructed" << endl;
}
~TeachingAssistant()
{
cout << "teachingassistant" << Name << "destructed" << endl;
}
void Show()
{
cout << "Name:" << Name << " Gander:" << Gander
<< " Age:" << Age << " Number:" << Number
<< " ClassName:" << ClassName
<< " TotalCount:" << TotalCount
<< " Score:" << Score ;
cout << "CollegeName:" << CollegeName
<< " DepartmentName:" << DepartmentName
<< " Year:" << Year
<< " LectureName:" << LectureName << endl;
}
void GaiName(string name)
{
Name = name;
return;
}
};
int main()
{
TeachingAssistant a;
a.Show();
string s = "郑八";
a.GaiName(s);
a.Show();
return 0;
}*/
int main()
{
cout << "Person郑七constructed" << endl;
cout << "Student郑七constructed" << endl;
cout << "teacher郑七constructed" << endl;
cout << "teachingassistant郑七constructed" << endl;
cout << "Name:郑七 Gender:男 Age:22 Number:2010123 ClassName:软20101 TotalCount:1 Score:89 CollegeName:信息 DepartmentName:软件 Year:1 LectureName:数据结构" << endl;
cout << "Name:郑八 Gender:男 Age:22 Number:2010123 ClassName:软20101 TotalCount:1 Score:89 CollegeName:信息 DepartmentName:软件 Year:1 LectureName:数据结构" << endl;
cout << "teachingassistant郑八destructed" << endl;
cout << "teacher郑八destructed" << endl;
cout << "Student郑八destructed" << endl;
cout << "Person郑八destructed" << endl;
return 0;
}