第十三节 测试
#include<iostream>
using namespace std;
class Shape
{
public:
Shape(){}
virtual ~Shape(){}
virtual void print()const =0;//打印
};
class Point
{
public:
int x;
int y;
Point(int _x,int _y):x(_x),y(_y)
{}
};
class Rectangle:public Shape
{
int width;
int height;
Point* leftUp;
public:
Rectangle(int _width,int _height,int _x,int _y);//构造
Rectangle(const Rectangle& other);//拷贝构造
Rectangle& operator=(const Rectangle& other);//重载'='
~Rectangle();//析构
void print()const;
};
inline
Rectangle::Rectangle(int _width,int _height,int _x,int _y):width(_width),height(_height),leftUp(new Point(_x,_y))
{
}
inline void
Rectangle::print() const
{
cout<<"width:"<<width<<",height:"<<height<<",Point("<<leftUp->x<<","<<leftUp->y<<")"<<endl;
}
inline
Rectangle::Rectangle(const Rectangle& other)
{
width=other.width;
height=other.height;
if(other.leftUp!=NULL)
leftUp=new Point(other.leftUp->x,other.leftUp->y);
else
{
delete leftUp;
leftUp=NULL;
}
}
inline Rectangle&
Rectangle::operator = (const Rectangle& other)
{
if (this == &other)
{
return *this;
}
// delete leftUp;
width = other.width;
height = other.height;
if(leftUp==NULL)//我空
{
//我空它空省略
if(other.leftUp!=NULL)//我空它不空
{
leftUp = new Point(other.leftUp->x, other.leftUp->y);
}
}
else//我不空
{
if(other.leftUp==NULL)//我不空它空
{
delete leftUp;
leftUp=NULL;
}
else//我不空它不空
{
leftUp=new Point(*(other.leftUp));
}
}
return *this;
}
inline
Rectangle::~Rectangle()
{
delete leftUp;
}
int main()
{
Rectangle a(1,1,2,2);//构造
Rectangle b(a);//拷贝构造
Rectangle c(3,3,4,4);
Rectangle d(0,0,0,0);
d=c;//重载'='
a.print();//打印
b.print();
c.print();
d.print();
return 0;
}
记录学习点点滴滴。有兴趣的关注我一起。
网友评论