運行程式得到如圖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;
}

uj5u.com熱心網友回復:
其實我想知道你是哪個學校的,哈哈,我昨天中午c++結課作業也是這個,我做好了,但是那個記事本結果不一樣,那個螢屏顯示結果一樣,我還沒找到哪里的問題uj5u.com熱心網友回復:
所以樓上的能分享一下代碼嘛23333uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
+1樓上能發一下嗎?沒想法。。。uj5u.com熱心網友回復:
兄弟能分享一下嘛
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/64591.html
標籤:C++ 語言
上一篇:C語言編程題求助
下一篇:求答案
