理解虚基类的作用、定义及其构造函数的调用。请阅读以下程序并在注释处补全代码,然后写出程序运行结果。
class Person {
string IdPerson;
string Name;
public:
Person(string, string);
Person();
~Person();
void PrintPersonInfo();
};
Person::Person(string id, string name) {
IdPerson = id;
Name = name;
cout << "构造Person" << endl;
}
Person::Person() {
cout << "构造Person"<<endl;
IdPerson = '\0';
Name = '\0';
}
Person::~Person() {
cout << "析构Person" << endl;
}
void Person::PrintPersonInfo() {
cout << "身份证号:" << IdPerson << endl << "姓名:" << Name << endl;
}
class Student :public virtual Person {
string NoStudent;
string CourseName;
float Grade;
public:
Student(string id, string name, string nostud, string coursename, float grade);
Student();
~Student() { cout << "析构Student" << endl; }
void PrintStudentInfo();
};
Student::Student(string id, string name, string nostud, string coursename, float grade)
:Person(id, name) {
NoStudent = nostud;
CourseName = coursename;
Grade = grade;
cout << "构造 Student" << endl;
}
void Student::PrintStudentInfo() {
PrintPersonInfo();
cout << "学号:" << NoStudent << endl << "课程名:" << CourseName << endl << "成绩:" << Grade <<
endl;
}
class Employee:public virtual Person {
string NoEmployee;
string Title;
public:
Employee(string id, string name, string noemplyee, string title);
~Employee() { cout << "析构Employee" << endl; }
void PrintEmployeeInfo();
};
Employee::Employee(string id, string name, string noemplyee, string title)
:Person(id, name) {
NoEmployee = noemplyee;
Title = title;
cout << "构造Employee" << endl;
}
void Employee::PrintEmployeeInfo() {
cout << "教职工号:" << NoEmployee << endl << "职称:" << Title << endl;
}
class EStudent :public Employee, public Student {
string NoEStudent;
public:
EStudent(string id, string name, string nostudent, string coursename, float grade, string noemployee, string title, string noestud);
~EStudent() { cout << "析构EStudent" << endl; }
void PrintEStudentInfo();
};
//补充代码,EStudent类有参构造函数的实现
void EStudent::PrintEStudentInfo() {
PrintStudentInfo();
PrintEmployeeInfo();
cout << "在职学习号:" << NoEStudent << endl;
}
int main() {
EStudent estu1("320102811226161", "朱海鹏", "Stu201707001", "数据库", 98, "Tch003", "副教授", "EStuO01");
return 0;
}