标题:新手小白,求教一道经典的 C++ 题
只看楼主
皓皓
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-7-22
结帖率:0
已结贴  问题点数:10 回复次数:4 
新手小白,求教一道经典的 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里面,这个程序该怎么写?求教呀。。
搜索更多相关主题的帖子: include public 经典的 表面积 Enter 
2014-07-27 11:15
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6809
专家分:42393
注 册:2010-12-16
得分:4 
写多几个程序,就慢慢体会到了。没有规定什么一定public 的,只是符合自己设计,好读好维护,别人看起来不会很搓,就行了

我行我乐
我的博客:
http://blog.yuccn. net
2014-07-27 15:59
皓皓
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2014-7-22
得分:0 
回复 2 楼 yuccn
那把那三个放进private里面到底怎么写呀??
2014-07-27 16:03
funyh250
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:26
帖 子:290
专家分:1573
注 册:2013-12-25
得分:4 
程序代码:
#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;
}

学习是大事   吃喝拉撒睡是小事   其他的那都不是事
2014-07-28 08:02
whyheng
Rank: 1
等 级:新手上路
帖 子:1
专家分:4
注 册:2014-7-29
得分:4 
一点非技术性问题,Weight 是重量的意思, 宽度的英文是width。
2014-07-29 10:15



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-434437-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.741792 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved