Understanding of C++ Classes and Objects (2)

In the previous section, we talked about the concept of classes and objects, but related to class initialization.
The initialization of a class actually refers to the assignment of class members.

1. Several methods of class member assignment

1. Direct initialization

In the structure of the class, there is a premise to directly initialize the members: the premise is that all data members are public and no constructor is declared.
Some people may want to ask why the constructor cannot be declared. Specifically, we can verify it through the code.

The data member is defined as public, and the constructor is declared-the code block has no error prompt, and the compilation error is reported.
#include<iostream>
using namespace std;
class  CA
{
    
    
public:
	 CA();//代码块,无错误提示,编译报错。
	~ CA();
public:
	int m_num;

};


int main()
{
    
    
	CA ca;
	ca.m_num = 1;//直接初始化
	return 0;
}

operation result:
Insert picture description here

The data member is defined as public, and there is no constructor-the code block has no error prompt and the compilation is successful.
#include<iostream>
using namespace std;
class  CA
{
    
    
public:
	//成员函数;
public:
	int m_num;

};


int main()
{
    
    
	CA ca;
	ca.m_num = 1;//直接初始化
	return 0;
}

operation result:
Insert picture description here

Note: But we usually don't recommend to initialize the data members directly in the structure of the class, more often through the constructor, public member functions, and member initialization lists.

2. Constructor initialization

#include<iostream>
using namespace std;
class CA
{
    
    
public:
//构造函数
	CA(int a)
	{
    
    
		this->m_num = a;
	}
//实现了构造函数后,一定要记得实现析构函数,否则报错
	~CA()
	{
    
    

	}
//公有的函数-验证是否成功初始化
	int Getnum()
	{
    
    
		return this->m_num;
	}
private:
	int m_num;

};

//程序入口
int main()
{
    
    
	CA ca(1);//创建时,在类对象后加括号,括号里面加实参即可实现构造函数初始化
	cout << ca.Getnum() << endl;
	return 0;
}

operation result:
Insert picture description here
Note: After implementing the constructor, you must remember to implement the destructor, otherwise an error will be reported. The logic of the destructor can be empty, but not just a declaration

3. Initialization of member initialization list

#include<iostream>
using namespace std;
class CA
{
    
    
public:
	//第一种:成员初始化列表进行初始化
	CA() :m_num(1)
	{
    
    }
	//第二种:成员初始化列表进行初始化
	CA(int a) :m_num(a)
	{
    
    

	}
	~CA()
	{
    
    

	}
	int Getnum()
	{
    
    
		return this->m_num;
	}
private:
	int m_num;

};


int main()
{
    
    
	CA ca;
	cout << ca.Getnum() << endl;
	CA ca1(6);
	cout << ca1.Getnum() << endl;
	return 0;
}

operation result:
Insert picture description here

4. Public function initialization

#include<iostream>
using namespace std;
class CA
{
    
    
public:
	CA()
	{
    
    

	}
	~CA()
	{
    
    

	}
	//通过公有的成员函数来对成员进行初始化
	void Setnum(int a)
	{
    
    
		this->m_num = a;
	}
	//获取成员的值并返回--验证是否成功进行初始化
	int Getnum()
	{
    
    
		return this->m_num;
	}
private:
	int m_num;

};


int main()
{
    
    
	CA ca;
	ca.Setnum(8);//类对象调用公有成员函数,对成员进行初始化
	cout << ca.Getnum() << endl;//类对象调用Get方法,验证是否进行初始化
	return 0;
}

operation result:
Insert picture description here

Regarding the learning of C++, welcome everyone to follow me, comment on my articles, like, bookmark, follow me not to get lost. Netizens are also welcome to discuss C++ dry goods with me. The knowledge of C++ is much more than that, we will analyze and explain one by one. If netizens want me to explain, they can comment and leave a message, and I will do my best to explain it to everyone.

: : ProMer_Wang

Link: https://blog.csdn.net/qq_43801020/article/details/106880711

This article is the original article of ProMer_Wang, the copyright belongs to the author, please indicate the source of the original text for reprinting, welcome to reprint!

Guess you like

Origin blog.csdn.net/qq_43801020/article/details/106880711