Understanding of C++ Classes and Objects (1)

1. The concept of class

1. Class is a concept in the programming of object-oriented language and the basis of object-oriented programming.
2. The essence of a class is a reference data type, similar to basic data types such as byte, short, int (char), long, float, double, etc. The difference is that it is a complex data type. Because it is essentially a data type, not data, it does not exist in memory and cannot be directly manipulated. Only when it is instantiated as an object will it become operable.
3. Class is an abstraction of a class of things with common characteristics in real life.
4. The properties and methods are encapsulated inside the class for operating its own members. A class is the definition of a certain object and has a behavior. It describes what an object can do and the method it does. They are programs and processes that can operate on this object. It contains information about how the object behaves, including its name, properties, methods, and events.

In fact, to understand in a more general way, it is to abstract the system that it belongs to. Example1: Dogs and Cats belong to animals. Then we can abstract a concept, abstract the concept of "animal", then in the class You can declare a class;

class Animal
{
    
    
//方法

//属性


};

It’s not difficult to understand the attributes and methods of classes. Just like cats and dogs belong to animals, then continue to abstract their commonalities. Cats and dogs are both animals, with names, weights, ages, and genders. These can all be used as attributes of the "animal class", so we can implement this in the code:

class Animal
{
    
    
public:
	//成员函数(方法,也可称为行为或者功能)
private:
	char m_Name[20];		//名字
	short m_Weight;			//体重
	short m_Age;			//年龄
	
};

The understanding of the method of a class is actually the behavior of the class, and can also be understood as the function that the class can perform. You can define this function yourself. Of course, the function is defined by yourself, and you can add functions at will. But usually, in programming, we try to conform to the concept as much as possible. For example, you can't abstract the functions that animals can't achieve. Even if you abstract the behaviors that animals can't achieve, then whether this behavior can get everyone in programming Understanding? Or does the abstracted function really make sense?
Since it is an abstraction of a commonality, then we usually abstract the commonality. As for the usage and function of the class, we will talk about it later. Here, I just abstracted some methods of the animal and defined the method logic, just as a case of understanding the class.

#include<iostream>
using namespace std;
class Animal
{
    
    
public:
//抽象了一个移动方法
	void Move()
	{
    
    
		cout << m_Name << "移动了" << endl;
	}
//抽象了一个吃东西方法
	void Eat()
	{
    
    
		cout << m_Name << "吃东西了" << endl;
	}
//初始化Set函数,这里牵扯到了类的初始化,我们可以后续再细讲类的初始化
	void Set(char name[20], int weight, int age)
	{
    
    
		strcpy(m_Name,name);
		this->m_Weight = weight;
		this->m_Age = age;
	}
private:
	char m_Name[20];		//名字
	short m_Weight;			//体重
	short m_Age;			//年龄
	
};


Second, the concept of objects

This object is not the other object. Do you have an object? Does it matter to me? Okay, let's stop joking and continue to understand the concept of objects.
Concept: A class is a collection of things or events with common characteristics, and a class object is an instantiation of a class.

Insert picture description here
The object here refers to a specific instance, so we usually introduce such a sentence, the instantiation object of a class, in general, is actually creating a class from the perspective of God, for example, we have created an animal class, So as "God", the concept of "animal" is not enough. Let's take specific things, such as cats, dogs, elephants, etc. For the category of "animals", we use specific things, specific examples, To instantiate the object.

Because only by instantiating the class object, we can better understand the function of the class. What can the class do and what is the role of the class. Here I instantiated objects, cats, and dogs through code.
Concentrate on listeningInsert picture description here

int main()
{
    
    
	Animal Dog;				//用Animal类实例化对象 Dog,也就表明了Dog拥有了类的相关属性及行为
	Dog.Set("Tom", 10, 1);	//用Set方法给Dog初始化,因为还没引入构造函数的概念,因此这里我仅用Set函数进行初始化
	Dog.Eat();				//Dog调用Eat()方法
	Dog.Move();				//Dog调用Mova()方法
	Animal Cat;				//用Animal类实例化对象 Cat,也就表明了Cat拥有了类的相关属性及行为
	Cat.Set("Amy", 8, 1);	//用Set方法给Cat初始化,因为还没引入构造函数的概念,因此这里我仅用Set函数进行初始化
	Cat.Eat();				//Cat调用Eat()方法
	Cat.Move();				//Cat调用Mova()方法
	
	return 0;
}

operation result:
Insert picture description here

Three, the relationship between class and object

The relationship between a class and an object is like the relationship between a mold and a casting. The instantiation result of a class is an object, and the abstraction of a class of objects is a class. A class describes a group of objects with the same characteristics (attributes) and the same behavior (methods) .

Fourth, the difference between class and object

1. Class is an abstract concept. It does not exist in real time/space. Classes just define abstract properties and behaviors for all objects. Just like the "Animal" category, although it can contain many individuals, it does not exist in the real world.

2. The object is a specific of the class. It is a real thing.

3. The class is a static concept, and the class itself does not carry any data. When no objects are created for the class, the class itself does not exist in the memory space.

4. Object is a dynamic concept. Each object has its own unique attributes and behaviors that are different from other objects. The properties of an object can change with its own behavior. (That is, in the code, artificial calls, to process the logic of the code, change due to the order of the calls)

: : ProMer_Wang

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

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/106879762