Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1320826
  • 博文数量: 177
  • 博客积分: 3640
  • 博客等级: 中校
  • 技术积分: 1778
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-27 16:51
文章分类

全部博文(177)

文章存档

2014年(1)

2013年(10)

2012年(3)

2011年(163)

分类: C/C++

2011-05-29 19:41:27

题目:声明教授类(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...
阅读(3027) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~