所在的位置: C++ >> C++优势 >> c练习题组合继承多态

c练习题组合继承多态

1.编写一个学生和教师数据的输入和显示程序,学生数据有编号、姓名、班号和成绩,教师数据有编号、姓名、职称和部门。要求将编号、姓名的输入和显示设计成一个类person,并作为学生数据操作类student和教师数据操作类teacher的基类。

2.建立一个基类building,含有保护成员floors、rooms和square,分别用来表示一座楼房的层数、房间数以及它的总面积。建立类building的派生类house,含有私有成员bedrooms,balcony,分别用来表示卧室与阳台的数量。另外,建立类building的派生类office,含有私有成员phones和meeting_rooms,分别用来表示电话与会议室的数目。这两个派生类都含有构造函数和show()函数,用于对数据成员进行初始化和显示出这些数据。

1.程序代码:

#includeiostream

usingnamespacestd;

classperson

{

  private:

    stringnum;

    stringname;

  public:

    person(stringn,stringm);

    voidshow();

};

person::person(stringn,stringm)

{

  num=n;name=m;

}

voidperson::show()

{

  coutnum"-"nameendl;

}

classstudent

{

  private:

    stringclass0;

    stringgrades;

  public:

    student(stringn,stringm,stringclass1,stringg);

    voidshow();

    personp;  

};

student::student(stringn,stringm,stringclass1,stringg):p(n,m){

  class0=class1;grades=g;

}

voidstudent::show()

{

  p.show();

  coutclass0"-"gradesendl;

}

classteacher

{

  private:

    stringtitle;

    stringdepartment;

  public:

    teacher(stringn,stringm,stringt,stringd);

    voidshow();

    personp;

};

teacher::teacher(stringn,stringm,stringt,stringd):p(n,m)

{

  title=t;department=d;

}

voidteacher::show()

{

  p.show();

  couttitle"-"departmentendl;

}

intmain()

{

  cout"学生"endl;

  studentstu1("","shushu","","");

  stu1.show();

  cout"教师"endl;

  teachertea1("","zs","


转载请注明:http://www.aierlanlan.com/grrz/7571.html

  • 上一篇文章:
  •   
  • 下一篇文章: 没有了