C++课程设计,急急急!

来源:百度知道 编辑:UC知道 时间:2024/06/02 00:26:09
设计矩形类,包括构造函数,析构函数,"GET"函数和"SET"函数,以及下述重载的运算符:
1.重载加法运算符+,将俩个矩形相加;
2.重载加法运算符-,将俩个矩形相减;
3.重载加法运算符*,将俩个矩形相乘.
只须实现相加,相减,相乘既可!

#include <iostream>
using namespace std;

class Rect{
private:
unsigned int width;
unsigned int height;

public:
Rect():width(0), height(0) {}
Rect(int w, int h):width(w), height(h) {}

public:
unsigned int getWidth() { return (*this).width; }
void setWidth(unsigned int width) { (*this).width = width; }
unsigned int getHeight() {return (*this).height;}
void setHeight(unsigned int height) {(*this).height = height;}

public:
Rect operator+(Rect rhs)
{
return Rect((*this).width+rhs.width, (*this).height+rhs.height);
}

Rect operator-(Rect rhs)
{
return Rect((*this).width-rhs.width, (*this).height-rhs.height);
}

Rect operator*(Rect rhs)
{
return Rect((*this).width*rhs.width, (*this).height*rhs.height);
}
};

int main(int argc, char* argv[])
{
Rect rect1(5, 4), rect2(3,2);
Rect addrect = rect1+