Problem1398--类的练习--多态4

1398: 类的练习--多态4

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 15  Solved: 6
[Submit] [Status] [Web Board] [Creator:]

Description

编写一个程序,定义长方形类,实现如下所有函数
拷贝构造、赋值构造、获取面积、获取IOU面积、获取交集面积、并集面积、面积大小比较、是否为同一长方形比较、自定义cout << 打印函数

构造函数声明如下
Rectangular(double l=0, double t=0, double w=0, double h=0)
    {
        if (w < 0 || h < 0)
        {
            cout << "width or height cannot be negative" << endl;
        }
        else
        {
            left = l;
            top = t;
            width = w;
            height = h;
            area = width*height;
        }
    }
其中  l表示left,t表示top,left,top表示长方形最上角定点的坐标。w表示width(宽度),h表示height(高度)

主函数测试如下:
int main(int argc, char *argv[])
{
    Rectangular a(0, 0, 10, 20);
    Rectangular b(0, 0, 20, 10);
    Rectangular c(0, 0, 20, 11);
 
    Rectangular b2(b);
 
    cout << "a information: " << endl;
    cout << a << endl;
 
    cout << "b information: " << endl;
    cout << b << endl;
 
    cout << "b2 information: " << endl;
    cout << b2 << endl;
 
    cout << "c information: " << endl;
    cout << c << endl;
 
    cout << "a == b : " << (a == b) << endl;
    cout << "b == b2 : " << (b == b2) << endl;
 
    cout << "b > b2 : " << (b > b2) << endl;
    cout << "b >= b2 : " << (b >= b2) << endl;
    cout << "a < c : " << (a < c) << endl;
 
    cout << "a - b : " << a - b << endl;
    cout << "a - c : " << a - c << endl;
 
    cout << "a + b : " << a + b << endl;
    cout << "a + c : " << a + c << endl;
 
    return 0;

}



Sample Output

height = 10
area = 200

b2 information:
left = 0
top = 0
width = 20
height = 10
area = 200

c information:
left = 0
top = 0
width = 20
height = 11
area = 220

a == b : 0
b == b2 : 1
b > b2 : 0
b >= b2 : 1
a < c : 1
a - b : 100
a - c : 90
a + b : 300
a + c : 310

Source/Category

 

[Submit] [Status]