本文主要是介绍[YTU]_2476(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
样例输入
2001 Huang m
1970 1 1
1994 5 26
样例输出
num:2001 name:Huang sex:m birthday:1994/5/26#include <iostream> using namespace std; class Rectangle //矩形类 { private:int length; //矩形的长和宽int width; public:Rectangle();Rectangle(int l,int w); //构造函数,l、w分别代表长和宽int getArea(); //求面积 };class Bulk: public Rectangle //立方体类 { public:Bulk(); //默认构造函数Bulk(int l, int w,int h); //带参数的构造函数int getVolume(); private:int height; //高,长、宽继承自基类 };Rectangle::Rectangle(){} Rectangle::Rectangle(int l,int w):length(l),width(w){} int Rectangle::getArea() {return length*width; } Bulk::Bulk(){} Bulk::Bulk(int l,int w,int h):Rectangle(l,w),height(h){} int Bulk::getVolume() { K #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::setbirthday(int y,int m,int d) {year=y;month=m;day=d; } void BirthDate::display() {cout<<"birthday:"<<year<<'/'<<month<<'/'<<day<<endl; } Teacher::Teacher(int n,string nam,char s):num(n),name(nam),sex(s){} void Teacher::display() {cout<<"num:"<<num<<endl<<"name:"<<name<<endl<<"sex:"<<sex<<endl; } Professor::Professor(int n,string nam,char s,BirthDate BIR):Teacher(n,nam,s),birthday(BIR){} void Professor::setbirthday(int y,int m,int d) {birthday.BirthDate::setbirthday(y,m,d); } void Professor::display() {Teacher::display();birthday.BirthDate::display(); } 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; }
这篇关于[YTU]_2476(C++习题 继承与组合)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!