[oop hard core explanation] Hello teacher, I am classmate Chen, what is object-oriented? How should we understand and think?

Received three same questions today? Question: Hello, Chen, what is object-oriented? How can we simply understand it? Object-oriented: how to understand objects, classes, encapsulation, inheritance, polymorphism, and combination? ? ?

1. What is oop object-oriented programming?

As a new method, Object Oriented Programming (oop for short) is essentially an abstract thinking process and object-oriented method embodied in model building.

Models are used to reflect the characteristics of things in the real world. It is impossible for any model to reflect all the specific characteristics of objective things. It can only be an abstraction of the characteristics and changing laws of things, and describe the characteristics of objects more generally, more intensively, and more profoundly within the scope it involves. The abstraction achieved by building a model is the deepening of people's understanding of the object.

2. What is an object?

Object (Object): something that can do things with it. An object has three attributes: state, behavior, and identity.

3. What is a class?

A class is a description of a certain type of thing, which is abstract; an object actually exists and is an individual of this type of transaction.

After reading the meaning of objects and classes, I still don’t understand it thoroughly. It doesn’t matter. Let’s give a small example:

For example, animals are a kind of "class", and dogs, cats, people, and sheep are a kind of "objects".

Wow, it turns out that the class is so simple, I understand, hehehe.

4. What is encapsulation?

Encapsulation can be broken down into two meanings:

The first meaning: bundle data and operations together to create a new type of process.
The second meaning: the process of separating the interface from the implementation.

5. What is inheritance?

A relationship between classes in which a class shares the structure and behavior defined by one or more other classes. Inheritance describes the "is a kind of" relationship between classes. Subclasses can extend, override, and redefine the behavior of the base class.

6. What is the combination?

Composition is both a relationship between classes and a relationship between objects.
In this relationship an object or class contains other objects and classes.
Simply put: a composition describes a "has" relationship.

7. What is polymorphism?

A concept in type theory whereby a name can denote objects of many different classes related to a common superclass. Thus, any object denoted by this name may respond differently to some common set of operations.

8. Two sections of magical oop and non-oop code

After talking about the concept, let's look at two pieces of code: 01 is C language code without OOP, and 02 is C++ code with OOP .

01

#include<stdio.h>

//定义变量
char *name;
int age;
float score;

//定义函数
void display(){
    
    
    printf("%s的年龄是%d,高考成绩是%1f\n",name,age,score);
}

int main(){
    
    
     //变量赋值
	name = "张三";
	age = 30;
	score = 662;
	 //调用函数
	 display();

	 return 0;
}

insert image description here
Code running result: Zhang San's age is 30, and his college entrance examination score is 662.

02

#include<stdio.h>

//通过class关键字类定义类
class Student{
    
    
public:
	//类包含的变量
	char *name;
	int age;
	float score;
	//类包含的函数
	void display(){
    
    
	    printf("%s的年龄是 %d,高考成绩是%.1f\n",name,age,score);
	}
};

int main(){
    
    
    //通过类来创建对象
	Student stu1;
	//类中的变量赋值
	stu1.name = "张三";
	stu1.age = 30;
	stu1.score = 662;
    //调用类中的函数
	stu1.display();

	return 0;
  
}

insert image description here
Code running result: Zhang San's age is 30, and his college entrance examination score is 662.

9. In-depth understanding of classes and objects

1. One class

01 and 02 have the same code running results? ? ? What's going on? Think back carefully to the classes and objects we talked about earlier.

In programming languages, we regard variables as data, which are used to store various forms of values; we regard functions as operations, which are used to perform certain processing on data. All codes are composed of data and operations, and the essence of program operation is to perform various operations on data.

Before the emergence of OOP, data and operations were separated, and we could not aggregate related data and operations syntactically. When the amount of code increased dramatically, it was difficult for us to figure out which data was associated with which operation, resulting in logic Very messy, not conducive to large-scale program development.

With OOP, we can put related data and operations in a container, which is a class. Classes encapsulate related variables and functions together so that they are grammatically associated and isolated from variables/functions in other classes to prevent us from misoperation.

insert image description here

Classes are isolated from each other. This means that variables and functions are only valid in the current class and are not visible to other classes. For example, a function in class A cannot use variables in class B, and vice versa.

OOP is actually a kind of code encapsulation idea. It puts related variables and functions into a class, protects and isolates them, and forms small modules one by one. Each small module can complete a small task.

OOP has absolutely no advantage in code execution efficiency. Its main purpose is to facilitate programmers to organize and manage code, quickly sort out programming ideas, and bring innovations in programming thinking.

2. Two exploration classes and objects

Through the above C++ code in 02, it can be found that the class cannot be used directly . It is necessary to create an object (object) through the class first, and then call the variables and functions in the class through the object .

In layman's terms, a class is just a complex data type constructed by the user. It is the same as basic types such as integers (int), decimals (float), and strings (string). Variables must be defined before they can be used. The variables defined by the class have a new name called object.

It doesn't matter if you don't understand, let's give another small example:

Comparing classes to drawings and objects to parts, drawings describe the parameters (member variables) and tasks (member functions) of parts; one drawing can produce multiple parts with the same properties, and different drawings can produce Different types of parts.

A class is just a drawing, which serves as an illustration and does not take up memory space; objects are specific parts, and they take up memory space only if they have a place to store them.

In C++, an object can be created through the class name, that is, the drawing is produced into a part. This process is called the instantiation of the class, so the object is also called an instance of the class (Instance).

Guess you like

Origin blog.csdn.net/qq_62259825/article/details/126440781