新手小白,求教一道经典的 C++ 题
定义盒子Box类,要求具有以下成员:长、宽、高分别为x,y,z,可设置盒子形状;可计算盒子体积;可计算盒子的表面积。标答给的程序是:
#include <iostream>
using namespace std;
class Box{
public:
int weight;
int length;
int hight;
void box_shape(int w, int l, int h);
int box_volume(int w, int l, int h);
int box_area(int w, int l, int h);
};
int main()
{
Box mybox;
cout << "Please Enter weight and length and hight:";
cin >> mybox.weight >> mybox.length >> mybox.hight;
int box_v, box_a;
mybox.box_shape(mybox.weight, mybox.length, mybox.hight);
box_v = mybox.box_volume(mybox.weight, mybox.length, mybox.hight);
cout << "This box's volume =" << box_v << endl;
box_a = mybox.box_area(mybox.weight, mybox.length, mybox.hight);
cout << "This box's area = " << box_a << endl;
}
void Box::box_shape(int w, int l, int h)
{
if(w == l && l == h)
cout << "This is a Cube!" << endl;
else
cout << "This is a cuboid!" << endl;
}
int Box::box_volume(int w, int l, int h)
{
return w * l * h;
}
int Box::box_area(int w, int l, int h)
{
return 2 * w * l + 2 * l * h + 2 * w * h;
}
我表示自己private跟public下的成员搞不清,不是说一般都把数据成员设定为private,把成员函数设为public么?如果把三个属性weight,length,hight放到private里面,这个程序该怎么写?求教呀。。