C++中的类和对象

来源:百度知道 编辑:UC知道 时间:2024/06/17 02:40:55
把Location类的对象作为矩形类Rectangle的成员,即平面上的一个矩形一由个位置和矩形的长宽决定,设计矩形类Rectangle,并输出定点坐标值和矩形的长宽值

#include <iostream>
#include <math.h>
using namespace std;

class Location
{
private:
float x;
float y;

public:
Location();
~Location();
void SetLocation(float xx, float yy);
float operator - (Location&);
float GetXY(bool b);
};

Location::Location()
{ }

Location::~Location()
{ }

void Location::SetLocation(float xx, float yy)
{
x = xx;
y = yy;
}

float Location::operator - (Location& location)
{
this->x -= location.x;
this->y -= location.y;
return sqrt(this->x*this->x + this->y*this->y);
}

float Location::GetXY(bool b)
{
if (b)
return x;
else
return y;
}

class Rectangle
{
Location location1;
Location location2;
Location location3;
Location location4;
public: