注册 登录
编程论坛 C++教室

新手小白,求教一道经典的 C++ 题

皓皓 发布于 2014-07-27 11:15, 712 次点击
定义盒子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里面,这个程序该怎么写?求教呀。。
4 回复
#2
yuccn2014-07-27 15:59
写多几个程序,就慢慢体会到了。没有规定什么一定public 的,只是符合自己设计,好读好维护,别人看起来不会很搓,就行了
#3
皓皓2014-07-27 16:03
回复 2 楼 yuccn
那把那三个放进private里面到底怎么写呀??
#4
funyh2502014-07-28 08:02
程序代码:
#include <iostream>
using namespace std;
     class Box{

    int weight;
    int length;
    int hight;

 public:
    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 get_weight()
    {
        return weight;
    }
    void set_weight(int w)
    {
        weight=w;
    }
        
};
int main()
{
    Box mybox;
    int inputweight;
    cout << "Please Enter weight and length and hight:";
//    cin >> mybox.weight >> mybox.length >> mybox.hight;
    cin>>inputweight;
    mybox.set_weight(inputweight);

    int box_v, box_a;
        mybox.box_shape(mybox.get_weight,// mybox.length, mybox.hight);
    box_v = mybox.box_volume(mybox.get_weight,// mybox.length, mybox.hight);
    cout << "This box's volume =" << box_v << endl;
    box_a = mybox.box_area(mybox.get_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;
}



程序代码:
#include <iostream>
using namespace std;
     class Box{

    int weight;
    int length;
    int hight;

 public:
    void box_shape();
    int box_volume();
    int box_area();

    friend istream& operator>>(istream&is,Box&box)
    {
        is>>box.weight>>box.length>>box.hight;
        return is;
    }
   
        
};
int main()
{
    Box mybox;
    cout << "Please Enter weight and length and hight:";
//    cin >> mybox.weight >> mybox.length >> mybox.hight;
    cin>>mybox;

    int box_v, box_a;
    mybox.box_shape();
    box_v = mybox.box_volume();
    cout << "This box's volume =" << box_v << endl;
    box_a = mybox.box_area();
    cout << "This box's area = " << box_a << endl;
   
}
void Box::box_shape()
{
    if(weight == length && length == hight)
        cout << "This is a Cube!" << endl;
    else
        cout << "This is a cuboid!" << endl;
}
     int Box::box_volume()
{
    return weight * length * hight;
}

int Box::box_area()
{
    return 2 * weight * length + 2 * length * hight + 2 * weight * hight;
}
#5
whyheng2014-07-29 10:15
一点非技术性问题,Weight 是重量的意思, 宽度的英文是width。
1