C++的题目.写代码

来源:百度知道 编辑:UC知道 时间:2024/06/02 06:41:45
1、假定矩形的长和宽分别为length、width,请设计一个矩形类Rect,用普通函数area_compare()作为友元函数来比较两个矩形的面积大小;

2、假定矩形的长和宽分别为length、width,请设计一个矩形类Rect,用Compare类的成员函数area_cmp()作为友元函数来比较两个矩形的面积大小。

(1)
#include<iostream>
using namespace std;
class Rect
{
double length,width;
friend bool area_compare(Rect a,Rect b);
public:
Rect()
{
length=width=0;
}
void set(double a,double b)
{
length=a;
width=b;
}
double get_are()
{
return length*width;
}
};
bool area_compare(Rect a,Rect b)
{
return a.get_are()>b.get_are();
}
void main()
{
Rect a,b;
b.set(1.22,4.33);
a.set(2.443,12312.12312);
cout<<area_compare(a,b);
}
(2)
#include<iostream>
using namespace std;
class Compare;
class Rect
{
double length,width;
friend class Compare;
public:
Rect()
{
length=width=0;
}
void set(double a,double b)
{
length=a;
width=b;
}
double get_are()
{
return length*width;
}
};
class Compare
{
public: