c++习题继承与组合

将本章5.8节中的程序片段加以补充完善,成为一个完整的程序。在程序中使用继承和组合。在定义Professor类对象Prof1时给出所有数据的初值,然后修改prof1的生日数据,最后输出prof1的全部最新数据。

class Teacher 
{
	public:
	...
	private:
		int num;
		string name;
		char sex;
};

class Birthday
{
	public:
	...
	private:
		int year;
		int month;
		int day;
};

class Professor:public:Teacher
{
	public:
	...
	private:
		Birthday birth;
};

输入

先输入原始数据
num
name
sex
birthday year
birthday month
birthday date
area
然后输入修改的新
birthday year
birthday month
birthday date

输出

分别输出
original data:

new data:
格式参考样例

样例输入

3012
Zhang
f
1949
10
1
125.4
1950
6
1

样例输出

original data:
num:3012
name:Zhang
sex:f
birthday:10/1/1949
area:125.4
new data:
num:3012
name:Zhang
sex:f
birthday:6/1/1950
area:125.4

代码

#include<iostream>
#include<string>
using namespace std;
class Teacher
{
 public:
  Teacher(int n,string nam,string s){num=n;name=nam;sex=s;
  }
  void display(){
   cout<<"num:"<<num<<endl;
   cout<<"name:"<<name<<endl;
   cout<<"sex:"<<sex<<endl;
  }
  protected:
  int num;
  string name;
  string sex;
};
class Birthday
{
 protected:
  int year;
  int month;
  int day;
 public:
  Birthday(int y,int m,int d){year=y;month=m;day=d;
  } 
  void show(){
   cout<<"birthday:"<<month<<"/"<<day<<"/"<<year<<endl;
  }
  void show1(int y,int m,int d){
   year=y;
   month=m;
   day=d;
  }
};
class Professor:public Teacher
{
 public:
  Birthday birth;
  float area;
  Professor(int n,string nam,string s,int y,int m,int d,float a):Teacher(n,nam,s),birth(y,m,d){area=a;
  }
  void fun(){
   Teacher::display();
   birth.show();
   cout<<"area:"<<area<<endl; 
  } 
  void change(int y,int m,int d){
   Teacher::display();
   birth.show1(y,m,d);
   birth.show();
   cout<<"area:"<<area<<endl;
  }
};
int main()
{
 int a,y,m,d,y1,m1,d1;string b,c;
 cin>>m1>>d1>>y1;
 float ar;
 cin>>a>>b>>c>>y>>m>>d>>ar;
 Professor p1(a,b,c,y,m,d,ar);
 cout<<"original date:"<<endl;
 p1.fun();
 cout<<"new date:"<<endl;
 p1.change(m1,d1,y1);
 return 0;  
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值