#include
#include
#include
using namespace std;
#define PI 3.1415
class circle
{
protected:
double R;
public:
circle(){R=0;}
circle(double r){R=r;}
virtual void display(){cout<<"請輸入半徑: "; cin>>R;}
};
class sphere:public circle
{
private:
double v1;
double s1;
public:
sphere(){}//球
sphere(double r):circle(r){}
void display(double R)
{
s1=4*PI*R*R;
v1=(PI*R*R*R*4)/3 ;
cout<<"球的表面積為:"<<s1<<endl<<"球的體積為:"<<v1<<endl;
}
};
class cylinder:public circle //圓柱
{
private:
double s2;
double v2;
double h;
public:
cylinder(){}
cylinder(double r,double H):circle(r){h=H;}
void display(double R)
{
cout<<"請輸入圓柱的高:";
cin>>h;
s2=2*PI*R*R+2*PI*R*h;
v2=PI*R*R*h;
cout<<"圓柱的表面積為:"<<s2<<endl<<"圓柱的體積為:"<<v2<<endl;
}
};
class cone:public circle //圓錐
{
private:
double s3;
double v3;
double h;
public:
cone(){}
cone(double r,double H):circle(r){h=H;}
void display(double R)
{
cout<<"請輸入圓錐的高:";
cin>>h;
s3=PI*R*R*sqrt(R*R+h*h)+PI*R*R;
v3=(PI*R*R*h)/3;
cout<<"圓錐的表面積為:"<<s3<<endl<<"圓錐的體積為:"<<v3<<endl;
}
};
void main()
{
cout<<"請選擇:"<
int l;
double R;
cin>>l;
circle pa;
pa.display();
if(l==1)
{
sphere a;
a.display(R);
}
else if(l==2)
{
cylinder b;
b.display(R);
}
else if(l==3)
{
cone c;
c.display(R);
}
}
main函數中R使用前未初始化。