c++程序设计第3版谭浩强课后答案
需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、 height(高)。要求用成员函数实现以下功能:
(1) 由键盘分别输入3个长方柱的长、宽、高;
(2) 计算长方柱的体积;
(3) 输出3个长方柱的体积。
请编程序,上机调试并运行。
以下是此题的【c++源代码】
答案1,源代码:
#include <iostream>
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<<"please input lengh, width,height:";
cin>>lengh;
cin>>width;
cin>>height;
}
float Box::volume()
{ return(lengh*width*height);}
void Box::display()
{ cout<<volume()<<endl;}
int main()
{Box box1,box2,box3;
box1.get_value();
cout<<"volmue of bax1 is ";
box1.display();
box2.get_value();
cout<<"volmue of bax2 is ";
box2.display();
box3.get_value();
cout<<"volmue of bax3 is ";
box3.display();
return 0;
}
******************************************
答案2,源代码:
#include <iostream>
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<<"please input lengh, width,height:";
cin>>lengh;
cin>>width;
cin>>height;
}
void Box::volume()
{ vol=lengh*width*height;}
void Box::display()
{ cout<<vol<<endl;}
int main()
{Box box1,box2,box3;
box1.get_value();
box1.volume();
cout<<"volmue of bax1 is ";
box1.display();
box2.get_value();
box2.volume();
cout<<"volmue of bax2 is ";
box2.display();
box3.get_value();
box3.volume();
cout<<"volmue of bax3 is ";
box3.display();
return 0;
}
本文链接:http://www.wb98.com/cjia/post/cjia_8.6.html
本站文章搜索: