3个回答
展开全部
#include <iostream>
using namespace std;
#define PI 3.1415926
class Area
{
public:
virtual double biaoarea()=0;
virtual double volumn()=0;
};
class Ball:public Area
{
private:
double r;
public:
Ball():Area()
{
r=0.0;
}
virtual double biaoarea()
{
double k;
k=4.0*PI*r*r;
return k;
}
virtual double volumn()
{
double k=4.0/3.0;
k=k*PI*r*r*r;
return k;
}
void set(double t)
{
r=t;
}
};
class Cube:public Area
{
private:
double length;
double width;
double high;
public:
Cube():Area()
{
length=0.0;
width=0.0;
high=0.0;
}
virtual double volumn()
{
double k;
k=length*width*high;
return k;
}
virtual double biaoarea()
{
double k;
k=2.0*(length*width+length*high+width*high);
return k;
}
void set(double a,double b,double c)
{
length=a;
width=b;
high=c;
}
};
class Cylinder:public Area
{
private:
double r;
double high;
public:
Cylinder():Area()
{
r=0.0;
high=0.0;
}
virtual double biaoarea()
{
double k;
k=PI*r*r+2.0*PI*r*high;
return k;
}
virtual double volumn()
{
double k;
k=PI*r*r*high;
return k;
}
void set(double a,double b)
{
r=a;
high=b;
}
};
class Taper:public Area
{
private:
double r;
double l;
double hight;
public:
Taper():Area()
{
r=0.0;
l=0.0;
hight=0.0;
}
virtual double biaoarea()
{
double k;
k=PI*r*r+PI*r*l;
return k;
}
virtual double volumn()
{
double k;
k=(1.0/3.0)*PI*r*r*hight;
return k;
}
void set(double a,double b,double c)
{
r=a;
l=b;
hight=c;
}
};
int main()
{
double length,width,high,rr;
Ball mm;
Cube nn;
Taper kk;
Cylinder tt;
int chioce;
do
{
cout<<"请输入你需要计算的集合体:(1.球 2.立方体 3.圆锥体 4.圆柱体 5.退出):"<<endl;
cin>>chioce;
switch(chioce)
{
case 1:
cout<<"请输入需要计算的球的半径r:";
cin>>rr;
mm.set(rr);
cout<<"该球的表面积为:"<<mm.biaoarea()<<endl;
cout<<"该球的体积为: "<<mm.volumn()<<endl;
break;
case 2:
cout<<"请输入需要计算的立方体的长、宽、高:";
cin>>length>>width>>high;
nn.set(length,width,high);
cout<<"该立方体的表面积为:"<<nn.biaoarea()<<endl;
cout<<"该立方体的体积为: "<<nn.volumn()<<endl;
break;
case 3:
cout<<"请输入需要计算的圆锥体的半径、母线长、高:";
cin>>rr>>length>>high;
kk.set(rr,length,high);
cout<<"该圆锥的表面积为:"<<kk.biaoarea()<<endl;
cout<<"该圆锥体的体积为:"<<kk.volumn()<<endl;
break;
case 4:
cout<<"请输入需要计算的圆柱体的半径和高:";
cin>>rr>>high;
tt.set(rr,high);
cout<<"该圆柱体的表面积为:"<<tt.biaoarea()<<endl;
cout<<"该圆柱体的体积为: "<<tt.volumn()<<endl;
break;
case 5:
cout<<"退出成功!"<<endl;
break;
default:
cout<<"输入错误,请重新输入!"<<endl;
break;
}
} while (chioce != 5);
return 0;
}
2013-07-01
展开全部
楼上的函数写错了。
double CCube::area()const
{
return light*light;
}
以及
CCube x(10);
cout<<"立方体边长为30"<<endl;
double CCube::area()const
{
return light*light;
}
以及
CCube x(10);
cout<<"立方体边长为30"<<endl;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
using namespace std;
class CSolid
{
public:
virtual double area() const=0;
virtual double volume() const=0;
};
class CCube:public CSolid
{
public:
CCube(double l):light(l) {}
virtual double area() const;
virtual double volume()const;
private:
double light;
};
double CCube::area()const
{
return light*light;
}
double CCube::volume()const
{
return light*light*light;
}
class CBall:public CSolid
{
public:
CBall(double r):radius(r) {}
virtual double area()const;
virtual double volume()const;
private:
double radius;
};
double CBall::area()const
{
return 4*3.14*radius*radius;
}
double CBall::volume()const
{
return (4*3.14*radius*radius*radius)/3;
}
class CCylinder:public CSolid
{
public:
CCylinder(double r,double h):radius(r),height(h) {}
virtual double area()const;
virtual double volume()const;
private:
double radius;
double height;
};
double CCylinder::area()const
{
return (2*(3.14*radius*height+3.14*radius*radius));
}
double CCylinder::volume()const
{
return 3.14*radius*radius*height;
}
int main( )
{
CSolid *p;
double s,v;
CCube x(10);
cout<<"立方体边长为30"<<endl;
p=&x;
s=p->area( );
v=p->volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
CBall y(5);
cout<<"球体半径为5"<<endl;
p=&y;
s=p->area( );
v=p->volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
CCylinder z(10,20);
cout<<"圆柱体底面半径、高分别为10, 20"<<endl;
p=&z;
s=p->area( );
v=p->volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
return 0;
}
其他的,直接继承基类,然后写个计算公式就可以了
using namespace std;
class CSolid
{
public:
virtual double area() const=0;
virtual double volume() const=0;
};
class CCube:public CSolid
{
public:
CCube(double l):light(l) {}
virtual double area() const;
virtual double volume()const;
private:
double light;
};
double CCube::area()const
{
return light*light;
}
double CCube::volume()const
{
return light*light*light;
}
class CBall:public CSolid
{
public:
CBall(double r):radius(r) {}
virtual double area()const;
virtual double volume()const;
private:
double radius;
};
double CBall::area()const
{
return 4*3.14*radius*radius;
}
double CBall::volume()const
{
return (4*3.14*radius*radius*radius)/3;
}
class CCylinder:public CSolid
{
public:
CCylinder(double r,double h):radius(r),height(h) {}
virtual double area()const;
virtual double volume()const;
private:
double radius;
double height;
};
double CCylinder::area()const
{
return (2*(3.14*radius*height+3.14*radius*radius));
}
double CCylinder::volume()const
{
return 3.14*radius*radius*height;
}
int main( )
{
CSolid *p;
double s,v;
CCube x(10);
cout<<"立方体边长为30"<<endl;
p=&x;
s=p->area( );
v=p->volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
CBall y(5);
cout<<"球体半径为5"<<endl;
p=&y;
s=p->area( );
v=p->volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
CCylinder z(10,20);
cout<<"圆柱体底面半径、高分别为10, 20"<<endl;
p=&z;
s=p->area( );
v=p->volume( );
cout<<"表面积:"<<s<<endl;
cout<<"体积:"<<v<<endl;
cout<<endl;
return 0;
}
其他的,直接继承基类,然后写个计算公式就可以了
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询