题目:声明教授类(professor)是教师类(teacher)的派生类,另有一个类birthday包含month,day,year数据成员,要求在程序中使用
继承与组合,在定义professor类对象时给定全部初值,并输出,然后修改对象的生日,再输出。
#include
#include
using namespace std;
class birthday
{
public:
birthday(int m=0,int d=0,int y=0):month(m),day(d),year(y){}
void set()
{
cin>>month>>day>>year;
}
void show()
{
cout< }
protected:
int month,day,year;
};
class teacher
{
public:
teacher(string n,char s,int a):name(n),***(s),age(a){}
protected:
string name;
char ***;
int age;
};
class professor:public teacher
{
public:
professor(string n,char s,int a,birthday b):teacher(n,s,a),bir(b){} //构造函数
void change_birthday()
{
cout<<"Please input the new date(month day year):"< bir.set();
}
void show()
{
cout<<"name *** age birthday"< cout< bir.show();
}
private:
birthday bir;
};
int main()
{
professor pr("wangjia",'F',35,birthday(9,26,1972)); //对象初始化
pr.show();
pr.change_birthday();
pr.show();
return 0;
}
运行结果:
name *** age birthday
wangjia F 35 9/26/1972
Please input the new date(month day year):
12 20 1973
name *** age birthday
wangjia F 35 12/20/1973
Press any key to continue...
阅读(3077) | 评论(0) | 转发(0) |