枫程序 2023-06-03 09:34 采纳率: 87.5%
浏览 123
已结题

C++类和对象,多态性,继承,虚函数虚基类

解决代码存在的问题:terminate called after throwing an instance of 'std::bad_alloc'
what(): std::bad_alloc,代码输入到graduate的数据后就终止了,如何解决?求解正确输入输出!!!
people类派生出student类,添加属性:班号string classNO;从people类派生出teacher类,添加属性:职务string principalship、部门string department。从student类中派生出graduate类,添加属性:专业stringr subject、导师teacher advisor;从graduate类和teacher类中派生出TA(助教)类,注意虚基类的使用。用成员函数实现对人员信息的录入和显示。附:people类的属性:number(编号)、sex(性别)、birthday(出生日期)、id(身份证号)等等。其中“出生日期”声明为一个“日期”类内嵌子对象。

#include <iostream>
#include <string>
using namespace std;

// 定义出生日期Bir内嵌类
class Bir
{
public:
    Bir() {}
    Bir(int Y, int M, int D) : year(Y), month(M), day(D)
    {
        cout << "Bir构造函数执行中......" << endl;
    };
    ~Bir()
    {
        cout << "Bir析构函数执行中......" << endl;
    }
    Bir(const Bir &other) : year(other.year), month(other.month), day(other.day) { cout << "Bir复制构造函数执行中......" << endl; };
    inline int Get_year() const { return year; }
    inline int Get_month() const { return month; }
    inline int Get_day() const { return day; }

private:
    int year;
    int month;
    int day;
};

// 定义人员管理类People
class People
{
public:
    People() {}
    People(string Number, string Sex, Bir Birthday, string Id)
        : number(Number), sex(Sex), birthday(Birthday), id(Id) { cout << "People构造中......" << endl; };
    ~People() { cout << "People析构中......" << endl; }
    virtual void print() const;
    People(const People &other)
        : number(other.number), sex(other.sex), birthday(other.birthday), id(other.id) { cout << "People复制构造中......" << endl; };

protected:
    string number;
    string sex;
    Bir birthday;
    string id;
};
void People::print() const
{
    cout << "number: " << number << endl;
    cout << "sex: " << sex << endl;
    cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
    cout << "id: " << id << endl;
}
class Student : virtual public People
{
protected:
    string classNO;

public:
    Student() {}
    Student(string Number, string Sex, Bir Birthday, string Id, string classno) : People(Number, Sex, Birthday, Id), classNO(classno)
    {
        cout << "Student构造中......" << endl;
    }
    ~Student()
    {
        cout << "Student析构中......" << endl;
    }
    Student(const Student &other)
        : People(number, sex, birthday, id), classNO(other.classNO) { cout << "Student复制构造中......" << endl; };
    virtual void print() const
    {
        cout << "number: " << number << endl;
        cout << "sex: " << sex << endl;
        cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
        cout << "id: " << id << endl;
        cout << "classno: " << classNO << endl;
    }
};
class Teacher : virtual public People
{
protected:
    string Principalship;
    string Department;

public:
    Teacher() {}
    Teacher(string Number, string Sex, Bir Birthday, string Id, string principalship, string department) : People(Number, Sex, Birthday, Id), Principalship(principalship), Department(department)
    {
        cout << "Teacher构造中......" << endl;
    }
    Teacher(const Teacher &other) : People(number, sex, birthday, id), Principalship(other.Principalship), Department(other.Department)
    {
        cout << "Teacher复制构造中......" << endl;
    }
    virtual void print() const
    {
        cout << "number: " << number << endl;
        cout << "sex: " << sex << endl;
        cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
        cout << "id: " << id << endl;
        cout << "Principalship: " << Principalship << endl;
        cout << "Department: " << Department << endl;
    }
    ~Teacher()
    {
        cout << "Teacher析构中......" << endl;
    }
};
class Graduate : virtual public Student
{
protected:
    string Subject;
    Teacher Advisor;

public:
    Graduate() {}
    Graduate(string Number, string Sex, Bir Birthday, string Id, string classno, string subject, Teacher advisor) : Student(Number, Sex, Birthday, Id, classno), Subject(subject), Advisor(advisor)
    {
        cout << "Graduate构造中......" << endl;
    }
    Graduate(const Graduate &other) : Student(number, sex, birthday, id, classNO), Subject(other.Subject), Advisor(other.Advisor)
    {
        cout << "Graduate复制构造中......" << endl;
    }
    virtual void print() const
    {
        Advisor.print();
        cout << "Subject: " << Subject << endl;
    }
    ~Graduate()
    {
        cout << "Graduate析构中......" << endl;
    }
};
class TA : virtual public Graduate, virtual public Teacher
{
public:
    TA() {}
    TA(string Number, string Sex, Bir Birthday, string Id, string classno, string subject, Teacher advisor, string principalship, string department) : Graduate(
                                                                                                                                                           Number, Sex, Birthday, Id, classno, subject, advisor),
                                                                                                                                                       Teacher(Number, Sex, Birthday, Id, principalship, department)
    {
        cout << "TA构造中......" << endl;
    }
    TA(const TA &other) : Graduate(number, sex, birthday, id, classNO, Subject, Advisor),
                          Teacher(number, sex, birthday, id, Principalship, Department)
    {
        cout << "TA复制构造中......" << endl;
    }
    virtual void print() const
    {
        cout << "number: " << number << endl;
        cout << "sex: " << sex << endl;
        cout << "birthday: " << birthday.Get_year() << "年" << birthday.Get_month() << "月" << birthday.Get_day() << "日" << endl;
        cout << "id: " << id << endl;
        cout << "Principalship: " << Principalship << endl;
        cout << "Department: " << Department << endl;
        cout << "Subject: " << Subject << endl;
    }
    ~TA()
    {
        cout << "TA析构中......" << endl;
    }
};
void test()
{

    string Number, Sex, Id, classno, subject, principalship, department;
    int YEAR, MONTH, DAY, n1, n2, n3, n4;
    Teacher tea1;
    Teacher tea2;
    cout << "Student:" << endl;
    cin >> n1;
    Student *stu = new Student[n1];
    for (int i = 0; i < n1; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        stu[i] = Student(Number, Sex, bir_thday, Id, classno);
    }
    cout << "Teacher:" << endl;
    cin >> n2;
    Teacher *tea = new Teacher[n2];
    for (int i = 0; i < n1; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea[i] = Teacher(Number, Sex, bir_thday, Id, principalship, department);
    }
    cout << "Graduate:" << endl;
    cin >> n3;
    Graduate *gra = new Graduate[n3];
    for (int i = 0; i < n3; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        cout << "Input subject:" << endl;
        cin >> subject;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea1 = Teacher(Number, Sex, bir_thday, Id, principalship, department);
        gra[i] = Graduate(Number, Sex, bir_thday, Id, classno, subject, tea1);
    }
    cout << "TA:" << endl;
    cin >> n4;
    TA *ta = new TA[n4];
    for (int i = 0; i < n4; i++)
    {
        cout << "Input number: " << endl;
        cin >> Number;
        cout << "Input sex: " << endl;
        cin >> Sex;
        cout << "Input birthday:" << endl;
        cin >> YEAR >> MONTH >> DAY;
        Bir bir_thday(YEAR, MONTH, DAY);
        cout << "Input id:" << endl;
        cin >> Id;
        cout << "Input classno:" << endl;
        cin >> classno;
        cout << "Input subject:" << endl;
        cin >> subject;
        cout << "Input principalship:" << endl;
        cin >> principalship;
        cout << "Input department:" << endl;
        cin >> department;
        tea2 = Teacher(Number, Sex, bir_thday, Id, principalship, department);
        ta[i] = TA(Number, Sex, bir_thday, Id, classno, subject, tea2, principalship, department);
    }
    for (int i = 0; i < n1; i++)
    {
        stu[i].print();
    }
    for (int i = 0; i < n2; i++)
    {
        tea[i].print();
    }
    for (int i = 0; i < n3; i++)
    {
        gra[i].print();
    }
    for (int i = 0; i < n4; i++)
    {
        ta[i].print();
    }
    delete[] stu;
    delete[] tea;
    delete[] gra;
    delete[] ta;
}
int main()
{
    test();
    return 0;
}


  • 写回答

11条回答 默认 最新

  • 真相重于对错 2023-06-03 10:56
    关注
    获得1.05元问题酬金
    
    Teacher(const Teacher& other) : /*People(number, sex, birthday, id),*/ //把这里注释掉
    
    评论

报告相同问题?

问题事件

  • 系统已结题 6月11日
  • 请回答用户的提问 6月3日
  • 修改了问题 6月3日
  • 修改了问题 6月3日
  • 展开全部

悬赏问题

  • ¥15 FOR循环语句显示查询超过300S错误怎么办
  • ¥15 数电设计题 没有设计思路 不知道用什么芯片进行设计 求提供设计思路
  • ¥15 在动态多目标优化问题中,第一幅图展示的是问题DF6的相关定义和绘制的POS和POF图,请问图中公式PS(t)和PF(t)是如何推导的
  • ¥60 设计一种优化算法结合案例给出智能仓储四向穿梭车的调度计划
  • ¥15 Errno2:No such file or directory,在当前文件确实没有该图片,怎么解决?
  • ¥15 博世摄像头数据存储的问题(iscsi)
  • ¥15 如何实现对学生籍贯信息管理系统的选择排序
  • ¥15 git clone报错
  • ¥15 3d-slicer超声造影动态图像导入报错
  • ¥15 化工过程分析与合成问题求解决