/*
* Copyright (c)2013, 烟台大学计算机学院学生
* All rightsreserved.
* 文件名称: array.cpp
* 作 者: 李中意
* 完成日期:2014 年6 月 11 日
* 版本号: v1.0
* 输入描述:无
*/
#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;
};
//主函数已给定如下,提交时不需要包含,会自动添加到程序尾部
BirthDate::BirthDate(int y,int m,int d):year(y),month(m),day(d){}
void BirthDate::display()
{
cout<<"birthday:"<<year<<"/"<<month<<"/"<<day<<endl;
}
void BirthDate::setbirthday(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
Teacher::Teacher(int n,string nam,char s):num(n),name(nam),sex(s){}
void Teacher::display()
{
cout<<"num:"<<num<<endl;
cout<<"name:"<<name<<endl;
cout<<"sex:"<<sex<<endl;
}
Professor::Professor(int n,string nam,char s,BirthDate Birthday):Teacher(n,nam,s),birthday(Birthday){}
void Professor::display()
{
Teacher::display();
birthday.display();
}
void Professor::setbirthday(int y,int m,int d)
{
birthday.setbirthday(y,m,d);
}
/* C++代码 */
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;
}
OJ2476继承与转化
最新推荐文章于 2022-03-08 16:32:09 发布