Classes and objects - properties of objects - constructors and destructors

**1. * * initialization and cleanup of objects are two very important security issues
An object or variable has no initial state, and the consequences of its use are unknown.
The same use of an object or variable, not cleaned up in time, will also cause certain security problems
**2. * * the constructor and destructor solve the above problems. These two functions will be automatically called by the compiler to complete the initialization and cleaning of objects.
Object initialization and cleanup is what the compiler forces us to do, so if we don't provide construction and destruction,
The compiler will provide that the compiler provides that the constructor and destructor provided by the compiler are empty implementations.

3. Constructor: the main function is to assign values to the member properties of the object when creating the object. The constructor is called by the compiler and does not need to be called manually.

Syntax: class name () {}

matters needing attention:
1. Constructor, no void or return value
2. The function name is the same as the class name
3. The constructor can have parameters, so overload can occur.
4. When the program calls the object, it will call the structure automatically, without manual call, and only once.

**4. Destructor: * * it is mainly used to automatically call the system before the object is destroyed to perform some cleaning work.

Syntax: ~ class name () {}

matters needing attention:
1. Destructor, no void or return value
2. The function name is the same as the class name, and the symbol is added before the name.~
3. Destructors can have no parameters, so overloading is not allowed.
4. Before the object is destroyed, the program will call the destructor automatically, without manual call, and only once.

#include "pch.h"
#include <iostream>



using namespace std;


//Constructor for initialization
class person {

public:

	person() {                  /*1.Constructor, no void and no return value
                                         2.Function name is the same as class name
                                            3.Constructors can have parameters, so overloading can occur
                                      4.When the program calls the object, it will automatically call the construction, without manual call, and only once
			  
			                         */

		cout << "Constructor number ah call" << endl;
	}

	//Destructor for cleanup
	~person() {

		cout << "Analysis function number ah call" << endl;


	}



};

void test01() {

	person p;//After the execution of test01, the data on the stack will be released, that is, the destructor will be called before releasing.
}

5. Classification and call of constructors
There are two types of classification:
According to the parameters, it can be divided into parametric structure and nonparametric structure.
By type: normal construction and copy construction
Three call modes:
bracketing
Display method
Implicit transformation

#include "pch.h"
#include <iostream>


using namespace std;

class person {

public:
//Constructor
	person() {

		cout << "non-parameter constructor " << endl;
	}
	person(int a) {
		age = a;

		cout << "Parametrical constructor" << endl;
	}
//copy constructor 
	person(const person &p) {
		//Copy all the attributes of the incoming person to me
		age = p.age;

		cout << "copy constructor " << endl;
	}

	~person() {

		cout << "Destructor" << endl;
	}

	int age;


};

void test() {
   //1. brackets
	person p2(10);//Parametric function call
	
	person p3(p2);//Copy constructor call

	//Note: 1. When calling the default constructor, do not add ()
	//           2. Do not use copy constructor to initialize anonymous object

	//2. display method
	person p1;
	person p2 = person(10);//Parametric structure
	person p3 = person(p2);//copy construction
	//2. Do not use copy constructor to initialize anonymous objects
	//The compiler will think that person (P3) = = person P3;

	//3. Implicit conversion

	person p4 = 10//Equivalent to person p4 = person(10)




}

6. Call time of copy constructor

In C + +, there are three situations when copy constructors are called
1. Initialize a new object with an already created object
2. Method of value transfer: transfer value to function parameter
3. Return local object by value

#include "pch.h"
#include <iostream>
using namespace std;

//1. Initialize a new object with an already created object
//2. Method of value transfer: transfer value to function parameter
//3. Return local object by value
class person {

public:
	person() {
		cout << "non-parameter constructor " << endl;

	}

	person(int age) {
		cout << "Parametrical constructor" << endl;
		m_age = age;

	}

	person(const person &p) {
		cout << "copy constructor " << endl;
		m_age = p.m_age;
	}

	~person() {
		cout << "Destructor" << endl;

	}

	int m_age;

};
//1. Initialize a new object with an already created object

void test01() {

	person p1(20);
	person p2(p1);


}



//2. Method of value transfer: transfer value to function parameter
void dowork(person p) {




}
void test02() {
	person p;
	dowork(p);

}



//3. Return local object by value
person dowork1() {
	person p1;
	return p1;
}
void test03() {

	person p = dowork1();



}

int main()
{
	test01();
	test02();
	test03();



}

. 7 constructor call rules
By default, the C + + compiler adds at least three functions to one class
1. Default constructor (no parameter, empty function body)
2. Default destructor (no parameter, function body is empty)
3. Default copy constructor (copy the value of the property)

The call rules are as follows:
1. If the user defines a parameter constructor, C + + does not provide a default no parameter construct, but it will provide a default copy construct.

2. If the user defines the copy constructor, C + + will not provide other constructors.

Keywords: P4

Added by snpo123 on Thu, 24 Oct 2019 15:44:49 +0300