ep8_6.cpp
上传用户:wxcui2006
上传日期:2022-07-12
资源大小:1274k
文件大小:4k
- /*8.6 几何形体的派生关系如下:
-
- 对平面形体有长和面积,对立体有表面积和体积,对几何图形基类,周长、面积和体积应怎样计算
- (用什么函数)?对平面圆形体积怎样计算(用什么函数|)?对立体圆形周长怎么计算(用什么函
- 数)?要求实现运行时的多态性。请编程,并测试。*/
- #include <iostream>
- #include <cmath>
- using namespace std;
- const double PI=3.1415926535;
- class Geometric_shape{//几何图形
- public:
- virtual double perimeter()=0; //周长
- virtual double area()=0; //面积
- virtual double volume()=0; //体积
- virtual void Show(){};
- };
- class Circle :public Geometric_shape{//圆
- double radius;
- public:
- Circle(){radius = 0; }
- Circle(double vv){radius = vv;}
- double perimeter(){return 2.0*PI*radius;} //周长
- double area(){return PI*radius*radius;} //面积
- double volume(){return 0;} //体积
- void Show(){cout<<"radius="<<radius<<endl;}
- };
- class Rectangle:public Geometric_shape{//矩形
- double width,length;
- public:
- Rectangle(){width=0; length=0;}//长宽
- Rectangle(double wid,double len){
- width = wid;
- length= len;
- }
- Rectangle(Rectangle& rr){
- width = rr.width;
- length = rr.length;
- }
- double perimeter(){return 2.0*(width+length);} //周长
- double area(){return width*length;} //面积
- double volume(){return 0;} //体积
- void Show(){cout<<"width="<<width<<'t'<<"length="<<length<<endl;}
- };
- class Triangle:public Geometric_shape{//三角形
- double a,b,c;
- public:
- Triangle(){a=0;b=0;c=0;}
- Triangle(double v1,double v2,double v3){a = v1;b = v2;c = v3;}
- double perimeter(){return a+b+c;} //周长
- double area(){
- double s=(a+b+c)/2.0;
- return sqrt(s*(s-a)*(s-b)*(s-c));
- } //面积
- double volume(){return 0;} //体积
- void Show(){cout<<"a="<<a<<'t'<<"b="<<b<<'t'<<"c="<<c<<endl;}
- };
- class Box:public Rectangle{//长方体
- double height;
- public:
- Box(){height=0;}
- Box(double wid,double len,double heg):Rectangle(wid,len){height=heg;}
- double volume(){return area()*height;} //体积
- };
- class Cylinder:public Circle {//圆柱体
- double height;
- public:
- Cylinder(){height=0;}
- Cylinder(double vv,double heg):Circle(vv){height=heg;}
- double volume(){return area()*height;} //体积
- };
- class Cone: public Circle {//圆锥
- double height;
- public:
- Cone(){height=0;}
- Cone(double vv,double heg):Circle(vv){height=heg;}
- double volume(){return area()*height/3;} //体积
- };
- class T_pyramid:public Triangle{//三棱锥
- double height;
- public:
- T_pyramid(){height=0;}
- T_pyramid(double v1,double v2,double v3,double heg):Triangle(v1,v2,v3){height=heg;}
- double volume(){return area()*height/3;} //体积
- };
- class T_prism:public Triangle{//三棱柱
- double height;
- public:
- T_prism(){height=0;}
- T_prism(double v1,double v2,double v3,double heg):Triangle(v1,v2,v3){height=heg;}
- double volume(){return area()*height;} //体积
- };
- int main(){
- Geometric_shape * gs;
- Circle cc1(10);
- Rectangle rt1(6,8);
- Triangle tg1(3,4,5);
- Box bx1(6,8,3);
- Cylinder cl1(10,3);
- Cone cn1(10,3);
- T_pyramid tpy1(3,4,5,3);
- T_prism tpr1(3,4,5,3);
- cc1.Show();//静态
- cout<<"圆周长:"<<cc1.perimeter()<<'t';
- cout<<"圆面积:"<<cc1.area()<<'t';
- cout<<"圆体积:"<<cc1.volume()<<endl;
- gs=&rt1;//动态
- gs->Show();
- cout<<"矩形周长:"<<gs->perimeter()<<'t';
- cout<<"矩形面积:"<<gs->area()<<'t';
- cout<<"矩形体积:"<<gs->volume()<<endl;
- gs=&tg1;//动态
- gs->Show();
- cout<<"三角形周长:"<<gs->perimeter()<<'t';
- cout<<"三角形面积:"<<gs->area()<<'t';
- cout<<"三角形体积:"<<gs->volume()<<endl;
- gs=&bx1;//动态
- gs->Show();
- cout<<"长方体底周长:"<<gs->perimeter()<<'t';
- cout<<"长方体底面积:"<<gs->area()<<'t';
- cout<<"长方体体积:"<<gs->volume()<<endl;
- gs=&cl1;//动态
- gs->Show();
- cout<<"圆柱体底周长:"<<gs->perimeter()<<'t';
- cout<<"圆柱体底面积:"<<gs->area()<<'t';
- cout<<"圆柱体体积:"<<gs->volume()<<endl;
- gs=&cn1;//动态
- gs->Show();
- cout<<"圆锥体底周长:"<<gs->perimeter()<<'t';
- cout<<"圆锥体底面积:"<<gs->area()<<'t';
- cout<<"圆锥体体积:"<<gs->volume()<<endl;
- gs=&tpy1;//动态
- gs->Show();
- cout<<"三棱锥底周长:"<<gs->perimeter()<<'t';
- cout<<"三棱锥底面积:"<<gs->area()<<'t';
- cout<<"三棱锥体积:"<<gs->volume()<<endl;
- gs=&tpr1;//动态
- gs->Show();
- cout<<"三棱柱底周长:"<<gs->perimeter()<<'t';
- cout<<"三棱柱底面积:"<<gs->area()<<'t';
- cout<<"三棱柱体积:"<<gs->volume()<<endl;
- return 0;
- }