对于组合问题不是很懂
继承和组合的关系:声明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;
};
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();
}