需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、 height(高)。要求用成员函数实现以下功能:
(1) 由键盘分别输入3个长方柱的长、宽、高;
(2) 计算长方柱的体积;
(3) 输出3个长方柱的体积。
请编程序,上机调试并运行。
以下是此题的【c++源代码】
答案1,源代码:
#include
using namespace std;
class Box
{public:
void get_value();
float volume();
void display();
public:
float lengh;
float width;
float height;
};
void Box::get_value()
{ cout<
cin>>lengh;
cin>>width;
cin>>height;
}
float Box::volume()
{ return(lengh*width*height);}
void Box::display()
{ cout<
int main()
{Box box1,box2,box3;
box1.get_value();
cout<
box1.display();
box2.get_value();
cout<
box2.display();
box3.get_value();
cout<
box3.display();
return 0;
}
******************************************
答案2,源代码:
#include
using namespace std;
class Box
{public:
void get_value();
void volume();
void display();
public:
float lengh;
float width;
float height;
float vol;
};
void Box::get_value()
{ cout<
cin>>lengh;
cin>>width;
cin>>height;
}
void Box::volume()
{ vol=lengh*width*height;}
void Box::display()
{ cout<
int main()
{Box box1,box2,box3;
box1.get_value();
box1.volume();
cout<
box1.display();
box2.get_value();
box2.volume();
cout<
box2.display();
box3.get_value();
box3.volume();
cout<
box3.display();
return 0;
}