//面向对象的编程
/定义Volume类,在main函数中利用该类定义3个圆柱体,
其中一个圆柱体用带参数的构造函数产生,另外两个圆柱体的名称、半径和高由键盘输入。
计算这三个圆柱体的体积和表面积;最后输出三个圆柱体的基本信息。
/
const int PI=3.14
#include
#include
#include
using namespace std;
class Volume{
private:
int r;
int h;
string mm;
public:
void SetRadius( ) //设置圆柱体半径为r
{cin>>r;}
void SetHeight( ) //设置圆柱体高度为h
{cin>>h;}
void SetName( ) //设置圆柱体名称为mm
{getline(cin,mm);}
int GetRadius( ) //获取圆柱体半径
{return r;}
int GetHeight( ) //获取圆柱体高度
{return h;}
double GetVolume( ) //获取圆柱体体积
{return PIrrh;}
double GetSphere( ) //获取圆柱体表面积
{return PIrr2+2PIrh;}
void DispInfo( ) //显示圆柱体的基本信息
{cout<<setfill('')<<setw(20)<<endl;
cout<<"其高是:"<<GetRadius( );
cout<<endl<<"其半径是:"<<GetRadius( )<<endl;
cout<<"其表面积是:"<<GetSphere( )<<endl;
cout<<"其体积是:"<<GetVolume( )<<endl;}
//Volume( ); //不带任何参数的构造函数
};
Volume::Volume( int r1,int h1,string name ){
}//报错位置..................................................................
void main(){
Volume V1,V2,V3;
cout<<"请输入第一个圆柱体的名称、高、半径:";
V1.SetName( ) ;
V1.SetHeight( ) ;
V1.SetRadius( ) ;
cout<<"请输入第二个圆柱体的名称、高、半径:";
V2.SetName( ) ;
V2.SetHeight( ) ;
V2.SetRadius( ) ;
cout<<endl;
}
建议学习一下《代码整洁之道》
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
const double PI = 3.14;
using namespace std;
class Volume {
private:
int r;
int h;
string mm;
public:
void SetRadius() //设置圆柱体半径为r
{
cin >> r;
}
void SetHeight() //设置圆柱体高度为h
{
cin >> h;
}
void SetName() //设置圆柱体名称为mm
{
getline(cin, mm);
}
int GetRadius() //获取圆柱体半径
{
return r;
}
int GetHeight() //获取圆柱体高度
{
return h;
}
double GetVolume() //获取圆柱体体积
{
return PI * r * r * h;
}
double GetSphere() //获取圆柱体表面积
{
return PI * r * r * 2 + 2 * PI * r * h;
}
void DispInfo() //显示圆柱体的基本信息
{
// cout << setfill("*") << setw(20) << endl;
cout << "其高是:" << GetRadius();
cout << endl << "其半径是:" << GetRadius() << endl;
cout << "其表面积是:" << GetSphere() << endl;
cout << "其体积是:" << GetVolume() << endl;
}
//Volume(int r1, int h1, string name) {}
Volume() {}
};
int main() {
Volume V1, V2, V3;
cout << "请输入第一个圆柱体的名称、高、半径:";
V1.SetName();
V1.SetHeight();
V1.SetRadius();
cout << "请输入第二个圆柱体的名称、高、半径:";
V2.SetName();
V2.SetHeight();
V2.SetRadius();
V1.DispInfo();
V2.DispInfo();
return 0;
}