要自己写,给你个参考!这个是计算各类的面积和体积的代码!
#include<iostream>
#include<cmath>
using namespace std;
class shape
{
public:
shape(float x=0);
virtual void input()=0; //输入图形信息
virtual void disp()=0; //现实基本图形信息
virtual float area()=0; //计算表面积
virtual void volum()=0; //计算体积
protected:
float r;
};
class ball:public shape //球体类
{
public:
virtual void input();
virtual void disp();
virtual float area();
virtual void volum();
};
class squ:public shape //正方体类
{
public:
virtual void input();
virtual void disp();
virtual float area();
virtual void volum();
};
class rect:public shape //长方体类
{
public:
virtual void input();
virtual void disp();
virtual float area();
virtual void volum();
private:
float w,h;
};
class cyl:public shape //圆柱体类
{
public:
virtual void input();
virtual void disp();
virtual float area();
virtual void volum();
private:
float h;
};
void ball::input()
{
cout<<"请输入球的半径:";
cin>>r;
system("pause");
system("cls");
}
void ball::disp()
{
cout<<"球的表面积为:";
cout<<s;
cout<<"球的体积为:";
cout<<v;
system("pause");
system("cls");
}
float ball::area()
{
float s;
s=4*3.14*r*r;
return (s);
system("pause");
system("cls");
}
float ball::volum()
{
float v;
v=4.0/3*3.14*r*r*r;
return(v);
system("pause");
system("cls");
}
void squ::input()
{
cout<<"请输入正方体的边长:";
cin>>r;
system("pause");
system("cls");
}
void squ::disp()
{
cout<<"正方体的表面积为:";
cout<<s;
cout<<"正方体的体积为:";
cout<<v;
system("pause");
system("cls");
}
float squ::area()
{
float s;
s=6*r*r;
return (s);
system("pause");
system("cls");
}
float squ::volum()
{
float v;
v=r*r*r;
return(v);
system("pause");
system("cls");
}
void rect::input()
{
cout<<"请输入长方体的长:";
cin>>r;
cout<<"请输入长方体的宽:";
cin>>w;
cout<<"请输入长方体的高:";
cin>>h;
system("pause");
system("cls");
}
void ball::disp()
{
cout<<"长方体的表面积为:";
cout<<s;
cout<<"长方体的体积为:";
cout<<v;
system("pause");
system("cls");
}
float ball::area()
{
float s;
s=2*(r*w+r*h+w*h);
return (s);
system("pause");
system("cls");
}
float ball::volum()
{
float v;
v=r*w*h;
return(v);
system("pause");
system("cls");
}
void cyl::input()
{
cout<<"请输入圆柱体的底面半径:";
cin>>r;
cout<<"请输入圆柱体的高:";
cin>>h;
system("pause");
system("cls");
}
void ball::disp()
{
cout<<"圆柱体的表面积为:";
cout<<s;
cout<<"圆柱体的体积为:";
cout<<v;
system("pause");
system("cls");
}
float ball::area()
{
float s;
s=2*r*3.14*h;
return (s);
system("pause");
system("cls");
}
float ball::volum()
{
float v;
v=h*3.14*r*r;
return(v);
system("pause");
system("cls");
}
static int i =0; //全局静态变量
int main()
{
ball a;
squ b;
rect c;
cyl d;
int i;
for(;;)
{
cout<<"\t\t*********************************\n";
cout<<"\t\t*****几何体的表面积与体积********\n";
cout<<"\t\t*********************************\n";
cout<<"1-----------输入各种形状信息:\n";
cout<<"2-----------显示各种形状信息:\n";
cout<<"3-----------计算各种形状的表面积:\n";
cout<<"4-----------计算各种形状的体积:\n";
cout<<"5-----------退出系统:\n";
cout<<"\n\n请选择功能:";
cin>>i;
switch(i)
{
case 1:
{ system("cls");
a.input();
b.input();
c.input();
d.input();
}break;
case 2:
{ system("cls");
a.disp();
b.disp();
c.disp();
d.disp();
}break;
case 3:
{ system("cls");
a.area();
b.area();
c.area();
d.area();
}break;
case 4:
{ system("cls");
a.volume();
b.volume();
c.volume();
d.volume();
}break;
case 5:
{
return 0;
}
}
}
system("pause");
return 0;
}