This article extracts from the classic C + + books: "Thinking in C + +"
Operation Concept: What is the OOP program like
We already know that procedural procedures written in C are some data definitions and function calls. To understand the meaning of this program, programmers must master function calls and function implementations themselves. This is why procedural procedures need to be represented in the middle. The middle expression is prone to confusion, because the expression in the middle represents is primitive, more biased towards the computer, and not biased towards the problem being solved.
Because C + + adds many new concepts to the C language, it is natural for programmers to assume that m a i n () in C + + programs is more complex than C programs with the same functionality.
But surprisingly, a well-written C + + program is generally simpler and easier to understand than a C program with the same functionality. Programmers will only see messages that describe the definition of the problem space object (not the description of the computer), and send it to those objects. These messages represent the activities in this space.
One of the advantages of object-oriented programming is that it is easy to understand the code by reading it. In general, object-oriented programs require less code because many parts of the problem can be used with existing library code.
why C + + will succeed
C + + can be so successful, partly because its goal is not just to turn C into O-O language (although this is the original goal), but also to solve many of the problems faced by today's programmers, especially programmers who have invested heavily in C.
People already have a traditional view of O-O language: programmers should abandon everything they know and start over with a new set of concepts and new grammars, and he should believe that it's best to throw away all the old stuff from the process language. In the long run, this is right. But from a short-term point of view, these are still valuable. The most valuable may not be the existing code base (giving the right tools to transform it), but the existing mind bank.
As a professional C programmer, if you let him throw away everything he knows about C to adapt to the new language, then within a few months, he will be fruitless until his mind adapts to it. If he can adjust the existing C knowledge and expand on this basis, then he can continue to maintain high efficiency, with the existing knowledge, into the object-oriented design of the world. Because everyone has his own program design model, the transition is confusing. Therefore, the reason for C + + success is economic: the shift to o O p takes a cost, and the transition to C + + costs less.
The purpose of C + + is to improve efficiency. Efficiency depends on a lot of things, and language is designed to help the user as much as possible, without arbitrary rules or special performance to hinder the user. C + + succeeds because it is based on reality: as much as possible to the programmer to provide maximum convenience.
better c.
Even if the programmer continues to write C code in the C + + environment, it can benefit directly, because C + + blocks Some of the vulnerabilities in the C language and provides better type checking and compile-time analysis. Programmers must first explain the functions so that the compiler can check their usage. Preprocessor virtual Delete value substitution and macros, which reduces the difficulty of finding defects. C + + has a performance, called R E F E r e N c e S (reference), which allows for more convenient handling of the address of function parameters and return values. function overloading improves the processing of names, allowing programmers to use the same name for different functions. In addition, the namespace also strengthens the control of the name
System. A lot of performance makes C more secure.
Adopt a gradual learning approach
Problems related to learning a new language are efficiency issues. Inevitably, all companies are suddenly less efficient because software engineers learn new languages. C + + is an extension of C, not a new grammar and a new program design model. Programmers learn and understand these features, gradually apply and continue to create useful code. This is one of the most important reasons for C + + success.
In addition, the existing C code is still useful in C + +, but because the C + + compiler is stricter, it is common to see hidden errors when recompiling the code.
Operational efficiency
Sometimes it is worthwhile to exchange program execution speed for programmer's efficiency. If a financial model is useful only in the short term, it is important to quickly create this model faster than the program you write. Many applications require a certain degree of operational efficiency, so C + + always makes mistakes when it comes to higher operational efficiency. C Programmers attach great importance to operational efficiency, which makes them think that the language is not too large or too slow. When the generated code runs inefficiently, programmers can make some adjustments with some of the C + + performance.
C + + not only has the same basic control ability as C (and C + + program directly write assembly language ability), informal card It is pointed out that the speed of the object-oriented C + + program differs from the program speed written in C by ±1 0, and is often closer. programs designed with the O O p method may be more efficient than the corresponding version of C.
The system is more easily expressed and understood
A class designed for a problem can certainly be a better way to express the problem. This means that when writing code, the programmer is using the terms of the problem space to describe the solution of the problem (for example, "Put the chain in a box"), rather than the terminology of the computer, it is the term of the solution space, describing the solution of the problem (for example, "set the chip on a closed relay"). Programmers are involved in the concept of a higher level, a line of code can do more things.
Another benefit of easy expression is ease of maintenance. It is reported that maintenance accounts for a large part of the cost of the program throughout its life cycle . If the program is easy to understand, it is easier to maintain and reduces the cost of creating and maintaining documents .
"Library" makes you more effective
The quickest way to create a program is to use code that has already been written: libraries. The main goal of C + + is to make it easier for programmers to use libraries, which is done by converting libraries to new data types (classes). The introduction of a library is the addition of a new type to the language . The compiler is responsible for how the library is used, to ensure proper initialization and cleanup, to ensure that functions are properly called, so the programmer's energy can be focused on what he wants the library to do, not how to do it.
because the names are isolated between the parts of the program, the programmer wants to use as many libraries as they want, and there will be no C-language conflict of name with words like that.
• Code reuse for templates
Some important types require that the source code be modified for efficient reuse. Templates can automate the modification of code, making it a particularly useful tool for reusing library code. Types that are designed with templates can easily work with other types. Because templates hide the complexity of this type of code reuse from programmers, they are especially useful.
Error Handling
In the C language, error handling is notorious. Programmers often ignore them and are powerless to do so. If you are building a large and complex program, there is nothing worse than letting the error hide somewhere, and not pointing out where it came from. Exception handling for C + + (see Chapter 1th 7) guarantees that errors can be checked and processed.
Large Program Design
Many traditional languages have their own limitations on the size and complexity of the program. For example, B A S i C can be solved quickly for some types of problems, but if the program has several pages long, or is beyond the normal range of the language, it may never be a result. The C language also has such limitations, such as when a program exceeds 50 000 lines, the name conflict begins to become a problem. In short, the programmer runs out of functions and variable names. Another particularly bad problem is that if there are some small holes in C that are hidden in large programs, it is extremely difficult to find them.
There is no clear word to tell the programmer when his language will fail, and even if he does, he will ignore them. He does not say "my B a S i C program is too large, I have to rewrite it with C", but instead tries to cram it into another few lines, adding extra performance. So the extra cost increases quietly.
The purpose of Design C + + is to assist in large programming, that is, to remove the demarcation between small programs and the complexity of large programs. When the programmer writes the H-E l o-w o r L D utility, he does not need to use o o P, templates, namespaces, and exception handling, but these performance is useful when he needs it. Also, compilers are as effective as small programs and large programs in troubleshooting errors.
http://blog.csdn.net/rl529014/article/details/51921883
Thinking in C + +: 1th why C + + succeeds (improves C's disadvantage, can reuse C's knowledge and library, execution efficiency is equal)