Jurous
Jurous
采纳率63.6%
2020-06-02 15:05

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

已采纳

对于组合问题不是很懂
继承和组合的关系:声明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条回答

  • caozhy 回答这么多问题就耍赖把我的积分一笔勾销了 10月前
    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();
    }
    
    点赞 评论 复制链接分享