下列shape类是一个表示形状的抽象类,area( )为求图形面积的函数,total( )则是一个通用的用以求不同形状 15
请从shape类派生三角形类(triangle)、矩形类(rectangle)、并给出具体的求面积函数。...
请从shape类派生三角形类(triangle)、矩形类(rectangle)、并给出具体的求面积函数。
展开
1个回答
展开全部
main函数你自己写个吧 原创 有什么问题可以给我留言
#include <iostream>
2 using namespace std;
3 //通用类型定义用枚举实现比较好
4 enum which_shape{triangle,rectangle};
5 class shape
6 {
7 public:
8 virtual area();
9 shape(which_shape type);
10 virtual ~shape();
11 virtual void print_area();
12 private:
13 int shape_type;
14 };
15 class triangle :public shape
16 {
17 public:
18 virtual triangle(int , int)
19 virtual int area()const
20 {
21 tri_area = bottom * height / 2;
22 return tri_area;
23 }
24 virtual void print_area()const
25 {
26 cout<<"the area of triangle is:\n"<<tri_area<<endl;
27 }
28 private:
29 int bottom;
30 int height;
31 int tri_area;
32 };
33 class rectangle:public shape
34 {
35 public:
36 rectangle(int , int);
37 virtual int area()
38 {
39 rec_area = length * width;
40 return rec_area;
41 }
42 virtual void print_area ()const
43 {
44 cout<<"the area of rectangle is:\n"<<rec_area<<endl;
45 }
46 private:
47 int length;
48 int width;
49 int rec_area;
50 };
51 shape::shape(which_type shape)
52 {
53 shape_type = shape;
54 }
55 triangle::triangle(int bot, int hei):(bottom)bot, (height)hei{}
56 rectangle::rectangle(int len, int wid)
57 {
length = len;
59 width = wid;
60 }
#include <iostream>
2 using namespace std;
3 //通用类型定义用枚举实现比较好
4 enum which_shape{triangle,rectangle};
5 class shape
6 {
7 public:
8 virtual area();
9 shape(which_shape type);
10 virtual ~shape();
11 virtual void print_area();
12 private:
13 int shape_type;
14 };
15 class triangle :public shape
16 {
17 public:
18 virtual triangle(int , int)
19 virtual int area()const
20 {
21 tri_area = bottom * height / 2;
22 return tri_area;
23 }
24 virtual void print_area()const
25 {
26 cout<<"the area of triangle is:\n"<<tri_area<<endl;
27 }
28 private:
29 int bottom;
30 int height;
31 int tri_area;
32 };
33 class rectangle:public shape
34 {
35 public:
36 rectangle(int , int);
37 virtual int area()
38 {
39 rec_area = length * width;
40 return rec_area;
41 }
42 virtual void print_area ()const
43 {
44 cout<<"the area of rectangle is:\n"<<rec_area<<endl;
45 }
46 private:
47 int length;
48 int width;
49 int rec_area;
50 };
51 shape::shape(which_type shape)
52 {
53 shape_type = shape;
54 }
55 triangle::triangle(int bot, int hei):(bottom)bot, (height)hei{}
56 rectangle::rectangle(int len, int wid)
57 {
length = len;
59 width = wid;
60 }
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询