fhh517 于 2014-12-03 13:49 发布
#include <iostream.h>
const double PI = 3.14;
class Shape
{ public:
virtual double area()=0;
virtual double surface_area()=0;
virtual double volume()=0;
};
class Circle:virtual public Shape
{
protected:
double r;
public:
Circle(double x)
{ r=x;}
double area()
{ return PI*r*r; }
};
class Ball:virtual public Circle
{
public:
Ball(double x):Circle(x){}
double surface_area()
{ return 4*PI*r*r; }
double volume()
{ return (4/3)*PI*r*r*r;}
};
class Taper:virtual public Circle
{
protected:
double l;
public:
Taper(double x,double y):Circle(x)
{l=y;}
double surface_area()
{ return PI*r*l+PI*r*r; }
double volume()
{ return (1/3)*PI*r*r*l;}
};
class Cylinder:virtual public Circle
{
protected:
double h;
public:
Taper(double x,double y):Circle(x)
{h=y;}
double surface_area()
{ return 2*PI*r*h+2*PI*r*r; }
double volume()
{ return PI*r*r*h;}
};
int main ( )
{ Circle c(1.1);
Ball b(2.2);
Taper t(3.3,4.4);
Cylinder m(5.5,6.6);
Shape *p;
p=&c;
cout<<"the area of Circle is:"<<p->area()<<endl;
p=&b;
cout<<"the surface_area of Ball:"<<p->surface_area()<<endl;
cout<<"the Volume of Ball:"<<p->Volume()<<endl;
p=&t;
cout<<"the surface_area of Taper is:"<<p->surface_area()<<endl;
cout<<"the Volume of Taper:"<<p->Volume()<<endl;
p=&m;
cout<<"the surface_area of Cylinder is:"<<p->surface_area()<<endl;
cout<<"the Volume of Cylinder:"<<p->Volume()<<endl;
}
这个也是求解虚函数和纯虚函数下的面积和体积,为什么调不出来,谁能修改一下给出正确的程序!谢谢!