/* *程序的版权和版本声明部分: *Copyright(c)2014,烟台大学计算机学院学生 *All rights reserved. *文件名称: *作者:田成琳 *完成日期:2014 年 5 月 26 日 *版本号:v1.0 *对任务及求解方法的描述部分: *输入描述:num,name,sex,year,month,day 和修改后的year,month,day *问题描述:已知类如下: (1) BirthDate(生日类) 含有:year,month,day 等数据成员 (2) Teacher(教师类)含有:num,name,sex 等数据成员 (3) Professor(教授类)含有:教师类和生日类的数据成员 要求: (1)通过对Teacher和BirthDate使用继承和组合的方式设计Professor (2)定义Professor类对象prof,并给出所有数据的初值 (3)修改prof的生日数据 (4)输出prof的全部最新数据 *程序输出: Sample Input 2001 Huang m 1970 1 1 1994 5 26 Sample Output num:2001 name:Huang sex:m birthday:1994/5/26 *问题分析: *算法设计: */ #include <iostream> #include <string> using namespace std; class BirthDate { public: BirthDate(int,int,int); void display(); void setbirthday(int,int,int); private: int year; int month; int day; }; class Teacher { public: Teacher(int,string,char); void display(); private: int num; string name; char sex; }; class Professor:public Teacher { public: Professor(int,string,char,BirthDate); void display(); void setbirthday(int,int,int); private: BirthDate birthday; }; int main() { int num; string name; char sex; int year,month,day; cin>>num>>name>>sex; cin>>year>>month>>day; Professor prof(num,name,sex,BirthDate(year,month,day)); cin>>year>>month>>day; prof.setbirthday(year,month,day); prof.display(); return 0; } BirthDate::BirthDate(int y,int m,int d) { year=y; month=m; day=d; } void BirthDate::display() { cout<<"birthday:"<<year<<"/"<<month<<"/"<<day; } void BirthDate::setbirthday(int y,int m,int d) { year=y; month=m; day=d; } Teacher::Teacher(int nu,string n,char s) { num=nu; name=n; sex=s; } void Teacher::display() { cout<<"num:"<<num<<endl; cout<<"name:"<<name<<endl; cout<<"sex:"<<sex<<endl; } Professor::Professor(int nu,string n,char s,BirthDate m):Teacher(nu,n,s),birthday(m){} void Professor::setbirthday(int y,int m,int d) { birthday.setbirthday(y,m,d);//由对象调用public函数 } void Professor::display() { Teacher::display(); birthday.display();//由对象调用public函数 }
运行结果:
心得体会:。