Class and object end experience

1, Constructor (Advanced)

When a class instantiates an object, the compiler will automatically call the constructor of the class and give an initial value to each member variable in the object.

1.1 initial value

class Time{
public:
	Time(int hour,int minute,int second)
	{
		_hour = hour;
		_minute = minute;
		_second = second;
	}
private:
	int _hour;
	int _minute;
	int _second;
};

The constructor body only assigns the initial value to each member variable, not the initialization of the member variable, because the initialization can only be performed once, but the constructor body can be assigned multiple times.

1.2 initialization list

1.2.1 where is the initialization list

Then we need to talk about where member variables are initialized. As follows:

class Time{
public:
	Time()
	//Where member variables are initialized
	{
	}
private:
	int _hour;
	int _minute;
	int _second;
};

1.2.2 format of initialization list


Initialization list: start with a colon, then separate each member variable with a comma, and put the initial value in parentheses after each member variable.

1.2.3 precautions

1. Each member variable can only appear once in the initialization list
2. All member variables containing custom / reference / const types in the class must be initialized in the initialization list

	Time a;             //Custom type variable       
	int &_minute;       //Reference variable
	const int _second;  //const variable

3. The order in which member variables are declared in the class is the order in which they are initialized, regardless of the initialization order in the initialization list

In initialization_ Hour_ minute is not initialized, so it is still a random value. That is, in_ The hour initialization phase assigns a random value to_ hour.

1.3 explicit keyword

For the constructor of a single parameter, it has the ability of type conversion

In order to prohibit this implicit type conversion, the explicit keyword is used here

	Time(int hour)
		:_hour(hour)
	{}
	explicit Time(int hour)
		:_hour(hour)
	{}

In this way, the single parameter implicit conversion of the Time class object can be prohibited. It should be noted that the explicit keyword can only prohibit the implicit conversion of the single parameter constructor (the implicit conversion will not occur when the constructor parameter is greater than or equal to 2)

2, Static member function of class

2.1 static members

Static member of class: class member decorated with static
Static function of class: member function of class modified by static
Static member variable of class: member variable of class modified by static (initialization outside the class is required)

class People{
public:
	static void fun()   //Static member function
	{
		cout << _age << endl;
	}
private:
	static int _age;   //Static member variable
};
int People::_age = 0;   //Initializing static member variables outside the class

characteristic:
1. A static member of a class that is shared by all objects of the class.
2. The static member function does not have this pointer
3. Access method of class static member: Class Name: static member or object Static member (note access qualifier)
4. Non static member functions can call static member functions
5. Static member functions cannot call non static member functions.

2.2 default value of member variable

C++11 supports assignment in the declaration stage of class non static member variables. Here is actually the default value of declared member variables

class People{
public:
private:
	static int _age;   //Static member variable
	string _name = "coutinho";   //Give default value
};
int People::_age = 0;   //Static member variables can only be initialized outside the class

3, Friends and inner classes

3.1 friend function

Friend functions can directly access the private members of a class. They are ordinary functions defined outside the class, but to declare them inside the class, the keyword friend is required.
Overloaded C + + standard I / O:

class People{
	friend ostream& operator<<(ostream& _cout, const People& data);
	friend istream& operator>>(istream& _cin, People& data);
private:
	int _age; 
	string _name; 
};
ostream& operator<<(ostream& _cout, const People& data){
	_cout << data._age << "-" << data._name;
	return _cout;
}
istream& operator>>(istream& _cin, People& data){
	_cin >> data._age;
	_cin >> data._name;
	return _cin;
}

be careful:
1. Friend functions can access all members of the class, but they are not member functions of the class
2. Friend functions can be declared anywhere in the class and are not limited by access qualifiers
3. A function can implement friend functions of multiple classes

3.2 friend category

1. The friend relationship is one-way and not exchangeable
2. Friend relationship cannot be transferred

3.3 internal class

As the name suggests, it is to define another class in one class.
be careful:
1. The inner class is the friend of the outer class (the private members of the outer class can be accessed directly in the inner class)
2. The internal class is an independent class and cannot be called through external class objects
3. The inner class can be defined between any access qualifiers of the outer class.
4. The size of the external class is independent of the size of the internal class

Tags: C++ Class

Posted by SieRobin on Sun, 01 May 2022 17:23:21 +0300