(Study Note 1) Class and Object Detailed Explanation (C++)

insert image description here

1. Class definition

A class is a syntax unique to an object-oriented language. In an object-oriented language, all variables and functions are collectively called objects, and there can be objects in a class.

grammar

class ClassName
{
    
    
// 类体:由成员函数和成员变量组成
	void Init();
	
	int a;
};

//class为定义类的关键字,ClassName为类名,{}中为类的主题,注意类定义结束时后面分号不能省略。

//类中的内容称为类的成员:类的变量称为类的属性或成员变量;类中的函数称为类的方法或者成员函数。

How the class is defined

  1. The declaration and definition are all placed in the class body (note: if the member function is defined in the class, the compiler may call it)


// 成员函数声明定义都在类中
class xty
{
    
    
public:
	void Print()
	{
    
    
		cout << "defined in class" << endl;
	}


private:
	int a;
	int b;
};

  1. The declaration of the class is placed in the .h file, and the member function is defined in the .cpp file. Note: before defining the name of the member function, you need to add the class name::


// .h文件
class xty2
{
    
    
public:
	//声明
	void Print();


private:
	int a;
	int b;
};




// .cpp定义 

void xty2::Print()
{
    
    
	cout << "defined in class" << endl;
}

In the practice of engineering projects, it is recommended to deal with declarations and definitions separately.
There is a statement indicating that the syntax is correct, and it can be compiled; if there is a definition, the address can be found according to the symbol table when linking, and it can be linked

2. Class access qualifiers

Access qualifier: 1.public (public); 2.protected (protected); 3.private (private)

2.1 Features of access qualifiers

  1. Public modified members can be directly accessed outside the class.
  2. Protected and private modified members cannot be directly accessed outside the class
  3. Access rights are scoped from the occurrence of this access qualifier until the next occurrence of the access qualifier
  4. If there is no access qualifier, the scope is }, which is the end of the class
  5. The default access permission of class is private, and struct is public (because struct is compatible with C)
class Xty
{
    
    
public:
	void Print()
	{
    
    
		cout << "defined in class" << endl;
	}

private:
	int a = 10;
	int b;
	int c;
};

int main()
{
    
    
	//声明xp对象
	Xty xp;
	xp.a = 10;   //报错,因为a 为私有,所以对xp.a不可访问  
	int z = xp.a;  //报错,因为a 为私有,所以对xp.a不可访问
	xp.Print();   //可以运行,Print成员函数为公有,因此可以在类外面访问
	return 0;
}

The purpose of this is to set member variables as private and member functions as public to prevent external members from arbitrarily modifying member variables.

2.2 The difference between struct and class

  1. First of all, struct is a keyword of C language. In order to be compatible with C syntax, C++ makes the default permission of struct class public, so that it can be accessed through objects outside the class; while class is private by default, and cannot be accessed casually outside the class.

3. Three major characteristics

C++ has three major features: inheritance, encapsulation, and polymorphism.

3.1 Package

Organically combine the data and the method of operating the data, hide the attributes and implementation details of the object, and only interact with the object through the interface exposed to the outside world. Encapsulation is essentially a kind of management, allowing users to use classes more safely and conveniently.

4. Class scope

A class defines a new scope, and all members of the class are in the scope of the class. When defining members outside a class, you need to use the :: scope qualifier.

class Xty
{
    
    
public:
	void Print();

private:
	int a = 10;
	int b;
	int c;
};

//在类外面定义成员时,需加上域名,来说明Print是属于Xty这个域里面的
void Xty::Print()
{
    
    
	cout << "hello world!" << endl;
}

5. Instantiation of the class

Creating a class from a type is called instantiation of the class.

  1. A class describes an object and is a model. The defined class does not allocate actual memory space.
  2. A class can instantiate (create) multiple objects, and the objects occupy the actual physical space and store class member variables
class Xty
{
    
    
public:
	void Print();

	int a;
	int b;
	int c;
};

//在类外面定义的成员函数
void Xty::Print()
{
    
    
	cout << "hello world!" << endl;
}

int main()
{
    
    
	//Xty的类创建出xp的对象
	Xty xp;
	//是给xp的对象赋值,而不是给类赋值
	xp.a = 10;
	xp.b = 20;
	xp.Print();
	return 0;
}

6. Class object model

How does the compiler store objects?
Let's look at a code example first:

//类中既有成员变量又有成员函数
class X1
{
    
    
public:
	void Print()
	{
    
    
		cout << "hello world" << endl;
	}
private:
	char a;
};

//类中只有成员变量
class X2
{
    
    
private:
	int a;
};

//类中什么也没有
class X3
{
    
    
};


int main()
{
    
    	
	X1 x1;
	X2 x2;
	X3 x3;
	cout<<"x1 : " << sizeof(x1) << endl; // 1
	cout<<"x2 : " << sizeof(x2) << endl; // 4
	cout<<"x1 : " << sizeof(x3) << endl; // 1
	return 0;
}

It can be seen from observation that the member variables of the object occupy the storage space, but the member functions do not occupy the space of the object, and when there is nothing in the class object, 1B is used to occupy the space to indicate the existence of the object.


In fact: member variables and member functions are stored separately, only member variables occupy the storage space of the object, and the member functions of all objects of this class are stored in the common code area, and all objects share the code.

insert image description here

7. this pointer

First look at the following piece of code:

class Time
{
    
    
public:
	void Init(int h, int min, int s)
	{
    
    
		_h = h;
		_min = min;
		_s = s;
	}

	void Print()
	{
    
    
		cout << _h << ":" << _min << ":" << _s << endl;
	}

private:
	int _h;
	int _min;
	int _s;
};


int main()
{
    
    
	Time t1;
	Time t2;

	t1.Init(10, 20, 3);
	t2.Init(5, 10, 1);

	t1.Print(); // 10:20:3
	t2.Print(); // 5:10:1
}

Because the member function is in the public area, and the member function has the Print function and the Init function, how does C++ know when calling the t1.Init() function, how does it know whether I want to use the t1 object or the t2 object?
Answer: The C++ compiler adds a hidden pointer parameter to each "non-static member function", making the pointer point to the current object (the object that calls the function when the function is running), and all "member variables" in the function body Fuck, all are accessed through this pointer. It's just that all operations are transparent to the user, that is, the user does not need to pass it, and the compiler automatically completes it. This pointer is the "this pointer".

7.1 Characteristics of this pointer

  1. The type of this pointer: the class type *const, in the member function, cannot assign a value to this pointer
  2. Can only be used inside member functions.
  3. The essence of the this pointer is the formal parameter of the member function. When the object calls the member function, the object address is passed as the actual parameter to the this formal parameter. So the this pointer is not stored in the object.
  4. The this pointer is the first hidden pointer parameter of the "member function". Generally, it is automatically passed by the compiler through the ecx register and does not need to be passed by the user.

The this pointer exists, but it is automatically done by the compiler for us. We can use it or not


Actual parameters and formal parameters cannot be explicitly passed and accepted this pointer, and an error will be reported (cannot be compiled). As shown in the figure below:
insert image description here
But the this pointer can be used inside the member function (can be compiled), as shown in the figure below:
insert image description here

7.2 Issues related to this pointer

  1. Where does this pointer exist? The this pointer is a formal parameter, and the formal parameter is stored in the stack area.
  2. Can this pointer be null? The this pointer can be empty. When accessing a member function, it will directly go to the public area to find the function, and will not find the function through dereference, so it will not dereference the null pointer, so it can.

Guess you like

Origin blog.csdn.net/weixin_45153969/article/details/131690598