Problem Description
定义并实现一个矩形类Rectangle,有长(length)、宽(wide)两个属性,成员函数area计算矩形的面积。
输出格式为:左对齐,每个输出项占10列。
添加必要的矩形类成员,完善程序。
//你的代码将被嵌在这里
int main()
{
Rectangle r1(3, 4);
r1.display();
Rectangle r2;
r2 = r1;
r2.display();
Rectangle r3(r1);
r3.display();
Rectangle r4=r1;
r4.display();
Rectangle rArray[2];
for (int i = 0; i < 2; i++)
rArray[i].display();
return 0;
}
Input Description
无
Sample Output
Length Width Area
3 4 12
调用了赋值运算符重载函数
Length Width Area
3 4 12
调用了拷贝构造函数
Length Width Area
3 4 12
调用了拷贝构造函数
Length Width Area
3 4 12
Length Width Area
0 0 0
Length Width Area
0 0 0
#include<iostream>
using namespace std;
#include<iomanip>
class Rectangle
{
public:
int length, wide;
Rectangle(int a,int b)
{
length = a;
wide = b;
}
Rectangle & operator=(const Rectangle& p)
{
length = p.length;
wide = p.wide;
cout << "调用了赋值运算符重载函数" << endl;
return *this;
}
//拷贝构造函数
Rectangle(const Rectangle& p)
{
length