In-depth explanation of classes and objects (1)

Table of contents

1. Initial understanding of classes and objects

1.1 The difference between process-oriented and object-oriented

1.2 Introduction of class

 Definitions within 1.3

1.4 Class access qualifiers and encapsulation

1.4.1 Access Qualifiers

1.4.2 Packaging

1.5 Scope of classes

1.6 Class Object Model

1.6.1 How to calculate the class object size


1. Initial understanding of classes and objects

1.1 The difference between process-oriented and object-oriented

The C language is process-oriented , focusing on the process , analyzing the steps to solve the problem, and solving the problem step by step through function calls.
C++ is based on object-oriented , focusing on objects , splitting one thing into different objects, and relying on the interaction between objects to complete.

Take a simple example:

The design of a simple takeaway system
is process-oriented: focus on implementing the processes of placing orders, receiving orders, and delivering food. Reflected at the code level--method/function
object-oriented: focus on implementing class objects and the relationship between class objects, users, merchants, riders and the relationship between them. Reflected at the code level--class design and the relationship between classes
 

1.2 Introduction of class

In C language, we have already learned the custom type of structure , let's use structure to define a student type:

struct Student
{
	char name[10];
	int age;
	int id;
};

int main()
{
	struct Student s1; //兼容C
	Student s2;        //升级到类,Student为类名,也是类型
	return 0;
}

 Since C++ is compatible with C syntax, struct is also upgraded to a class in C++. In C language, we cannot initialize member variables after defining structure variables. However, in C++, we can in the structure Define member functions, and at the same time, you can use member functions to initialize structure variables and other operations. Let's see how it works:

struct Student
{
	//成员变量
	char _name[10];
	int _age;
	int _id;//前面加_是为了和后面函数的形参进行区分
	//成员方法
	//初始化成员变量
	void Init(const char* name, int age, int id)
	{
		strcpy(_name, name);
		_age = age;
		_id = id;
	}
	//打印成员变量
	void Print()
	{
		cout << _name << endl;
		cout << _age << endl;
		cout << _id << endl;
	}
};

int main()
{
	struct Student s1; //兼容C
	Student s2;        //升级到类,Student为类名,也是类型

	s1.Init("张三", 18, 1);
	s2.Init("李四", 19, 2);

	s1.Print();
	s2.Print();
	return 0;
}

 The result of the printout:

 Definitions within 1.3

class is the keyword to define a class:

class className
{
	//类体:由成员变量和成员函数组成
};

 So, can the keyword struct in the above structure code be changed to class? Just try it out:

 Obviously, the compiler reported an error to us. Don't worry, I will introduce the reasons for the error one by one below:

First of all, object-oriented has three major characteristics: encapsulation, inheritance, polymorphism

What we need to understand first is encapsulation:

1. In object-oriented, data and methods are put together in the class

2. C++ implements encapsulation through access qualifiers

The following are the access qualifiers

1.4 Class access qualifiers and encapsulation

1.4.1 Access Qualifiers

The way of C++ to achieve encapsulation: use the class to combine the properties and methods of the object to make the object more perfect, and selectively provide its interface to external users through access rights.

 Access qualifier description: 

1. Public modified members can be directly accessed outside the class
2. Protected and private modified members cannot be directly accessed outside the class (here protected and private are similar)
3. The scope of access rights emerges from this access qualifier The position starts until the next access qualifier appears
4. If there is no access qualifier behind, the scope ends at the end of the class.
5. The default access right of class is private (struct public because struct is compatible with C)
 

The fifth point here is that the default access right of the class is private, which is why the above code compiler reports an error. If we set the member function as public (or set both the member variable and the member function as public), the compiler will not An error was reported:

Therefore, when we define a class, try to clearly define the access qualifier instead of using the default qualifier of class/struct, so that when others read our code, they can know the member attributes at a glance.

1.4.2 Packaging

 
Encapsulation is a kind of strict management, and non-encapsulation is a kind of free management

In C++, data and methods are encapsulated into classes, the members that can be accessed by you are defined as public, and the members that you do not want to access are defined as private.

The benefits of using encapsulation:
1. We can modify our own code without modifying the program fragments that call our code, which makes the code easier to maintain.
2. Encapsulate the associated variables and functions into an object. The variables describe the properties of the object, and the functions describe the behavior of the object, which is in line with our understanding of the objective world.
3. It also implements data access restrictions on attributes, and also strengthens the security of program codes.

1.5 Scope of classes

A class defines a new scope, and all members of the class are in the scope of the class. To define a member outside a class, you need to use the :: scope parser to indicate which class domain the member belongs to

Use in actual development of modular programming:

 Stack.h file:

#pragma once

class Stack
{
public:
	void Init();
	void Push(int x);
	// ...
//private:
	int* _a;
	int _top;
	int _capacity;
};

Stack.cpp file:

#include "Stack.h"

void Stack::Init()
{
	_a = nullptr;
	_top = _capacity = 0;
}

This achieves the separation of data and methods.

1.6 Class Object Model

1.6.1 How to calculate the class object size

1. Class size calculation follows the structure alignment principle

2. The size of a class is related to data members and has nothing to do with member functions (empty class size is 1 byte), and the size of a class has nothing to do with static data members

3. The empty class will give one byte, this byte does not store valid data, it is just to occupy a place, indicating that the object exists

 Case 1: There are both member variables and member functions in the class

class A1
{
public:
	void f1() {}
private:
	int _a;
};

  Size: 4 bytes

Case 2: There are only member functions in the class

class A2
{
public:
	void f1() {}
};

   Size: 1 byte

Case 3: There is nothing in the class

class A3
{
}; ​

    Size: 1 byte

Guess you like

Origin blog.csdn.net/m0_73648729/article/details/129657886