- /**********************************************************/
- //Function : main,Geometric及其派生类
- //parm :
- //comment :
- //return : void
- //Author :
- //date : 2013.1.4
- /**********************************************************/
- #include <iostream.h>
- #include <math.h>
- const double PI=3.14159;
- class Geometric{
- public:
- Geometric()
- {}
- virtual double Get_Girth()=0; //周长
- virtual double Get_Area()=0; //面积
- virtual double Get_Volume()=0; //体积
- virtual void print()=0; //输出
- };
- class Rectangle:public Geometric{
- protected:
- double length,width;
- public:
- Rectangle(double l,double w):Geometric(),length(l),width(w){}
- double Get_Girth()
- {
- return 2 * (width+length);
- }
- double Get_Area()
- {
- return width * length;
- }
- double Get_Volume()
- {
- return 0.0;
- }
- void print()
- {
- cout<<"The Rectangle's Girth is:"<<Get_Girth()<<endl;
- cout<<"The Rectangle's Area is:"<<Get_Area()<<endl;
- }
- };
- class Circle:public Geometric{
- protected:
- double radius;
- public:
- Circle(double r):Geometric()
- {
- radius = r;
- }
- double Get_Girth()
- {
- return 2 * PI * radius;
- }
- double Get_Area()
- {
- return PI * radius * radius;
- }
- double Get_Volume()
- {
- return 0.0;
- }
- void print()
- {
- cout<<"The Circle's Girth is:"<<Get_Girth()<<endl;
- cout<<"The Circle's Area is:"<<Get_Area()<<endl;
- }
- };
- class Ball:public Circle{
- public:
- Ball(double r):Circle(r)
- {}
- double Get_Girth()
- {
- return 0;
- }
- double Get_Area()
- {
- return 4 * Circle::Get_Area();
- }
- virtual double Get_Volume()
- {
- return 4.0 / 3 * Get_Area() * radius;
- }
- void print()
- {
- cout<<"The Ball's Area is:"<<Get_Area()<<endl;
- cout<<"The Ball's volume is:"<<Get_Volume()<<endl;
- }
- };
- class Column:public Circle{
- protected:
- double height;
- public:
- Column(double r,double h):Circle(r),height(h){}
- double Get_Area()
- {
- return Circle::Get_Girth() * height + 2 * Circle::Get_Area();
- }
- double Get_Volume()
- {
- return Circle::Get_Area() * height;
- }
- void print()
- {
- cout<<"The Column's Area is:"<<Get_Area()<<endl;
- cout<<"The Column's volume is:"<<Get_Volume()<<endl;
- }
- };
- void fun(Geometric &p)
- {
- p.print();
- }
- void main()
- {
- double r,h,w,l;
- cout<<"Please input the radius and height:"<<endl;
- cin>>r>>h;
- Circle obj_Cir(r);
- fun(obj_Cir);
- Ball obj_Bal(r);
- fun(obj_Bal);
- Column obj_Col(r,h);
- fun(obj_Col);
- cout<<"Please input the length and width:"<<endl;
- cin>>l>>w;
- Rectangle obj_Rec(l,w);
- fun(obj_Rec);
- }
第五次实验代码(其他班)——继承多态
最新推荐文章于 2024-07-13 19:27:27 发布