The 13th day of C + + Learning (object initialization and cleaning)

Keywords: C++

1. Object initialization (constructor) and cleanup (destructor)
(1) Constructor: it is mainly used to assign values to the member properties of the object when creating the object. The constructor is automatically called by the compiler.
(2) Destructor: it is mainly used to automatically call the system before the object is destroyed and perform some cleaning work.
Note: the initialization and cleanup of objects are what the compiler forces us to do. Therefore, if we do not provide these two functions, the compiler will provide that the two functions of the compiler are empty implementations.

2. Usage of these two functions
(1) Constructor syntax: class name () {}
a. The constructor does not return a value and does not write void
b. The function name is the same as the class name
c. Constructors can have arguments, so overloading can occur
d. When the program calls the object, it will automatically call the construct without manual call, and only call it once
(2) Destructor syntax: ~ class name () {}
a. The destructor does not return a value and does not write void
b. The function name is the same as the class name, and the function name is preceded by~
c. Constructors cannot have parameters, so overloading cannot occur
d. When the program calls the object, it will automatically call the destructor without manual call, and only call it once

#include <iostream>
using namespace std;

class Person
{
public:
	//1. Constructor for initialization
	Person()
	{
		cout << "Person Call to constructor" << endl;
	}
	//2. Destructor to clean up
	~Person()
	{
		cout << "Person Call to destructor" << endl;
	}
};

void test01()
{
	Person p;//After test01 executes the data on the stack, release this function
}

int main()
{
	test01();
	Person p;//The destructor will not be executed here. The destructor will be executed only after the main function is executed

	system("pause");
	return 0;
}

3. Classification and calling of constructors
(1) Two types of classification: divided into parametric structure and nonparametric structure by parameters, and classified by type (common structure and copy structure)
(2) Calling method: bracket method, display method and implicit conversion method

#include<iostream>
using namespace std;

class Person
{
public:
	//Parameterless (default) constructor
	Person()
	{
		cout << "Person Call to parameterless constructor" << endl;
	}
	//Parameterized constructor
	Person(int a)
	{
		age = a;
		cout << "Person Call with parameter constructor" << endl;
	}
	//copy constructor 
	Person(const Person &p)
	{
		//Copy all the attributes of this person passed in to the current object function
		age = p.age;
		cout << "Person Call to copy constructor" << endl;
	}
	//Destructor
	~Person()
	{
		cout << "Person Call to destructor" << endl;
	}
public:
	int age;
};
//call
void test01()
{
	//bracketing
	//Note 1: do not add () when calling the default constructor. If parentheses are added, the compiler may default it to function declaration
	/*Person p1;
	Person p2(10);
	Person p3(p2);
	cout << "p2 The age of is: "< P2. Age < < endl; / / 10
	cout << "p3 The age of is: "< P3. Age < < endl// ten*/
	
	//Display method
	//Person p1;
	Person p2 = Person(10);//The individual Person(10) is an anonymous object, and p2 gives him a name
	cout << "aaaa" << endl;//This sentence is executed after the constructor with parameters is called, and then the destructor is called
	//Person p3 = Person(p2);
	//Person(10);// Features: after the execution of the current line, the system will immediately recycle the anonymous object
	//cout << "aaa" << endl;// This sentence will be printed after the anonymous object is recycled
	//Note 2: do not use the copy constructor to initialize anonymous objects
	//Person(p2);// The compiler will assume that Person(p2) == Person p2, which is repeatedly defined
	
	//Implicit transformation method
	Person p4 = 10;//The compiler defaults to Person p4 = Person(10);
	Person p5 = p4;
}

int main()
{
	test01();

	system("pause");
	return 0;
}

4. Call timing of copy constructor
The copy constructor is called in all three ways

#include <iostream>
using namespace std;

class Person
{
public :
	Person()
	{
		cout << "Person Default constructor call" << endl;

	}
	Person(int age)
	{
		cout << "Person Parameterized constructor call" << endl;
		m_age = age;
	}
	Person(const Person &p)
	{
		cout << "Person Copy constructor call" << endl;
		m_age = p.m_age;
	}
	~Person()
	{
		cout << "Person Destructor call" << endl;
	}
public:
	int m_age;
};

//1. Initialize an object with an already created object
void test01()
{
	Person p1(20);
	Person p2(p1);
	cout << "p2 Your age is:" << p2.m_age << endl;
}

//2. The method of value transfer is to transfer values to function parameters
void dowork(Person p)
{
	
}
void test02()
{
	Person p;
	dowork(p);
}

//3. Local object returned by value transfer
Person dowork2()
{
	Person p1;
	cout << (int *)&p1 << endl;
	return p1;
}
void test03()
{
	Person p = dowork2();
	cout << (int *)&p << endl;
}

int main()
{
	//test01();
	//test02();
	test03();
	system("pause");
	return 0;
}

5. Constructor calling rules
(1) When creating objects, the C + + compiler provides three constructors by default
(2) If the user defines a parametric constructor, the C + + compiler will not provide a default parameterless construct, but a default copy construct
(3) If the user defines a copy constructor, the c + + compiler will not provide other constructors (the first two constructors cannot be called at this time)

Posted by Sj0wKOoMel on Sun, 19 Sep 2021 04:56:44 -0700