C + + constructor (Chapter 4)

Posted by PHPist on Sun, 09 Jan 2022 00:48:57 +0100

catalogue

Basic concepts of constructor

Function of constructor

Constructor form

Call timing of constructor

Default constructor

Implicitly generated constructor

"=default"

Example 4_ 1 Revision 1

Example 4_ 1 revision 2

Delegating constructors

review

Delegating constructors

copy constructor

Copy constructor definition

Implicit copy constructor

"=delete"

There are three situations in which the copy constructor is called

Example 4-2 complete program of point class

Destructor

Basic concepts of constructor

Function of constructor

 when the object is created, use specific values to construct the object and initialize the object to a specific initial state.

 for example:

 if you want to set the initial test time to 0:0:0 when constructing a Clock class object, you can set it through the constructor.

Constructor form

 the function name is the same as the class name;

 the return value type cannot be defined, nor can there be a return statement;

 there can be formal parameters or no formal parameters;

 it can be an inline function;

 it can be overloaded;

 default parameter values can be provided.

Call timing of constructor

 automatically called when the object is created

 for example:

Clock myClock(0,0,0);

Default constructor

 constructors that do not require arguments can be called

• constructor with empty parameter table

 constructor with default values for all parameters

 the following two are the default constructors. If they appear in the class at the same time, a compilation error will be generated:

Clock(); 
Clock(int newH=0,int newM=0,int newS=0); 

Implicitly generated constructor

 if the constructor is not defined in the program, the compiler will automatically generate a default constructor when necessary

 the parameter list is empty, and the initial value is not set for the data member;

 if the initial value of the member is defined in the class, the initial value defined in the inner class is used;

 if the initial value in the class is not defined, it is initialized by default;

 the default initialization value of basic type data is uncertain.

"=default"

 if the constructor is not defined in the program, the compiler will no longer implicitly generate the default constructor by default. If you still want the compiler to implicitly generate the default constructor at this time, you can use "= Default".

 for example

class Clock {
public:
	Clock() = default; //Instructs the compiler to provide a default constructor
	Clock(int newH, int newM, int newS); //Constructor
private:
	int hour, minute, second;
}

Constructor example (1)

Example 4_ 1 Revision 1

//Class definition

class Clock {

public:

     Clock(int newH,int newM,int newS);//Constructor

     void setTime(int newH, int newM, int newS);

     void showTime();

private:

     int hour, minute, second;

};


//Implementation of constructor:

Clock::Clock(int newH,int newM,int newS): hour(newH),minute(newM),  second(newS) {

     }

//The implementation of other functions is the same as example 4_ one


int main() {

  Clock c(0,0,0); //Call constructor automatically

  c.showTime();

     return 0;

}

Example 4_ 1 revision 2

class Clock {

public:

       Clock(int newH, int newM, int newS); //Constructor

       Clock(); //Default constructor 

       void setTime(int newH, int newM, int newS);

       void showTime();

private:

       int hour, minute, second;

};

Clock::Clock(): hour(0),minute(0),second(0) { }//Default constructor 

//The implementation of other functions is the same as before


int main() {

    Clock c1(0, 0, 0);       //Call a constructor with parameters

    Clock c2;         //Call a parameterless constructor

   ......

}

Delegating constructors

Class often has multiple constructors, but the parameter table and initialization list are different, and their initialization algorithms are the same. At this time, in order to avoid code duplication, you can use the delegate constructor.

review

Clock Class:
Clock(int newH, int newM, int newS) : hour(newH), minute(newM),
second(newS) { //Constructor
}
Clock::Clock() : hour(0), minute(0), second(0) { }//Default constructor 

Delegating constructors

 the delegate constructor uses other constructors of the class to perform the initialization process

 for example:

Clock(int newH, int newM, int newS): hour(newH),minute(newM), 
second(newS){
}
Clock(): Clock(0, 0, 0) { }

copy constructor

Copy constructor definition

Copy constructor is a special constructor whose formal parameters are object references of this class. The function is to initialize a new object of the same type with an existing object.

class Class name {
public :
 Class name (formal parameter);//Constructor
 Class name( const Class name &Object name);//copy constructor 
 // ...
};
Class name::Class( const Class name &Object name)//Implementation of copy constructor
{ Function body }

Implicit copy constructor

 if the programmer does not declare a copy initialization constructor for the class, the compiler generates an implicit copy constructor.  the function of this constructor is to initialize the corresponding data member of the object to be created with the value of each data member of the object as the initial value.

"=delete"

 if you do not want the object to be copied

• C++98 practice: declare the copy constructor as private, and do not provide the implementation of the function.

• C++11 practice: use "= delete" to indicate that the compiler does not generate the default copy constructor.

 example:

class Point { //Definition of Point class
public:
Point(int xx=0, int yy=0) { x = xx; y = yy; } //Constructor, inline
Point(const Point& p) =delete; //Indicates that the compiler does not generate a default copy constructor
private:
int x, y; //Private data
};

There are three situations in which the copy constructor is called

 when defining an object, take another object of this class as the initial value, and copy construction occurs

 if the formal parameter of the function is an object of a class, when calling the function, the formal parameter object will be initialized with the actual parameter object, and a copy structure will occur;  if the return value of the function is an object of a class, when the function returns to the calling function after execution, a temporary nameless object will be initialized with the object in the return statement and passed to the calling function. At this time, a copy structure occurs.

• in this case, unnecessary duplication can also be avoided by moving the structure (described in Chapter 6)

Example 4-2 complete program of point class

#include <iostream>
using namespace std;

class Point { //Definition of Point class
public:
	Point(int xx = 0, int yy = 0) { x = xx; y = yy; } //Constructor, inline
	Point(const Point& p); //copy constructor 
	void setX(int xx) { x = xx; }
	void setY(int yy) { y = yy; }
	int getX() const { return x; } //Constant function (Chapter 5)
	int getY() const { return y; } //Constant function (Chapter 5)
private:
	int x, y; //Private data
};
//Implementation of copy constructor
Point::Point(const Point& p) {
	x = p.x;
	y = p.y;
	cout << "Calling the copy constructor " << endl;
}
//The formal parameter is a Point class object
void fun1(Point p)
{
	cout << p.getX() << endl;
}
//The return value is a Point class object
Point fun2()
{
	Point a(1, 2);
	return a;
}
int main() {
	Point a(4, 5);
	Point b(a); //Initialize b with a.
	cout << b.getX() << endl;
	fun1(b); //Object b as an argument to fun1
	b = fun2(); //The return value of the function is a class object
	cout << b.getX() << endl;
	return 0;
}

Destructor

  • Complete some cleanup before the object is deleted.

  • At the end of the object's lifetime, the system automatically calls it, and then releases the space to which the object belongs.

  • If the destructor is not declared in the program, the compiler will automatically generate a default destructor with an empty function body.

  • Examples of constructors and destructors

  • #include <iostream>
    
    using namespace std;
    class Point {     
    public:
      Point(int xx,int yy);
      ~Point();
      //... Other function prototypes
    private:
      int x, y;
    };

Topics: C++ Back-end