|
楼主 |
发表于 2020-5-15 23:49:49
|
显示全部楼层
题目
/*
**程序说明:考察“继承和派生”、“动态多态性”的原理、机制和实现
(1)类的继承结构:基类Account是其直接公有派生类TeacherAccount和StudentAccount的虚基类,而OnJobPostgraduateAccount类是Teacher类和Student类的共同直接公有派生类。
(2)在主函数中,使用基类Account的指针分别指向派生类TeacherAccount、StudentAccount和OnJobPostgraduateAccount的对象,并调用“虚函数”实现“动态多态”。
**作业要求:首先阅读并分析已给代码,然后将程序补充完整,最后运行程序得到如图1所示的屏幕输出结果和如图2所示的文件输出结果。
*/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
typedef enum{TEACHER, STUDENT, ONJOBPOSTGRADUATE} Role;
string role[] = {"Teacher", "Student", "OnJobPostGraduate"};
typedef enum{MALE, FEMALE} Sex;
string sex[] = {"Male", "Female"};
typedef enum{ASSISTANT, LECTURER, APROFESSOR, PROFESSOR} Position;
string position[] = {"Assistatn", "Lecturer", "AProfessor", "Professor"};
typedef enum{CS, EE, CE} Major;
string major[]={"CS", "EE", "CE"};
typedef struct Identity {
string _name;
Sex _sex; //enum{MALE, FEMALE} Sex
int _age;
Role _role; //enum{TEACHER, STUDENT, ONJOBPOSTGRADUATE} Role
int _number;
string _department;
} Id;
class Account {
private:
Id id;
public:
Account(Id);
~Account();
void setRole();
/******补充成员函数声明******/
/****************************/
};
Account::Account(Id i): id(i) {}
Account::~Account() {}
void Account::setRole() {
cout << "Input role (0-Teacher/1-Student/2-OnJobPostGraduate): ";
int x;
cin>>x;
switch(x) {
case 0: id._role = TEACHER; break;
case 1: id._role = STUDENT; break;
case 2: id._role = ONJOBPOSTGRADUATE; break;
}
}
void Account::showId() {
cout << "name: " << id._name << '\n'
<< "sex: " << sex[id._sex] << '\n'
<< "age: " << id._age << '\n'
<< "role: " << role[id._role] << '\n'
<< "number: " << id._number << '\n'
<< "department: " << id._department << endl;
}
void Account::writeToFile() {
ofstream outFile("res.txt");
outFile << "name: " << id._name << '\n'
<< "sex: " << sex[id._sex] << '\n'
<< "age: " << id._age << '\n'
<< "role: " << role[id._role] << '\n'
<< "number: " << id._number << '\n'
<< "department: " << id._department << endl;
outFile.close();
}
/******补充TeacherAccount类的头部******/
/************************/
{
private:
Position pos; //enum{ASSISTANT, LECTURER, APROFESSOR, PROFESSOR} Position
public:
TeacherAccount(Id, Position);
~TeacherAccount();
string& getPosition();
void showPosition();
/******补充成员函数声明******/
/****************************/
};
/******定义TeacherAccount类的构造函数******/
/******************************/
TeacherAccount::~TeacherAccount() {}
string& TeacherAccount::getPosition() {
return position[pos];
}
void TeacherAccount::showPosition() {
cout << "position: " << position[pos] << endl;
}
void TeacherAccount::showId() {
Account::showId();
showPosition();
}
/******补充StudentAccount类的头部******/
/************************/
{
private:
Major maj; //enum{CS, EE, CE} Major
public:
StudentAccount(Id, Major);
~StudentAccount();
string& getMajor();
void showMajor();
/******补充成员函数声明******/
/****************************/
};
/******定义StudentAccount类的构造函数******/
/******************************/
StudentAccount::~StudentAccount() {}
string& StudentAccount::getMajor() {
return major[maj];
}
void StudentAccount::showMajor() {
cout << "major: " << major[maj] << endl;
}
void StudentAccount::showId() {
Account::showId();
showMajor();
}
/******补充OnJobPostgraduateAccount类的头部******/
/************************/
{
public:
OnJobPostgraduateAccount(Id, Position, Major);
~OnJobPostgraduateAccount();
/******补充成员函数声明******/
/****************************/
};
/******定义OnJobPostgraduateAccount类的构造函数******/
/******************************/
OnJobPostgraduateAccount::~OnJobPostgraduateAccount() {}
void OnJobPostgraduateAccount::showId() {
Account::showId();
showPosition();
showMajor();
}
void OnJobPostgraduateAccount::writeToFile() {
Account::writeToFile();
ofstream outFile("res.txt", ios_base::app);
outFile << "position: " << getPosition() << endl
<< "major: " << getMajor() << endl;
outFile.close();
}
int main() {
Id stud = {"Meng Chao", MALE, 18, STUDENT, 20190131, "Dep. of Computer"};
Id tch = {"Zheng Xiao-hong", FEMALE, 25, TEACHER, 1000568, "Dep. of Computer"};
StudentAccount s(stud, CS);
TeacherAccount t(tch, LECTURER);
OnJobPostgraduateAccount g(tch, LECTURER, EE);
g.setRole(); //设置数据成员_role为OnJobPostgraduat
/******定义基类Account的指针变量pt,并初始化为空指针******/
/******对pt进行赋值,实现对TeacherAccount类的showId成员函数的调用******/
/********************/
cout << endl;
/******对pt进行赋值,实现对StudentAccount类的showId成员函数的调用******/
/********************/
cout << endl;
/******对pt进行赋值,实现对OnJobPostgraduateAccount类的showId成员函数的调用******/
/********************/
pt->writeToFile();
return 0;
}
图1 屏幕输出内容
图2 输出文件内容 |
|