#include
using namespace std;
class rect
{
public:
rect(int width, int length) : width(width),length(length){}
int Area()
return length * width;
}
int Perimeter()
return 2 * (length + width);
friend int add(rect &r1, rect &r2);
private:
int length;
int width;
};
int add(rect &r1,rect &r2)
return r1.Area() + r2.Area();
int main()
rect rect1(2,4), rect2(4,6);
cout
return 0;
執行結果
rect1的面積是8
rect1的周長是12
rect2的面積是24
rect2的周長是20
rect1 和rect2的面積之和為32
Press any key to continue
#include
using namespace std;
class rect
{
public:
rect(int width, int length) : width(width),length(length){}
int Area()
{
return length * width;
}
int Perimeter()
{
return 2 * (length + width);
}
friend int add(rect &r1, rect &r2);
private:
int length;
int width;
};
int add(rect &r1,rect &r2)
{
return r1.Area() + r2.Area();
}
int main()
{
rect rect1(2,4), rect2(4,6);
cout
cout
cout
cout
cout
return 0;
}
執行結果
rect1的面積是8
rect1的周長是12
rect2的面積是24
rect2的周長是20
rect1 和rect2的面積之和為32
Press any key to continue