首页 > 编程语言 > 详细

C++练习题(继承与派生)

时间:2021-01-30 18:00:29      阅读:33      评论:0      收藏:0      [点我收藏+]

   定义一个汽车类Automobile,包含数据成员品牌、颜色、车重、马力;定义小客车类Car继承Automobile,增加数据成员座位数;定义小货车类Wagon继承Automobile,增加数据成员载重量;定义客货两用车类StationWagon继承CarWagon。分析类之间的关系,完成各类的设计和定义,并在各类中提供构造函数、输入和输出信息函数。

 

  1 #include<iostream>
  2 #include<string>
  3 using namespace std;
  4 class Automobile
  5 {
  6 protected:
  7     string brand;
  8     string color;
  9     double weight;
 10     double power;
 11 public:
 12     Automobile(string br=" ",string co=" ",double we=0,double po=0):brand(br),color(co),weight(we),power(po)
 13     {
 14         cout<<"Automobile con!"<<endl;
 15     }
 16     void Input();
 17     void Show();
 18 };
 19 void Automobile::Input()
 20 {
 21     cout<<"输入品牌:"; cin>>brand;
 22     cout<<"输入颜色:"; cin>>color;
 23     cout<<"输入车重:"; cin>>weight;
 24     cout<<"输入马力:"; cin>>power;
 25 }
 26 void Automobile::Show()
 27 {
 28     cout<<"品牌:"<<brand<<endl;
 29     cout<<"颜色:"<<color<<endl;
 30     cout<<"车重:"<<weight<<endl;
 31     cout<<"马力:"<<power<<endl;
 32 }
 33 class Car:virtual public Automobile
 34 {
 35 protected:
 36     int seats;
 37 public:
 38     Car(string br=" ",string co=" ",double we=0,double po=0,int ss=5):Automobile(br,co,we,po),seats(ss)
 39     {
 40         cout<<"Car con!"<<endl;
 41     }
 42     void Input();
 43     void Input_Car();
 44     void Show();
 45     void Show_Car();
 46 };
 47 void Car::Input_Car()
 48 {
 49     cout<<"输入座位数:"; cin>>seats;
 50 }
 51 void Car::Input()
 52 {
 53     Automobile::Input();
 54     Input_Car();
 55 }
 56 void Car::Show_Car()
 57 {
 58     cout<<"座位数:"<<seats<<endl;
 59 }
 60 void Car::Show()
 61 {
 62     Automobile::Show();
 63     Show_Car();
 64 }
 65 
 66 class Wagon:virtual public Automobile
 67 {
 68 protected:
 69     int load;
 70 public:
 71     Wagon(string br=" ",string co=" ",double we=0,double po=0,int lo=11):Automobile(br,co,we,po),load(lo)
 72     {
 73         cout<<"Wagon con!"<<endl;
 74     }
 75     void Input();
 76     void Input_Wagon();
 77     void Show();
 78     void Show_Wagon();
 79 };
 80 void Wagon::Input()
 81 {
 82     Automobile::Input();
 83     Input_Wagon();
 84 }
 85 void Wagon::Input_Wagon()
 86 {
 87     cout<<"输入载重量:";cin>>load;
 88 }
 89 void Wagon::Show()
 90 {
 91     Automobile::Show();
 92     Show_Wagon();
 93 }
 94 void Wagon::Show_Wagon()
 95 {
 96     cout<<"载重量:"<<load<<endl;
 97 }
 98 
 99 class StationWagon:public Car,public Wagon
100 {
101 public:
102     StationWagon(string br=" ",string co=" ",double we=0,double po=0,int ss=5,int lo=11)
103         :Automobile(br,co,we,po),Car(br,co,we,po,ss),Wagon(br,co,we,po,lo)
104     {
105         cout<<"StationWagon con!"<<endl;
106     }
107     void Input();
108     void Show();
109 };
110 void StationWagon::Input()
111 {
112     Automobile::Input();
113     Input_Car();
114     Input_Wagon();
115 }
116 void StationWagon:: Show()
117 {
118     Automobile::Show();
119     Show_Car();
120     Show_Wagon();
121 }
122 int main()
123 {
124     Car c1;
125     cout<<"请输入汽车信息:"<<endl;
126     c1.Input ();
127     cout<<"该汽车的信息:"<<endl;
128     c1.Show ();
129     cout<<endl;
130 
131     Wagon w1;
132     cout<<"请输入货车信息:"<<endl;
133     w1.Input ();
134     cout<<"该货车的信息:"<<endl;
135     w1.Show ();
136     cout<<endl;
137 
138     StationWagon sw1;
139     cout<<"请输入客货两用车的信息:"<<endl;
140     sw1.Input ();
141     cout<<"该客货两用车的信息:"<<endl;
142     sw1.Show ();
143     cout<<endl;
144     return 0;
145 }

 

C++练习题(继承与派生)

原文:https://www.cnblogs.com/dazhuzi/p/14349329.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!