面向对象实验三(组合、继承与多态性)

一、实验目的
1、掌握继承机制。
2、掌握虚函数。
3、理解并掌握虚基类。

二、实验内容
1、编写一个程序:设计一个汽车类,数据成员有*个数、车重。小车类是汽车类的私有派生类,包含载客量。卡车类是汽车类的私有派生类,包含载客数和载重量。每个类都有数据的输出方法。

 1 #include<iostream>
 2 
 3 using namespace std;
 4 
 5 //汽车类
 6 class car
 7 {
 8 public:
 9     int wheel;//*个数
10     float weight;//车重
11     car(int a=0,float b=0);
12     void carprint();//汽车类的输出函数
13 };
14 
15 //小车类
16 class dolly:private car
17 {
18 private:
19     int busload;//载客量
20 public:
21     dolly(int c=24):car(4,2)
22     {
23         busload=c;
24     }
25     void dollyprint();//小车类的输出函数
26 };
27 
28 //卡车类
29 class truck:private car
30 {
31 private:
32     int busload;//载客量
33     float upweight;//载重量
34 public:
35     truck(int c=24,float d=30):car(4,2)
36     {
37         busload=c;
38         upweight=30;
39     }
40     void truckprint();//卡车类的输出函数
41 };
42 
43 int main()
44 {
45     car c1(4,2);
46     c1.carprint();
47     dolly c2(24);
48     c2.dollyprint();
49     truck c3(24,3);
50     c3.truckprint();
51     return 0;
52 }
53 
54 //汽车类的构造函数
55 car::car(int a,float b)
56 {
57     wheel=a;
58     weight=b;
59 }
60 
61 //汽车类的输出函数
62 void car::carprint()
63 {
64     cout<<"汽车类:"<<endl;
65     cout<<"*个数为:"<<wheel<<""<<endl;
66     cout<<"车重为:"<<weight<<""<<endl;
67     cout<<endl<<endl;
68 }
69 
70 //小车类的输出函数
71 void dolly::dollyprint()
72 {
73     cout<<"小车类:"<<endl;
74     cout<<"*个数为:"<<wheel<<""<<endl;
75     cout<<"车重为:"<<weight<<""<<endl;
76     cout<<"载客量为:"<<busload<<""<<endl;
77     cout<<endl<<endl;
78 }
79 
80 //卡车类的输出函数
81 void truck::truckprint()
82 {
83     cout<<"卡车类:"<<endl;
84     cout<<"*个数为:"<<wheel<<""<<endl;
85     cout<<"车重为:"<<weight<<""<<endl;
86     cout<<"载客量为:"<<busload<<""<<endl;
87     cout<<"载重量为:"<<busload<<""<<endl;
88 }

 

2、虚基类为Shape,从Shape派生出矩形类(左上角点、宽、高)、椭圆类(横轴、纵轴)。给出各个类的构造函数,成员初始化,在基类中定义虚函数GetArea()(计算面积),在派生类中改写。写出该程序的实现。

 1 #include<iostream>
 2 #define PI 3.14
 3 
 4 using namespace std;
 5 
 6 //形状类
 7 class Shape
 8 {
 9 public:
10     int x,y;
11     int wide,high;
12     Shape(){};
13     virtual void GetArea()=0;//虚函数GetArea()(计算面积)
14     virtual ~Shape(){};
15 };
16 
17 //矩形类
18 class rectangle:public Shape
19 {
20 private:
21     int x,y;
22     int wide,high;
23 public:
24     rectangle(int a,int b,int c,int d);//矩形类的构造函数
25     void GetArea();
26 };
27 
28 //椭圆类
29 class ellipse:public Shape
30 {
31 private:
32     int x,y;
33     int wide,high;
34 public:
35     ellipse(int a,int b,int c,int d);//椭圆类的构造函数
36     void GetArea();
37 };
38 
39 int main()
40 {
41     rectangle ob1(3,4,6,8);
42     ob1.GetArea();
43     ellipse ob2(0,0,2,2);
44     ob2.GetArea();
45     return 0;
46 }
47 
48 //矩形类的构造函数
49 rectangle::rectangle(int a,int b,int c,int d)
50 {
51     x=a;
52     y=b;
53     wide=c;
54     high=d;
55 }
56 
57 //椭圆类的构造函数
58 ellipse::ellipse(int a,int b,int c,int d)
59 {
60     x=a;
61     y=b;
62     wide=c;
63     high=d;
64 }
65 
66 void rectangle::GetArea()
67 {
68     cout<<"矩形类"<<endl;
69     cout<<"左上角点的坐标为:"<<'('<<x<<','<<y<<')'<<endl;
70     cout<<"矩形的宽为:"<<wide<<endl;
71     cout<<"矩形的高为:"<<high<<endl;
72     cout<<"矩形的面积为:"<<wide*high<<endl<<endl;
73 }
74 
75 void ellipse::GetArea()
76 {
77     float area;
78     area=PI*wide*high/4;
79     cout<<"椭圆类"<<endl;
80     cout<<"椭圆的横轴为:"<<wide<<endl;
81     cout<<"椭圆的长轴为:"<<high<<endl;
82     cout<<"椭圆的面积为:"<<area<<endl;
83 }