<p>/*
* 程序的版权和版本声明部分:
* Copyright (c) 2013, 烟台大学计算机学院
* All rights reserved.
* 文件名称:test.cpp
* 作 者:任子仪
* 完成日期:2014年 5月 28日
* 版 本 号:v12.1
* 输入描述:无
* 问题描述:。
* 程序输出:
* 问题分析:略
* 算法设计:略
*/</p><p>#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 birth)
:Teacher(n,nam,s),birthday(birth){}
void Professor::display()
{
Teacher::display();
birthday.display();
}
void Professor::setbirthday(int y,int m,int d)
{
birthday.setbirthday(y,m,d);
}</p><p>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;
}
</p>
示例图片:
心得体会: Teacher::display(); 牢牢记住了,坑死了。花了好长时间都没有弄出来,最后还是张玉老师帮的忙啊