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

试编写一个基于对象的程序,求3个长方体的体积。

小然然 发布于 2010-03-21 16:49, 2354 次点击
试编写一个基于对象的程序,求3个长方体的体积。长方体对象名为rectangle,数据成员包括:length,width,height。要求用成员函数实现以下功能:
①有键盘分别输入3个长方体的长、宽、高;
②计算其长方体之体积;
③输出3个长方体的体积。
2 回复
#2
apull2010-03-21 22:46
直接写了

程序代码:
class rectangle
{
    private:
        int length,width,height;
    public:
        void input();
        long volume();
        void print();
}
void rectangle::input()
{
    cout <<"input length,width,height:";
    cin >> length >> width >> height;
}
long rectangle::volume()
{
    return length*width*height;
}
void rectangle::print()
{
    cout << "V=" << rectangle()<< endl;
}

int main()
{
    rectangle rect;
    rect.input();
    rect.print();

    return 0;
}
#3
qlc002010-03-22 11:20
#include<iostream>
using namespace std;
class rectangle
{
    private:
        int length,width,height;
    public:
        void input();
        long volume();
        void print();
};
void rectangle::input()
{
    cout <<"input length,width,height:";
    cin>>length>>width>>height;
}
long rectangle::volume()
{
    return length*width*height;
}
void rectangle::print()
{
    cout<<"长方体的体积为:"<<volume()<<endl;
}

int main()
{
    rectangle R[3];
    for(int i=0;i<3;i++)
    {
    R[i].input();
    R[i].print();
    }

    return 0;
}
这个是对楼上的补充!
1