Jurous 2020-06-02 15:05 采纳率: 0%
浏览 363
已采纳

派生类继承和组合问题,有大佬帮忙解决一下吗

对于组合问题不是很懂
继承和组合的关系:声明Professor(教授)类是Teacher(教师)类的派生类(继承关系),另有一个类BirthDate(生日)类,Professor类有一个生日数据(组合关系)。

在定义Professor类对象prof1时给出所有数据的初值,然后修改prof1的生日数据,最后输出prof1的全部最新数据,其余成员随意设计

class Teacher

{

public:

pirvate:

int num;
string name;
char sex;

};

class BirthDate

{

public:

private:

int year;
int month;
int day;

};

class Professor:public Teacher

{

public:

private:

BirthDate birthday;

};

  • 写回答

1条回答 默认 最新

  • threenewbee 2020-06-02 15:34
    关注
    class Teacher
    {
    pirvate:
    int num;
    string name;
    char sex;
    public:
    void set(int n, string na, char s) { num = n; name = na; sex = s; }
    void show() { cout << num << name << sex; }
    };
    class BirthDate
    {
    private:
    int year;
    int month;
    int day;
    public:
    void set(int y, int m, int d) { year = y; month = m; day = d; }
    void show() { cout << year << month << day; }
    };
    class Professor:public Teacher
    {
    public:
    void setbd(int y, int m, int d) { birthday.set(y,m,d); }
    void show() { Teacher::show(); birthday.show(); }
    private:
    BirthDate birthday;
    };
    int main()
    {
    Professor prof1;
    prof1.set(1, "p1", 1);
    prof1.serbd(1990,1,1);
    prof1.show();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办