C++习题 继承与组合

Problem C: C++习题 继承与组合

[ Submit][ Status][ Web Board]

Description

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

Input

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

Output

 num,name,sex,year,month,day 

Sample Input

2001 Huang m1970 1 11994 5 26

Sample Output

num:2001name:Huangsex:mbirthday:1994/5/26

HINT



#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);
     
}
 
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;
}


  • 3
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为你提供一个简单的继承多态的练习题,希望能帮助你巩固相关知识。 假设有一个图形 Shape,其中包含一个纯虚函数 getArea(),用于计算该图形的面积。现在要定义两个子,分别是矩形 Rectangle 和圆形 Circle。请完成以下任务: 1. 定义 Shape : ```c++ class Shape { public: virtual double getArea() = 0; // 纯虚函数 }; ``` 2. 定义 Rectangle 继承自 Shape ,包含两个成员变量 width 和 height,以及实现 getArea() 函数的具体方法: ```c++ class Rectangle : public Shape { private: double width; double height; public: Rectangle(double w, double h) { width = w; height = h; } double getArea() { return width * height; } }; ``` 3. 定义 Circle ,同样继承自 Shape ,包含一个成员变量 radius,以及实现 getArea() 函数的具体方法: ```c++ class Circle : public Shape { private: double radius; public: Circle(double r) { radius = r; } double getArea() { return 3.1415926 * radius * radius; } }; ``` 4. 在主函数中创建一个 Shape 指针数组,分别指向一个 Rectangle 和一个 Circle 对象,并计算它们的面积: ```c++ int main() { Shape* shapes[2]; shapes[0] = new Rectangle(4, 5); shapes[1] = new Circle(3); for (int i = 0; i < 2; i++) { cout << "Area of shape " << i << ": " << shapes[i]->getArea() << endl; delete shapes[i]; } return 0; } ``` 以上就是一个简单的继承多态的练习题,希望能够帮助你巩固相关知识。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值