-
创建Point类
要求:
私有数据成员:x,y;均为单精度小数类型。
公有成员:
默认值为0.0的构造函数。
返回值为float的成员函数:getX()与getY(),返回相应的私有数据成员的值。
返回值为void的成员函数:setX(float)与setY(float),用于设置相应的数据成员的值。 -
创建MyRectangle类
要求:
私有数据成员:Point类的对象leftup,rightdonw。分别存储矩形的左上角坐标和右下角坐标。
公有函数成员:
返回值为void的print1(string)成员函数,输出的格式如下:
其中矩形2为这个矩形的名字,通过print1()函数的参数传入。
返回值类型为float的width()成员函数,返回当前矩形对象的宽。
返回值类型为float的length()成员函数,返回当前矩形对象的长。
返回值类型为Point的成员函数getleftup()和getrightdown()用于返回当前对象的左上角坐标和右下角坐标。
无参的构造函数MyRectangle()
带参的构造函数MyRectangle(Point,Point)
为MyRectangle类定义+、-运算符的重载函数。
+、-运算定义为友元函数。
运算定义为固定矩形左上角,对右下角的坐标进行加减运算,使得新矩形的长宽为原两个矩形的长宽之和或差。
定义+=、-=复合赋值运算符。
+=、-=复合赋值运算定义为类的成员函数;
+=、-=运算定义为固定矩形左上角,对右下角的坐标进行加减运算,使得新矩形的长宽为原两个矩形的长宽之和或差。 -
完成主函数,在主函数中设计类的对象,并由类的对象调用其成员函数实现本题中要求的输出结果。
要求输出格式如下:
#include <iostream>
using namespace std;
class Point
{
private:
float x,y;
public:
Point(float a=0.0,float b=0.0){
x=a;
y=b;
}
float getX(){
return x;
}
float getY(){
return y;
}
void setX(float a){
x=a;
}
void setY(float a){
y=a;
}
};
class MyRectangle
{
private:
Point leftup,rightdonw;
public:
MyRectangle(){
}
MyRectangle(Point a,Point b)
{
leftup.setX(a.getX());
leftup.setY(a.getY());
rightdonw.setX(b.getX());
rightdonw.setY(b.getY());
}
void print1(string a){
cout<<a<<"的左上角坐标为:("<<leftup.getX()<<","<<leftup.getY()<<")右下角坐标为:("
<<rightdonw.getX()<<","<<rightdonw.getY()<<"); "<<a<<"的长为:"<<length()<<"; "<<a<<"的宽为:"<<width()<<endl;
}
float width(){
return leftup.getY()-rightdonw.getY();
}
float length(){
return rightdonw.getX()-leftup.getX();
}
Point getleftup()
{
Point a;
a.setX(leftup.getX());
a.setY(leftup.getY());
return a;
}
Point getrightdonw()
{
Point a;
a.setX(rightdonw.getX());
a.setY(rightdonw.getY());
return a;
}
friend MyRectangle operator +(MyRectangle A,MyRectangle B){
MyRectangle a;
a.leftup.setX(A.leftup.getX());
a.leftup.setY(A.leftup.getY());
a.rightdonw.setX(A.rightdonw.getX()+B.length());
a.rightdonw.setY(A.rightdonw.getY()-B.width());
return a;
}
friend MyRectangle operator -(MyRectangle A,MyRectangle B){
MyRectangle a;
a.leftup.setX(A.leftup.getX());
a.leftup.setY(A.leftup.getY());
a.rightdonw.setX(A.rightdonw.getX()-B.length());
a.rightdonw.setY(A.rightdonw.getY()+B.width());
return a;
}
void operator +=(MyRectangle B){
this->rightdonw.setX(this->rightdonw.getX()+B.length());
this->rightdonw.setY(this->rightdonw.getY()-B.width());
}
void operator -=(MyRectangle A){
this->rightdonw.setX(this->rightdonw.getX()-A.length());
this->rightdonw.setY(this->rightdonw.getY()+A.width());
}
};
int main()
{
Point x(1,3),y(3,3),z(4,1),k(5,2);
MyRectangle A(x,z),B(y,k);
A.print1("矩形1");
B.print1("矩形2");
A+=B;
cout<<"+=后新矩形的左上角坐标为:("
<<A.getleftup().getX()<<","<<A.getleftup().getY()
<<")右下角坐标为:("<<A.getrightdonw().getX()<<","<<A.getrightdonw().getY()<<");"
<<"+=后新矩形的长为:"<<A.length()<<"; +=后新矩形的宽为:"<<A.width()<<endl;
cout<<"**************************************************"<<endl;
MyRectangle C(x,z),D(y,k);
C.print1("矩形1");
D.print1("矩形2");
C-=D;
cout<<"-=后新矩形的左上角坐标为:("
<<C.getleftup().getX()<<","<<C.getleftup().getY()
<<")右下角坐标为:("<<C.getrightdonw().getX()<<","<<C.getrightdonw().getY()<<");"
<<"-=后新矩形的长为:"<<C.length()<<"; -=后新矩形的宽为:"<<C.width()<<endl;
cout<<"**************************************************"<<endl;
cout<<"**************************************************"<<endl;
A.print1("矩形1");
B.print1("矩形2");
MyRectangle A1=A+B;
cout<<"+后新矩形的左上角坐标为:("
<<A1.getleftup().getX()<<","<<A1.getleftup().getY()
<<")右下角坐标为:("<<A1.getrightdonw().getX()<<","<<A1.getrightdonw().getY()<<");"
<<"+后新矩形的长为:"<<A1.length()<<"; +后新矩形的宽为:"<<A1.width()<<endl;
cout<<"**************************************************"<<endl;
A1=A-B;
cout<<"-后新矩形的左上角坐标为:("
<<A1.getleftup().getX()<<","<<A1.getleftup().getY()
<<")右下角坐标为:("<<A1.getrightdonw().getX()<<","<<A1.getrightdonw().getY()<<");"
<<"-后新矩形的长为:"<<A1.length()<<"; -后新矩形的宽为:"<<A1.width()<<endl;
return 0;
}
#输出