Hello, C + + (31) I finally found the object! 6.1 From structured design to object-oriented programming

Source: Internet
Author: User

6th Chapter

When C + + falls in love with object-oriented

Many people who enter the C + + world for the first time will ask: What do the two plus signs in C + + mean?

C + + is the development of the language, it is more than the C language of the two Plus, in fact, the C language of the self-increment operator, that the C + + language is the C language on the basis of the addition of new content and evolved. If one of the plus signs means that C + + adds new features to modern programming languages such as templates and exception handling on the basis of C, the other plus adds support for the idea of object-oriented programming based on C + +. It is the new content that these two pluses represent, let C + + on the foundation of C language, complete from tradition to modern, from process to object-oriented evolution, make C + + and C language have the essential difference.

And in the new C + + of these two content, the core is to object-oriented programming thought support, it is it's accession, just completed from C language to C + + gorgeous metamorphosis. So what exactly is object-oriented? Why should C + + add support for it? How does C + + support object-oriented? Don't worry, and listen to my one by one-way.

6.1 From structured design to object-oriented programming

The prototype of object-oriented programming appeared in the Simula language of the 1960. At that time, the field of program design is facing a crisis: in the face of more and more complex hardware and software systems, the traditional C-language-oriented process programming thought has become more and more unable to meet the needs of the reality-the process-oriented design can not well describe the whole system, and the design results are difficult to understand, Thus, the implementation of the software and the maintenance of the late has brought great challenges, the larger the more difficult to achieve, the more difficult to achieve later in the project, people are caught in an unprecedented software crisis. In order to defuse the software crisis, people began to look for the "software crisis" to eliminate the monster "silver Bomb" (bullet). The idea of object-oriented programming is born in this background, and it successfully dissolves the "software crisis" by emphasizing the expansibility and repeatability of design and implementation. Since then, the object-oriented design of the program began in the industry, and gradually become the mainstream. The evolution from C to C + + happens at this time, naturally, C + + also chooses to support object-oriented programming thinking.

The myth of People's month and the silver Bullet

"Human Moon Myth" is a book with far-reaching influence in the field of software. It was born in the context of a software crisis, and it was the book that proposed the concept of "silver Bullets".

In Western myths and legends, monsters can only be killed if they are hit by a silver bullet in the heart. In this book, the authors liken the increasingly large-scale software development projects that are increasingly difficult to manage and maintain to the legendary monsters that cannot be controlled, and hope that a technology can completely solve the software crisis as the Silver Bullet kills monsters.

In fact, the idea of object-oriented programming is not a full-scale silver bullet, it can not solve all the problems encountered by large software projects, but it proposed a more natural way to describe the software, to a certain extent, to solve the software crisis.

6.1.1-oriented process programming for "top-down, gradual refinement"

The simplest and most straightforward way to understand the merits of the new idea is to look at the shortcomings of the old process-oriented thinking. Looking back at the examples we have learned in the previous chapters, we always follow the process of solving problems: Ask questions first, then analyze the process of the problem, and then divide a big problem into small problems according to the process, and if the small problem is still more complex, subdivide it until the small problem can be easily solved. ; Implement each sub-module, solve each small problem; Finally, the main function calls these submodules according to the order of business process, and finally solves the whole big problem, as shown in 6-1. Like this from the problem, from the top down, the progressive refinement of the development of ideas we call "process-oriented design thinking", which describes the main problem-solving "process."

Figure 6-1 Process design-oriented processes

The idea of process-oriented programming was born in the 1960s, and it was the most popular program design idea at that time in the 1980s. Its popularity has its intrinsic reasons, compared with other programming ideas at that time, it has obvious advantages.

1. The program consists of only three basic structures

As described in the 4th chapter of the program flow control structure, the process-oriented programming concept to limit the program only the sequence, selection and circulation of the three basic control structures. Any program logic, whether simple or complex, can be implemented by different combinations or nesting of these three basic control structures. This makes the structure of the program relatively simple, easy to implement and maintain.

2. Divide and Conquer, conquer

When people solve complex problems, they always adopt the strategy of "divide and conquer", and divide the big problem into several small problems, then "conquer" and finally let the big problem be solved. Process-oriented programming thought also take this "divide and conquer" strategy, the larger program according to business logic into multiple sub-modules, and then the division of labor to complete these sub-modules, and finally in accordance with business processes to organize them, and ultimately the whole problem is resolved. According to certain principle, the big problem is subdivided into small problem "conquer", which conforms to the general law of People's thinking problem, its design result is easier to understand, and this method is easier for people to grasp. By decomposing the problem, it reduces the complexity of the problem and makes the program easy to implement and maintain. In addition, the partial decomposition of small problems (sub-modules) can be reused, thus avoiding the duplication of development. But many sub-modules can also be completed by a multi-person division of labor, improve the development efficiency.

3. From top to bottom, gradually refinement

The method of process-oriented programming is "top-down, refinement gradually". The so-called "top down, gradually refinement", is to consider from a macro perspective, according to the function or business logic to divide the program sub-module, define the overall structure of the program, and then the individual sub-modules are gradually refined, and finally decomposed into the program statement so far. This approach allows the programmer to fully consider the problem and make the logic of the program clear. It makes the entire development process from the original thinking "how" to consider "what to do first, what to do", the process is also clearer.

With the development of the Times, software development projects are becoming more and more complex. Although the idea of process-oriented programming has many advantages, its shortcomings are gradually exposed when it solves complex problems: In process-oriented programming, the data and operations are separated from each other, which leads to the change of the structure of the data, and the corresponding operation function has to be rewritten again. , if there is a change in demand or new requirements, it may involve the re-partitioning of the module, which will modify a large number of previously written functional modules. The separation of data and operation in process programming is characterized by the combination of some modules with the specific application environment, and the old program modules are difficult to be reused in the new program. The inherent shortcomings of these process-oriented programming ideas make it more and more unable to adapt to the development of large-scale software projects, which is "process-oriented, losing and process-oriented". So, people began to look for a new idea of program design. It is in this case that some new ideas of program design begin to emerge and gradually replace the thought of process-oriented programming, and the object-oriented programming idea is one of the "leading brothers".

6.1.20,000 objects: Object-oriented Programming

Object-oriented programming (oriented programming, OOP) is the inheritance and development of process-oriented programming, which not only draws the essence of the latter, but also analyzes and solves the problem in a way closer to human thinking: The procedure is the abstraction and description of the real world, The basic unit of the real world is the object, which corresponds to the basic unit of the program.

The object-oriented thought is that the real world is composed of many entities that are related to each other and exchange information. As large as a planet, a country, small to a person, a molecule, whether it is a life, or no life, can be regarded as an object. By analyzing these objects, each object is found to consist of two parts: data (variables) that describe the state or properties of an object, and methods (functions) that describe the behavior or functionality of the object. In contrast to the function of process-oriented data and manipulation of data, object-oriented objects combine the functions of data and manipulating data together to form objects to more accurately describe the real world. This can be said to be the most essential difference between the process-oriented and object-oriented.

Corresponding to the real world, in object-oriented, we use an object to represent an entity in the real world, each object has its own properties and behavior, and the whole program is composed of a series of interacting objects, the objects through each other to complete complex business logic. For example, in a class, there is a teacher Chen and 50 students, then we can use a teacher object and 50 student object to abstract and describe this class. For these 51 objects, some properties are common to them, such as name, age, and so on, and some attributes are specific to a particular class of objects, such as the teacher object has the attribute of the job, and the student object does not. In addition, teachers and students of the two kinds of objects have their own behavior, such as teachers have to prepare for lessons, classes, correcting homework behavior, while the student object is to listen to, complete homework and other behaviors. Teachers and students are responsible for their own behavior and responsibilities, but also related to each other, such as the teacher's action in class needs to take students as the object of action. Through the interaction between the objects, the whole class can function normally. The results of the entire object-oriented analysis design are very close to our real world and are naturally easier to understand and implement. The teacher is shown in Object 6-2.

Figure 6-2 using object-oriented thinking to abstract teachers into objects

Know more: What is the importance of object-oriented programming?

This, perhaps, can be said from the birth of the object-oriented.

Before object-oriented birth, there was a process-oriented person who abstracted the whole problem to be solved as a description of the data of the thing and a function describing the processing of the data, or the process of data processing. When the scale of the problem is small and the demand changes little, the process is working very well.

But (anything is afraid of "but" two words), when the scale of the problem is getting more and more complex, the demand changes more and more quickly, the process seems to be inadequate. Imagine that when we modify a structure according to the requirements change, we have to modify all the process functions associated with it, and a process function modification, often involves other data structures, when the system is small, this is relatively easy to solve, but when the system size is getting bigger, Especially when it comes to multi-person collaborative development, it's a nightmare. This is the famous software crisis.

In order to solve this software crisis, object-oriented came into being (there is a problem, there must be a solution to the problem of the emergence of the hero, most of the birth of this).

We know that object-oriented kick are encapsulation, inheritance, and polymorphism: It uses encapsulation to combine the data in the problem with the functions that process the data, forming the concept of a whole class, which is more in line with people's thinking habits, more conducive to understanding, and it is easier to understand and abstract some complex systems naturally. It uses inheritance to deal with the expansion of the system, on the basis of the original system, as long as the simple inheritance, you can complete the expansion of the system, without reinvent; it uses polymorphism to respond to changes in demand, unified excuses, but can have different implementations.

It can be said that the object-oriented thought uses its kick to solve the software crisis to some extent, which is the fundamental embodiment of its importance.

6.1.3 Object-oriented three cornerstones: encapsulation, inheritance, and polymorphism

We know that object-oriented is to solve the process can not solve the "software crisis" born, then it is how to solve the "software crisis"? Encapsulation, inheritance and polymorphism are the three cornerstones of object-oriented thinking, and it is their common role that the software crisis has been resolved to a certain extent, as shown in 6-3.

1. Encapsulation

Programs are used to abstract and describe the real world. So let's take a look at how we describe things around us in the real world. We always describe something from two aspects of data and operations: What this thing is and what this thing can do. For example, we would like to describe a teacher, we will say: He is 178 cm tall, weight 72 kilograms, age 32 years old, and he can give students classes, can be graded homework. In this way, a living teacher's image will be set up in our minds.

In the traditional process-oriented thinking, the data and operations in the program are separated from each other. In other words, when describing a thing, what (data) and what things can do (operations) are separated from each other. But in object-oriented thinking, we bundle the data with the corresponding operations through encapsulation mechanisms to form a complete data type with attributes (data) and behavior (operations). In C + +, we refer to this data type as class, and the variable defined by this data type is called an object. This makes the data in the program and the operation of the data together, more in line with the thinking habits of people to describe something, so it is easier to understand and implement. In simple terms, an object is a logical entity that encapsulates the action of data and operations, as well as the reflection of things in the real world in the program, 6-4.

Figure 6-4 encapsulating properties and behaviors into objects

The encapsulation mechanism also brings another benefit, which is the protection of data. In process-oriented thinking, because data and operations are separated from each other, some operations may incorrectly modify data that does not belong to it, resulting in a program error. In object-oriented thinking, data and operations are bundled together into objects, and the data can be private to an object, accessed only by operations bundled with it, thus avoiding accidental access to the data by other operations. This is like the money in the purse is our private property, only we can access, others are not accessible. Of course, except for thieves.

2. Inheritance

When it comes to creating something new, we always want to start on the basis of something old, without a doubt it will improve efficiency. But for the process-oriented C language, this is difficult to do. In C, if you have written a "class" function, and want to write a "math class" function, many times we have to start all over again. If we had to reinvent the board every time, that would be too inefficient. Obviously, this does not meet the development needs of large complex systems.

In order to solve this problem, the object-oriented thought puts forward the inheritance mechanism. Inheritance is an easy way for a type to get another type of property (member variable) and a behavior (member function). Inheritance, like evolution in the real world, inherits subtypes that can have properties and behaviors of the parent type, and can add new properties and behaviors that are specific to subtypes. For example, we have encapsulated the name attribute and speech behavior into the "human" class, and then we can easily inherit the "person" class, while adding job attributes and class behavior to get a new "teacher" class. And this new "teacher" class, not only has its parent class "person" the name attribute and the speech behavior, simultaneously also has its own function attribute and the class behavior. If necessary, we can also be on the basis of the "teacher" class to inherit the "math teacher", "Language teacher" category and so on. In this process, we directly reuse the existing attributes and behaviors of the parent class, which avoids the start of a process-oriented restart and greatly improves the development efficiency. As shown in 6-5.

Figure 6-5 Inheritance

3. polymorphic

"See leadership flattery, see subordinate domineering", is said a person two-faced, not what good person. In the C + + world, this phenomenon of doing different things under different circumstances has been crowned with a sounding name-polymorphism, which has become an important feature of object-oriented thinking.

Polymorphism is the direct result of inheritance. Because of inheritance, multiple types of objects in the same inheritance system tend to have the same behavior to be able to do the same thing, but because of the different types, these behaviors often need to be implemented in different ways. For example, "College teachers" and "primary school teachers" are inherited from the "teacher" of the parent class, both of them from the "teacher" parents inherit from the "class" behavior, but the two "class" The specific way is different: "Primary school teacher" is holding the textbook class, and "college teacher" is holding the mouse class. Polymorphism is when an object is doing something (invoking an interface function), the object is able to figure out exactly how to accomplish it. Or the above example, with a polymorphic mechanism, is also a call to "class" function, if this object is "primary school teacher" type, the use of "Primary school teacher" class implementation, and if this object is "university teacher" type, the use of "university teacher" class implementation. This is the C + + world in the "see Leadership flattery, see subordinates domineering." As shown in 6-6.

Figure 6-6 Polymorphic

Best practices: The difference between polymorphism and overloading

Polymorphism is a different implementation of calling a function depending on the situation compared to the function overloads we learned before. However, both the internal mechanism and the external form, the two have a very big difference.

First, on the intrinsic mechanism, the time between the two takes place differently. Overloading is a compile-time concept that occurs during the compile time of a program, and the compiler decides to invoke a specific implementation of this overloaded function based on the number and type of actual arguments that are called by the function in the code. Polymorphism is a run-time concept that occurs during the runtime of a program that determines the implementation of calling this function in a particular class, based on the actual object type that called the function.

Secondly, in the form of outside, the relationship between the two levels is different. For function overloading, overloaded functions of the same name are in the same scope, either as full-play functions or as local functions of a local scope. Polymorphism is accompanied by inheritance, which can only occur between classes at different levels of an inheritance system.

Polymorphic mechanisms allow different types of internal implementations to have the same function declarations and share the same external interfaces. This means that although specific actions are different for different objects, they (member functions) can be called in the same way through a common parent class. In simple terms, a polymorphic mechanism allows a set of related but not identical actions to be raised through the same interface. In this way, maintaining the consistency of the code reduces the complexity of the code. The same function call form, but what should be done in a particular case is determined by the compiler, without the programmer having to intervene manually, saving programmers a lot of things.

Throughout the three characteristics of object-oriented thinking, they are closely related and indivisible. Through encapsulation, we bundle the real-world data and the action of the data into a class, and then through the class definition object, it is very good to realize the abstraction and description of the real world things, through inheritance, can quickly derive the new type on the basis of the old type, and realize the reuse of the design and code very well. , the polymorphic mechanism ensures that, while inheriting, there is an opportunity to redefine the existing behavior to meet the needs of emerging new needs.

Because of the three characteristics of encapsulation, inheritance and polymorphism, object-oriented thought has an irreplaceable advantage in program design.

(1) Easy to design and implement.

The object-oriented thought emphasizes to understand the problem and solve the problem from the objective object, because this way is more accord with the law of the things we know, so it greatly reduces the difficulty of understanding the problem. The basic principles of encapsulation, inheritance and polymorphism used in object-oriented thought conform to the daily habits of human thinking, making the program structure with object-oriented thought design clear and easy to design and implement.

(2) Reuse of design and code, development efficiency and system quality have been improved.

The inheritance and polymorphism of object oriented thought emphasize the reuse of program design and code, which can be used to maximize the reuse of existing and extensive practice-tested design and code, so that the system can meet the new business requirements and have high quality. At the same time, the development efficiency is greatly improved because the previous design and code can be reused.

(3) easy to expand.

When developing large systems, the most worrying is the change in requirements and the expansion of the system. By using object-oriented thought inheritance, encapsulation and polymorphism, the system structure of "high cohesion and low coupling" can be designed to make the system more flexible and more scalable, so as to easily deal with the expansion requirements of the system and reduce the maintenance cost.

Best practice: Cohesion Poly, low coupling

Cohesion, low coupling is a concept in software engineering and is often used to determine the quality of a software design. The so-called high-cohesion, refers to a software module is a strong correlation of code, only responsible for a single task, that is often said "single responsibility principle." The low coupling refers to a complete system in which the modules and modules are kept as independent as possible from each other. In other words, let each module accomplish a specific sub-function as independently as possible. The interface between modules and modules is as small and simple as possible.

Cohesion-poly low-coupling system has better reusability, maintainability and expansibility, and can complete the development, maintenance and expansion of the system more efficiently, and continuously support the development of the business. Therefore, it can be used as a standard for judging the quality of a software design, and naturally the goal of our software design.

These advantages of object-oriented programming in software development make it one of the most popular program design ideas, which every programmer who enters the C + + world needs to understand and master. It is like the design of the "Easy Rib Sutra" as broad and profound, and here is only the object-oriented idea of the most basic knowledge, to fully understand and flexible use of object-oriented thinking, but also need to continue to learn and summarize in practice. While understanding the concept, we should focus on how to use object-oriented thinking to analyze the problem design program, only in this way can increase the software design and development skills, become a true master.

(I'll be back again.)

Hello, C + + (31) I finally found the object! 6.1 From structured design to object-oriented programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.