C语言输出长方柱的体积,需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、 height(高)。要求用成员函数实现以下功能...
需要求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;
}
總結
以上是生活随笔為你收集整理的C语言输出长方柱的体积,需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、 height(高)。要求用成员函数实现以下功能...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 启动T0运行的C语言语句是,单片机填空题
- 下一篇: android handler作用,3.