aoki.A.Z 发表于 2020-5-15 23:31:37

大佬,求求了,在线等,急QAQ

/*
**程序说明:考察“继承和派生”、“动态多态性”的原理、机制和实现
(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 << '\n'
      << "age: " << id._age << '\n'
      << "role: " << role << '\n'
      << "number: " << id._number << '\n'
      << "department: " << id._department << endl;
}
void Account::writeToFile() {
    ofstream outFile("res.txt");
    outFile << "name: " << id._name << '\n'
            << "sex: " << sex << '\n'
            << "age: " << id._age << '\n'
            << "role: " << 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;
}
void TeacherAccount::showPosition() {
    cout << "position: " << position << 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;
}
void StudentAccount::showMajor() {
    cout << "major: " << major << 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;
}
file:///C:\Users\Lenovo\AppData\Local\Temp\ksohtml10148\wps1.jpg
图1 屏幕输出内容
file:///C:\Users\Lenovo\AppData\Local\Temp\ksohtml10148\wps2.jpg
图2 输出文件内容

aoki.A.Z 发表于 2020-5-15 23:32:18

这根本看不了啊

aoki.A.Z 发表于 2020-5-15 23:45:42

本帖最后由 aoki.A.Z 于 2020-5-15 23:48 编辑

/*
**程序说明:考察“继承和派生”、“动态多态性”的原理、机制和实现
(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 << '\n'
      << "age: " << id._age << '\n'
      << "role: " << role << '\n'
      << "number: " << id._number << '\n'
      << "department: " << id._department << endl;
}
void Account::writeToFile() {
    ofstream outFile("res.txt");
    outFile << "name: " << id._name << '\n'
            << "sex: " << sex << '\n'
            << "age: " << id._age << '\n'
            << "role: " << 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;
}
void TeacherAccount::showPosition() {
    cout << "position: " << position << 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;
}
void StudentAccount::showMajor() {
    cout << "major: " << major << 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 输出文件内容
第一次用
这个图片怎么弄啊,我放在我QQ小号的空间里了
https://user.qzone.qq.com/2033805081/main
https://user.qzone.qq.com/2033805081/main

aoki.A.Z 发表于 2020-5-15 23:46:40

aoki.A.Z 发表于 2020-5-15 23:45
/*
**程序说明:考察“继承和派生”、“动态多态性”的原理、机制和实现
(1)类的继承结构:基类Account ...

https://user.qzone.qq.com/2033805081/main

aoki.A.Z 发表于 2020-5-15 23:47:11

https://user.qzone.qq.com/2033805081/main

aoki.A.Z 发表于 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 << '\n'
      << "age: " << id._age << '\n'
      << "role: " << role << '\n'
      << "number: " << id._number << '\n'
      << "department: " << id._department << endl;
}
void Account::writeToFile() {
    ofstream outFile("res.txt");
    outFile << "name: " << id._name << '\n'
            << "sex: " << sex << '\n'
            << "age: " << id._age << '\n'
            << "role: " << 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;
}
void TeacherAccount::showPosition() {
    cout << "position: " << position << 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;
}
void StudentAccount::showMajor() {
    cout << "major: " << major << 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 输出文件内容

aoki.A.Z 发表于 2020-5-15 23:51:04

第一次用,图片怎么发啊
图片我发在这个QQ小号的空间里了2张图片

aoki.A.Z 发表于 2020-5-15 23:51:35

这图片怎么发啊

sunrise085 发表于 2020-5-16 00:42:19

你等级不够,还不能发图片

发代码教程
页: [1]
查看完整版本: 大佬,求求了,在线等,急QAQ