YTU OJ 2476 Problem B C++习题 继承与组合

问题 B: C++习题 继承与组合

题目描述

已知类如下:
(1) BirthDate(生日类) 含有:year,month,day 等数据成员
(2) Teacher(教师类)含有:num,name,sex 等数据成员
(3) Professor(教授类)含有:教师类和生日类的数据成员
要求:
(1)通过对Teacher和BirthDate使用继承和组合的方式设计Professor
(2)定义Professor类对象prof,并给出所有数据的初值
(3)修改prof的生日数据
(4)输出prof的全部最新数据

输入
num,name,sex,year,month,day 和修改后的year,month,day

输出
num,name,sex,year,month,day

输入输出样例

样例输入 #1
2001 Huang m
1970 1 1
1994 5 26

样例输出 #1
num:2001
name:Huang
sex:m
birthday:1994/5/26

提示

 前置代码及类型定义已给定如下,提交时不需要包含,会自动添加到程序前部
/* C++代码 */
#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 c)
{
	num = n;name = nam;sex = c;
}
void Teacher::display(){
	cout << "num:" << num << endl;
	cout << "name:" << name << endl;
	cout << "sex:" << sex << endl;
}
Professor::Professor(int n,string nam,char c,BirthDate day):Teacher(n,nam,c),birthday(day){}

void Professor::display()
{
	Teacher::display();
	birthday.display();
}
void Professor::setbirthday(int i, int m, int n)
{
	birthday.setbirthday(i,m,n);
}
主函数已给定如下,提交时不需要包含,会自动添加到程序尾部
/* 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;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

三元湖有大锦鲤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值