2021-02-09 Check-in and learn C++ for the ninth day


One, class and object

1. Package

For details, please refer to the eighth day of learning content

2. Initialization and cleanup of objects

Object-oriented in C++ comes from life, each object will also have initial settings and settings for cleaning up data before the object is destroyed

(1) Constructor and destructor

Object ofinitializationwithClean upAre also two very important safety issues

  • An object or variable has no initial state, and the consequences of its use are unknown
  • Similarly, after using an object or variable, if it is not cleaned up in time, it will also cause certain security problems

Constructor: It is mainly used to assign values ​​to the member properties of the object when the object is created. The constructor is automatically invoked by the compiler and does not need to be manually invoked

Destructor: The main function is to automatically call the system before the object is destroyed to perform some cleanup work

Both construction and destruction are necessary. If you don’t write it yourself, the compiler will automatically add it, but the system provides an empty implementation

Constructor syntax :类名 () { }

  1. Constructor, no return value nor write void
  2. The function name is the same as the class name
  3. The constructor can have parameters, so overloading can occur
  4. The program will automatically call the structure when calling the object, there is no need to call it manually, and it will only be called once

Destructor syntax :~类名() { }

  1. Destructor, no return value nor write void
  2. The function name is the same as the class name, with the symbol ~ in front of the name
  3. The destructor cannot have parameters, so overloading cannot occur
  4. The program isBefore the object is destroyedThe destructor will be called automatically, no manual call is required, and it will only be called once

Comparative example

#include<iostream>
using namespace std;

//创建类
class person
{
    
    
public:
	//1、构造函数
	person()
	{
    
    
		cout << "person构造函数的调用" << endl;
	}
	//2、析构函数
	~person()
	{
    
    
		cout << "person析构函数的调用" << endl;
	}
};

void test01()
{
    
    
	person p;
}
int main()
{
    
    
	test01();

	system("pause");
	return 0;
}

Insert picture description here

#include<iostream>
using namespace std;

//创建类
class person
{
    
    
public:
	//1、构造函数
	person()
	{
    
    
		cout << "person构造函数的调用" << endl;
	}
	//2、析构函数
	~person()
	{
    
    
		cout << "person析构函数的调用" << endl;
	}
};

void test01()
{
    
    
	person p;
}
int main()
{
    
    
	//test01();
	person p;
	system("pause");
	return 0;
}

Insert picture description here

(2) Classification and call of constructor

Two classification methods:

  1. Classified by parameters: parameterized structure and non-parameterized structure
  2. Classified by type: ordinary construction and copy construction

Three calling methods:

  1. Bracketing
  2. Display method
  3. Implicit conversion

Precautions
== 1. When calling the default constructor, do not add (), the compiler will think this is a function declaration ==
== 2. Do not use the copy constructor to initialize anonymous objects ==

Sample code

#include<iostream>
using namespace std;

//创建类
class person
{
    
    
public:
	//1、构造函数
	person()
	{
    
    
		cout << "person无参构造函数的调用" << endl;
	}

	person(int a)
	{
    
    
		age = a;
		cout << "person有参构造函数的调用" << endl;
	}

	//拷贝构造函数
	person(const person &p)  //拷贝构造函数写法
	{
    
    
		//将传入的人身上的所有属性,拷贝到此函数内人的身上
		age = p.age;
		cout << "person拷贝构造函数的调用" << endl;
	}
	//2、析构函数
	~person()
	{
    
    
		cout << "person析构函数的调用" << endl;
	}

	int age;
};

//调用
void test01()
{
    
    
	//1、括号法
	cout << "1、括号法" << endl;
	person p1;//默认构造函数的调用
	person p2(10);//有参构造函数
	person p3(p2);//拷贝函数
	cout << "\n";

	//2、显示法
	cout << "2、显示法" << endl;
	person p4;
	person p5 = person(10);
	person p6 = person(p2);

	cout << "\n";
	cout << "匿名对象,执行结束后立即释放" << endl;
	person(10);//匿名对象  当前行执行结束后,系统会立即回收掉匿名对象
	cout << "123456" << endl;
	cout << "\n";

	//3、隐式转换法
	cout << "3、隐式转换法" << endl;
	person p7 = 10;//相当于写了person p6=person(10);//有参构造
	person p8 = p4;//拷贝构造
}
int main()
{
    
    
	test01();

	system("pause");
	return 0;
}

Insert picture description here

(3) When to call the copy constructor

  • Use an already created object to initialize a new object
  • Pass value to function parameter by value
  • Return local objects by value

(4) Constructor call rules

By default, the C++ compiler adds at least 3 functions to a class

  1. Default constructor (no parameters, the function body is empty)
  2. Default destructor (no parameters, the function body is empty)
  3. The default copy constructor, copy the value of the attribute

Constructor call rules:

  • If the user defines a parameter constructor, C++ no longer provides a default no-parameter construction, but will provide a default copy construction
  • If the user defines a copy constructor, C++ will not provide other constructors

[Note] The learning course is-Dark Horse Program C++ Tutorial

Guess you like

Origin blog.csdn.net/qq_42616280/article/details/113369032