C语言输出长方柱的体积,需要求3个长方柱的体积,请编写一个基于对象的程序。数据成员包括length(长)、width(宽)、 height(高)。要求用成员函数实现以下功能...

这是一个C++程序,定义了一个Box类,包含length、width和height属性,以及获取输入、计算体积和显示体积的方法。程序创建了三个Box对象,分别输入它们的尺寸,并计算和输出每个长方体的体积。
摘要由CSDN通过智能技术生成

需要求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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值