Topic:
For the following rectangle class implementation constructor, copy constructor, assignment operator, destructor.
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 ();
};
Analytical:
A constructor:
1. Use the initialization list as much as possible;
2. For the construction of the Leftup pointer, Leftup points to a point object, and the constructor needs to generate a new point object within the heap, pointing to the object with Leftup
inline Rectangle::rectangle (int width,intint x,int y): Width (width), Height (height), leftup ( x, y) {} // use initialization list as much as possible , including initialization of Leftup
Two-copy constructors:
1. Use the initialization list as much as possible
2. Note the copy construction of No to the parent class, by invoking the copy constructor of the parent class shape (other)
3. For the copy construction of the This->leftup, call the copy constructor of point to avoid extensive modifications when there is a change inside the point.
This New
4. For a discussion of whether other.leftup is empty, null pointers are not necessary to generate objects within the heap, and when Other.leftup is empty, This->leftup initially defaults to random values and assigns them to null pointers.
The complete copy constructor:
Inlinerectangle::rectangle (Constrectangle&Other ): Shape (Other), Width (other.width), height (other.height) {//Note the copy construction of the inherited object no, by calling the copy constructor of the parent class if(Other.leftup! = NULL) {//For a discussion of whether other.leftup is empty, null pointers do not need to generate objects within the heap This->leftup =NewPoint (*other.leftup);//call the copy constructor of point to avoid extensive modifications when there is a change inside the point. } Else{ This->leftup = NULL;//Leftup Initial default is a random value, to which he is assigned a null pointer. }}
Three-assignment operators
1. Assignment operators often need to first determine their own assignment to the situation, to avoid memory leaks
if ( This = = & other) {return * this;}
2. Call the assignment operator of the parent class to complete the assignment of the inherited part of the parent class by doing the following:
Shape::operator= (other); //
3. Need to discuss whether Leftup,other.leftup is empty
When the Other.leftup is empty, the This->leftup space is released directly, and it is assigned to empty.
When the other.leftup is not empty,
If the this->leftup is not empty, then directly assigns the contents of the Other->leftup point to the content that this->leftup points to;
If This->leftup is empty, create a new Point object
rectangle& Rectangle::operator= (Constrectangle&Other ) { if( This= = &other) {//assignment operators often need to first determine their own assignment to the situation, to avoid memory leaks return* This; } Shape::operator= (other);//Call the assignment operator of the parent class to complete the assignment of the inherited part of the parent class This->width =Other.width; This->height =Other.height; if(Other.leftup! =NULL) { if(Leftup! =NULL) { *leftup = *other.leftup;//you do not have to delete the Leftup and rebuild it, you can simply assign (dereference the pointer and invoke the assignment operator of the Point Class) } Else{Leftup=NewPoint (*other.leftup);//Leftup is empty, cannot be extracted, need to create a new object } } Else{ DeleteLeftup; This->leftup =NULL; } return* This;}
Four-destructor function
Rectangle:: ~Rectangle () { delete
Five overall code and other considerations
1.Rectangle assignment Constructors, construction order: The Father class, followed by the order declared in the subclass, regardless of the order in the initialization list.
2. Correctly distinguish between copy constructors and assignment operators.
A copy constructor is a constructor, that is, when a new object is created, an object exists and an object does not exist;
When the assignment operator is used, two objects must be present, so the question to be discussed is whether to self-assign and so on.
3 in the face of this type of problem method:
Forget the grammar and draw the memory model first
This example is
And then write the time to analyze whether the pointer is empty;
Copy construction is one side, there is no; the assignment operator is both on both sides;
If the combination of pointers is empty, the above considerations can be analyzed.
classshape{intno;};classpoint{Private: intx; inty; Public: Point (intXinty): x (x), Y (y) {}};classRectangle: Publicshape{intwidth; intheight; Point*Leftup; Public: Rectangle (intWidthintHeightintXinty); Rectangle (Constrectangle&Other ); Rectangle&operator=(Constrectangle&Other ); ~Rectangle ();}; Inline Rectangle::rectangle (intWidthintHeigt,intXintY): Width (width), height (height), Leftup (NewPoint (x, y)) {} //try to use the initialization list, including the initialization of the LeftupInlinerectangle::rectangle (Constrectangle&Other ): Shape (Other), Width (other.width), height (other.height) {//Note the copy construction of the inherited object no, by calling the copy constructor of the parent class if(Other.leftup! = NULL) {//For a discussion of whether other.leftup is empty, null pointers do not need to generate objects within the heap This->leftup =NewPoint (*other.leftup);//call the copy constructor of point to avoid extensive modifications when there is a change inside the point. } Else{ This->leftup = NULL;//Leftup Initial default is a random value, to which he is assigned a null pointer. }}rectangle& Rectangle::operator= (Constrectangle&Other ) { if( This= = &other) {//assignment operators often need to first determine their own assignment to the situation, to avoid memory leaks return* This; } Shape::operator= (other);//Call the assignment operator of the parent class to complete the assignment of the inherited part of the parent class This->width =Other.width; This->height =Other.height; if(Other.leftup! =NULL) { if(Leftup! =NULL) { *leftup = *other.leftup;//you do not have to delete the Leftup and rebuild it, you can simply assign (dereference the pointer and invoke the assignment operator of the Point Class) } Else{Leftup=NewPoint (*other.leftup);//Leftup is empty, cannot be extracted, need to create a new object } } Else{ DeleteLeftup; This->leftup =NULL; } return* This;} Rectangle::~Rectangle () {DeleteLeftup;}
Job Analysis C + + four function exercises