Elementary C++--Classes and Objects (3) (Illustration)

Let’s talk about the constructor again

In our previous constructor, the compiler will give a suitable initial value to each member of the object through the constructor.But this cannot be called initialization, just assignment.;Because initialization can only be initialized once, and countless assignments can be made inside the constructor;

initialization list

The initialization list is a syntax feature used to initialize class member variables in the constructor.
Through the initialization list, member variables can be initialized to the specified value when the object is created, instead of assigning values ​​one by one in the constructor body.

grammar:The initialization list follows the constructor parameter list with a colon (:) and precedes the constructor body..
In the initialization list, member variables follow Declaration order listed, and usecomma is separated.
The initialization of each member variable consists of the member variable name followed by a parenthesesand initial values ​​or calling other constructors to complete.

test:

class A
{
    
    
private:
	int _a;
public:
    A(int a = 1)
		:_a(a)
	{
    
    
	}
	/*A(int* a)
		:_a(a)
	{
	}*/
	A(const A& a)
	{
    
    
		_a = a._a;
		cout << "A(const A& a)" << endl;
	}
	~A()
	{
    
    
		cout << "~A()" << endl;

	}
};

class Date
{
    
    
private:
	int _year;
	int _month;
	int _day;
	int& _def;
	const int _n;
public:
	Date(int year, int month = 1, int day = 1)
		:_year(year),
		_month(month),
		_day(day),
		_def(year),
		_n(10)
		
	{
    
    
	}
};
int main()
{
    
    
	Date d1=2023;
	Date d2 = (2023, 11, 4);

	Date d3 = {
    
     2023, 11, 4 };
	return 0;
}

Test const and reference members:

Date member variables

Insert image description here
Constructor of Date class
Insert image description here
defines an object and initializes it to (2023, 10, 23);
Insert image description here
Both reference and const variables must be initialized
Insert image description here

Standard writing:

Insert image description here
Insert image description here

When there is a custom type in the member variable:

Insert image description here
Insert image description here

Order of members:

Insert image description here

If the initialization list is not complete:
Insert image description here
Insert image description here

If the custom type does not have a default constructor:

Insert image description here

Insert image description here

For class objects with custom type member variables, you can:

Insert image description here
Insert image description here

Insert image description here

implicit type conversion

Insert image description here

Insert image description here
Insert image description here
Insert image description here
Insert image description here

Insert image description here
Insert image description here

Insert image description here

When multiple parameters

Insert image description here
Insert image description here
Insert image description here

explicit keyword

This is a modifier used to modify the constructor of a class.When a constructor is explicitly modified, it will be marked as an explicit constructor, which means that the constructor cannot perform implicit type conversion.

Insert image description here
Insert image description here

static member

Static class member:Static class members are member variables shared by all objects of the class. Once they are declared as static members, only one copy exists in memory, and that exists before any object is instantiated. Static class members must be initialized outside the class.
Static member function:Static member functions do not operate on a specific object, they do not have a this pointer, which can be accessed directly through the class name.

class A
{
    
    
public:
	A()
	{
    
    
		count++;
	} 
	A(const A& a1)
	{
    
    
		count++;
	}
	~A()
	{
    
    
		cout << "~A()" << endl;
	}
	//调取count成员变量的函数
	//静态成员函数,特点:没有this指针
	static int GetCount()
	{
    
    
		return count;
	}
private:
	//声明
	static int count;
};
//定义
int A::count = 0;

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Insert image description here

Friend class

Friend classes describe the special relationship between two classes;
When a class is a friend class of another class, the class can access the private members of the other class..
Declare a friend class by using the friend keyword in the class declaration.

class Time
{
    
    
public:
	friend class Date;
	//在Time中声明Date类,在Date中可以直接访问Time的成员变量
	Time(int hour=1,int mintue=0,int sec=0)
		:_hour(hour),
		_mintue(mintue),
		_sec(sec)
	{
    
    }
private:
	int _hour;
	int _mintue;
	int _sec;
};

class Date
{
    
    
private:
	int _year;
	int _month;
	int _day;
	Time _t;
public:
	Date(int year, int month, int day)
		:_year(year),
		_month(month),
		_day(day)
	{
    
    

	}
	void SetTime(int hour, int minute, int sec)
	{
    
    
		//可以直接访问私有的成员变量
		_t._hour = hour;
		_t._mintue = minute;
		_t._sec = sec;
	}
};

Insert image description here

inner class

An inner class is a class defined inside another class.The inner class can access all members of the outer class, including private members, but the outer class cannot directly access the members of the inner class.

class A
{
    
    
private:
	static int k;
	int h;
public:
	class B//B天生为A的友元
	{
    
    
	public:
		void f(const A& a)
		{
    
    
			cout << k << endl;//k是静态成员,可以直接使用
			cout << a.h << endl;//h是非静态成员,需要有明确的类对象
		}
	};


	~A()
	{
    
    
		cout << "~A()" << endl;
	}
};

int A::k = 1;

int main()
{
    
    
	A::B b;//B在A类中需要加上作用域符
	
	b.f(A());//A()是匿名对象
	return 0;
}

Insert image description here

anonymous object

Anonymous objects are objects without specific names that are created directly during use..
They are usually used to temporarily execute a method or operation, or as the return value of a method.

Syntax: ClassName();

Example:

class Foo()
{
    
    
public:
    void display()
    {
    
    
        cout<<"this is a Foo"<<endl;
    }
}
int main()
{
    
    
    Foo().display();
}

Some optimizations when copying functions

class A
{
    
    
private:
	int _a;
public:
	A(int a=1)
		:_a(a)
	{
    
    
		cout << "A(int a)" << endl;
	}
	A(const A& a)
	{
    
    
		cout << "A(const A& a)" << endl;
	}
	A& operator=(const A& a)
	{
    
    
		cout << "A& operator=(const A& a)" << endl;
		if (this != &a)
		{
    
    
			_a = a._a;
		}
		return *this;
	}
	~A()
	{
    
    
		cout << "~A()" << endl;
	}
};

 void func1(A aa)
{
    
    
	cout << "func()" << endl;
}

A func2()
{
    
    
	A aa;
	return aa;
}

A func3(A aa)
{
    
    
	
	return aa;
}

Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/m0_74068921/article/details/134353471