Home  >  Article  >  Backend Development  >  C++ summary: basic concepts of object-oriented

C++ summary: basic concepts of object-oriented

php是最好的语言
php是最好的语言Original
2018-08-10 14:59:012841browse

Object-oriented language

In the first semester of freshman year, we learned C language. It is a very classic and very basic language. All computer science students probably have not escaped its clutches. C++ is very clear from its name. It is a derivative of C. It includes almost all the contents of C. However, their design ideas are quite different. C is a process-oriented program, while C++ is object-oriented. program, let’s explain the difference between the two.

Process-oriented: Analyze the steps required to solve the problem, and then use functions to implement these steps step by step. When using, just call them one by one.
Object-oriented: is to decompose the transaction that constitutes the problem into various objects. The purpose of establishing the object is not to complete a step, but to describe the behavior of something in the entire step of solving the problem.
For example, in backgammon, the process-oriented design idea is to first analyze the steps of the problem: 1. Start the game, 2. Black stones move first, 3. Draw the picture, 4. Judge winning or losing, 5. It’s White’s turn, 6. Draw the picture, 7. Determine winning or losing, 8. Return to step 2, 9. Output the final result. Implement each of the above steps with separate functions and the problem will be solved.
Object-oriented design solves problems from another perspective. The entire backgammon can be divided into 1. the black and white sides, whose behaviors are exactly the same; 2. the chessboard system, which is responsible for drawing the picture; 3. the rules system, which is responsible for determining fouls, winning and losing, etc. The first type of object (player object) is responsible for accepting user input and informing the second type of object (chessboard object) about the changes in the chess piece layout. After the chessboard object receives the i changes of the chess pieces, it is responsible for displaying this change on the screen, and at the same time using The third type of object (rule system) is used to judge the chess game.
It can be clearly seen that object-oriented divides problems by functions, not steps. It is also drawing a chess game. Such behavior is dispersed in multiple steps in process-oriented design, and different drawing versions are likely to appear, because usually designers will make various simplifications taking into account the actual situation. In object-oriented design, drawing can only appear in the chessboard object, thus ensuring the unity of drawing.
The unification of functions ensures the scalability of object-oriented design. For example, if I want to add the function of regretting chess, if I want to change the process-oriented design, then the series of steps from input to judgment to display must be changed, and even the order between steps must be adjusted on a large scale. If it is object-oriented, you only need to change the chessboard object. The chessboard system saves the chess records of the black and white sides, and you can simply backtrack. The display and rule judgment do not need to be taken into account. At the same time, the entire order of calling the object functions remains unchanged. The changes are only partial.
For another example, I want to change this backgammon game to a Go game. If you are designing for process, then the backgammon rules are distributed in every corner of your program. If you want to change it, it is better to rewrite it. But if you had an object-oriented design from the beginning, then you only need to change the rule objects. Isn't the difference between backgammon and Go the rules? (Of course, the size of the chessboard seems to be different, but do you think this is a problem? Just make some small changes in the chessboard object.) The general steps of playing chess have not changed at all from an object-oriented perspective. .
Of course, to make the changes only partial, the designer needs to have enough experience. Using objects does not guarantee that your program is object-oriented. Beginners or very poor programmers are likely to be process-oriented in an imaginary way. In fact, it is difficult for so-called object-oriented programs designed in this way to have good portability and scalability.

(This is the answer I found on Baidu! I didn’t write it myself. Thank you to the guy who answered the question!!!)

  • For Basic concepts of objects

1. Object

Scientific explanation: The object in the object-oriented method is an entity used to describe objective facts in the system. It is a basic unit used to constitute the system. An object consists of a set of properties and a set of behaviors.

Self-explanation: For example, "person" is a very big concept, but when it comes to yourself, you are the object. The one with entity and specific meaning is the object. After reading the introduction to classes I hope you can understand better. After all, I am not good at Chinese. . .

2. Class

Summarizes many things and divides them into some categories. The principle of classification is abstraction, that is, ignoring the non-essential characteristics of things and only paying attention to those essential characteristics related to the current goal, so as to find out the characteristics of things. Commonality refers to dividing things with common properties into one category to derive an abstract concept, such as stones, trees, cars, houses, etc.

3. Encapsulation

Encapsulation is an important principle of object-oriented methods, which is to combine the properties and services of an object into an independent system unit and hide the internal details of the object as much as possible.

4. inherit

Inheritance is one of the important reasons why object-oriented technology can improve the efficiency of software development. Its definition is: objects of a special class have all the properties and services of its general class, which is called the inheritance of a general class by a special class.

For example, after we understand the characteristics of a ship, when we consider a passenger ship, because we know that a passenger ship is also a ship, we can think that it has all the characteristics of a ship, so we only need to focus on the unique features of a passenger ship. Characteristically.

5. Polymorphism

Polymorphism refers to the attributes or behaviors defined in a general class. After being inherited by a special class, it can have different data, types or show different behaviors. (This will be explained later with specific examples!)

  • Complete program process

C++ summary: basic concepts of object-oriented

Related recommendations:

C Class & Object | C Manual Tutorial|

Summary of conceptual systems in C#-C#.Net tutorial

The above is the detailed content of C++ summary: basic concepts of object-oriented. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn