大家帮我看看这代码错在哪?

来源:百度知道 编辑:UC知道 时间:2024/06/05 17:01:11
定义矩形类Rectangle,其数据成员left和right是Point类的对象,分别表示矩形左上角坐标和右下角坐标。试求Rectangle类重载运算符“<”和“>”,实现两个矩形对象面积大小的比较,并编写一个完整的程序,验证重载运算符的正确性。
#include <iostream>
#include <cmath>
using namespace std;

class Point{
float x,y;
public:
Point(float a,float b) {x=a,y=b;}
friend class Rectangle;
};

class Rectangle{
Point left,right;
public:
Rectangle(float a,float b,float c,float d) {left.x=a,left.y=b,right.x=c,right.y=d;}
float Area()
{return (left.y-right.y)*(right.x-left.x);}
void Show()
{cout<<"矩形左上角坐标为:"<<'('<<left.x<<','<<left.y<<")\n";
cout<<"矩形右下角坐标为:"<<'('<<right.x<<','<<right.y<<")\n";
}
friend int operator>(Rectangle p1,Rectangle p2);
friend int operator<(Rectangle p3,Rectangle p4);
};

程序帮你改完了,你自己对照一下吧:

#include<stdio.h>
#include <cmath>
/*
#include <iostream>
using namespace std;
*/
class Point{
float x,y;
public:
Point(){x=0;y=0;}
Point(float a,float b) {x=a;y=b;}
friend class Rectangle;
};

class Rectangle
{
//Point left,right;
public:
Point left,right;
//Rectangle(float a,float b,float c,float d) {left.x=a,left.y=b,right.x=c,right.y=d;}
Rectangle(float a,float b,float c,float d) {left.x=a;left.y=b;right.x=c;right.y=d;}
float Area()
{return (right.y-left.y)*(right.x-left.x);}
void Show()
{//cout<<"矩形左上角坐标为:"<<'('<<left.x<<','<<left.y<<")\n";
//cout<<"矩形右下角坐标为:"<<'('<<right.x<<','<<right.y<<")\n";
printf("矩形左上角坐标为:(%f,%f)\n",lef