版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/navicheung/article/details/82796062
C++程序设计(第三版) 谭浩强 习题11.10 个人设计
习题 11.10 将本章11.8节中的程序片段加以补充完善,成为一个完整的程序。在程序中使用继承和组合。在定义Professor类对象prof1时给出所有数据的初值,然后修改prof1的生日数据,最后输出prof1的全部最新数据。
代码块:
#include <iostream>
#include <string>
using namespace std;
class Teacher
{
public:
Teacher(){}
Teacher(int n, string nam, char s){num=n; name=nam; sex=s;}
void display(){
cout<<"num: "<<num<<endl;
cout<<"name: "<<name<<endl;
cout<<"sex: "<<sex<<endl;
}
protected:
int num;
string name;
char sex;
};
class BirthDate
{
public:
BirthDate(){}
BirthDate(int y, int m, int d){year=y; month=m; day=d;}
void display1(){
cout<<"birthdate: "<<year<<"/"<<month<<"/"<<day<<endl;
}
private:
int year;
int month;
int day;
};
class Professor: public Teacher
{
public:
Professor(int n, string nam, char s, BirthDate bd){
num=n; name=nam; sex=s; birthday=bd;}
void show(){
display();
birthday.display1();
}
private:
BirthDate birthday;
};
int main()
{
BirthDate bd(1963, 12, 3);
Professor prof1(101, "ZhangLei", 'M', bd);
prof1.show();
system("pause");
return 0;
}