class Shape
{
int no;
};
class Point
{
int x;
int 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();
};
設(shè)計思路
01. Big Three Function Design - 三大函數(shù)設(shè)計
1.1 General Constructor - 構(gòu)造函數(shù)
private could be omit at the begging of a class.
class MyClass
{
// for private variables
public:
// for public interface
protected:
// for protected functions
}
There is a point-to-class pointer and how to build a constructor
// Class
class Point
{
int x;
int y;
public:
Point (int x = 0, int y =0)
{
this->x = x;
this->y = y;
}
int get_x() const {return x;}
int get_y() const {return y;}
};
class Rectangle: public Shape
{
int width;
int height;
Point* leftUp;
public:
Rectangle(int width, int height, int x, int y);
...
};
// Constructor
Rectangle::Rectangle(int width, int height, int x, int y)
{
this->width = width;
this->height = height;
this->leftUp = new Point(x, y);
}
Rectangle::Rectangle(const Rectangle& other):
Shape(other), // Step 1: father class member
width_(other.width_), // Step 2: non-pointer member
height_(other.height_)
{
if (other.leftUp_ != nullptr) // Step 3: Check whether other is nullptr
{
this->leftUp_ = new Point(*(other.leftUp_)); // Point copy constructor; other.leftUp_ is a (Point* ); so *(other.leftUp_) is a Point.
}
else
this->leftUp_= nullptr;
}