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

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

对于组合问题不是很懂
继承和组合的关系:声明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 0基础学机器人运动控制要多久?
  • ¥15 .net core 怎么进行中英文转换
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波