大家帮帮忙,真的不知道哪里错了,一直编译错误,希望给串代码参考一下
需要求3个长方体的体积,请编写一个基于对象的程序。数据成员抱愧哦length、width、height。要求用成员函数实现以下功能:
(1)由键盘分别输入长方体的长、宽、高(要求校验输入数据的合法性,如果任一数据小于0,则长宽高直接置为0,并显示"ERROR!",否则显示"OK!");
(2)计算长方体的体积;
(3)输出长方体的体积(先输出长宽高信息,再输出体积)
类和函数接口如:
class Cube{
public:
void set_value(int len,int wd,int ht);
int cal_volume();
void show();
private:
int length,width,height;
};
裁判测试程序样例
在这里给出函数被调用进行测试的例子:
int main()
{
int x,y,z;
Cube c;
cin>>x>>y>>z;
c.set_value(x,y,z);
c.show();
return 0;
}
/* 请在这里填写答案 */
参考代码如下:
#include <iostream>
using namespace std;
class Cube
{
public:
void set_value(int len, int wd, int ht);
int cal_volume();
void show();
private:
int length, width, height;
};
int main()
{
int x, y, z;
Cube c;
cin >> x >> y >> z;
c.set_value(x, y, z);
c.show();
return 0;
}
/* 请在这里填写答案 */
void Cube::set_value(int len, int wd, int ht){
if (len<0 || wd<0 || ht<0)
{
length = 0;
width = 0;
height = 0;
cout << "ERROR!" << endl;
}
else
{
length = len;
width = wd;
height = ht;
cout << "OK!" << endl;
}
}
int Cube::cal_volume()
{
return length * width * height;
}
void Cube::show()
{
cout << "长:" << length << endl;
cout << "宽:" << width << endl;
cout << "高:" << height << endl;
cout << "体积:" << cal_volume() << endl;
}
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!