正文

c++(面向对象关于类的盒子问题)2005-07-13 23:04:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/jay0518/2775.html

分享到:

#include<iostream>
using namespace std;
class Box
{
public:
    Box(double lengthvalue=1.0,double breadthvalue=1.0,double heightvalue=1.0);
    double volume();
    int comparevolume(Box& otherbox);
private:
    double length;
    double breadth;
    double height;
};
Box::Box(double lengthvalue,double breadthvalue,double heightvalue):length(lengthvalue),
breadth(breadthvalue),height(heightvalue)
{
    cout<<endl
        <<"constructor called"
        <<endl;
   length=lengthvalue;
   breadth=breadthvalue;
   height=heightvalue;
}
double Box::volume()
{
    return length*height*breadth;
}
int Box::comparevolume(Box& otherbox)
{
    if(otherbox.volume()>this->volume())
        return 0;
    else return 1;
}
int main()
{
    Box firstbox(1.0,2.0,3.0);
    Box secondbox;
    Box* pthirdbox=new Box(0.1,2.5,3.5);
    cout<<endl<<"volume of first box is:"
        <<firstbox.volume()<<endl;
    cout<<"volume of second box is:"
        <<secondbox.volume()<<endl;
    cout<<"volume of third box is:"
        <<pthirdbox->volume()<<endl;
    if(firstbox.comparevolume(secondbox))
    {
        if(firstbox.comparevolume(*pthirdbox))
        {
            if(secondbox.comparevolume(*pthirdbox))
            {
            cout<<endl
            <<"the largest_volume box is the first box."
            <<endl;
            cout<<"the second_volume box is the second box."
                <<endl;
            cout<<"the third_volume box is the the third box."
                <<endl;
            }
            else
            {
             cout<<endl
            <<"the largest_volume box is the first box."
            <<endl;
            cout<<"the second_volume box is the third box."
                <<endl;
            cout<<"the third_volume box is the the second box."
                <<endl;
            }
        }

        else
        {
            if(pthirdbox->comparevolume(secondbox))
            {
            cout<<"the largest_volume box is the third box."<<endl;
            cout<<"the second_volume box is the first box."<<endl;
            cout<<"the third_volume box is the second box."<<endl;
            }
            else
            {
                cout<<"the largest_volume box is the third box."<<endl;
            cout<<"the second_volume box is the second box."<<endl;
            cout<<"the third_volume box is the first box."<<endl;  
            }
        }
    }
    else
    {
         if(firstbox.comparevolume(*pthirdbox))
         {    
            cout<<endl
            <<"the largest_volume box is the second box."
            <<endl;
            cout<<"the second_volume box is the first box."
                <<endl;
            cout<<"the third_volume box is the the third box."
                <<endl;
         }

        else
        {
            if(pthirdbox->comparevolume(secondbox))
            {
            cout<<"the largest_volume box is the third box."<<endl;
            cout<<"the second_volume box is the second box."<<endl;
            cout<<"the third_volume box is the first box."<<endl;
            }
            else
            {
                cout<<"the largest_volume box is the second box."<<endl;
            cout<<"the second_volume box is the third box."<<endl;
            cout<<"the third_volume box is the first box."<<endl;  
            }
        }
    }
    return 0;
}

阅读(3456) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册