When I first fell in love with Object-Oriented six years ago, I remembered nearly ten definitions in one breath. Six years later, when I crawled from hundreds of thousands of lines of programs to prepare for snacks, I couldn't explain what "object-oriented" was, just as I couldn't tell what mathematics was. In software engineering, the fashionable terms "Object-Oriented Analysis" and "Object-Oriented Design" are usually used in the "requirement analysis" and "System Design" stages. There are several schools of "Object Oriented", such as Buddha, God, and God, who define the world in their own ways and leave a bunch of scriptures to explain the world.
Some scholars suggest looking for "objects" like this: analyzing the syntax of a sentence, finding out nouns and verbs. Nouns are objects, and verbs are object methods (I .e. functions ).
In order to confront Mao Zedong's "qinyuanchun · snow", the Kuomintang's literati specially asked the Qing Dynasty elders to write some neat poems to show their views. Old Jiang looked angry and scolded: "Niang SIPI, all of them have the smell of rotten corpses in the coffin ." After reading thousands of pages of software engineering materials, I finally found myself "mentally retarded", unable to understand the "Object-Oriented" theory, and realized that "programming is the final principle ."
There are many object-oriented programming languages, such as Smalltalk, Ada, Eiffel, Object Pascal, Visual Basic, and C ++. The C ++ language is the most flattering because it is compatible with the C language and has the performance of the C language. In recent years, a pure object-oriented language called Java has become very popular, and many people have been clamoring for the use of Java C ++. I think Java is like a C ++ nephew. Although it is not directly inherited, it is also a bit similar. When the nephew was playing on his uncle, he put a bubble in his urine. The two should not argue about it.
There are many books on C ++ programming. This Chapter does not talk about C ++ syntax, but only about some small programming principles. If I had understood these principles several years ago, I could have greatly improved the quality of hundreds of thousands of lines of programs.
1. Important Concepts of C ++ Object-Oriented Programming
There was such a role in the early Revolutionary film. He said, "I am a party representative, I represent the party, and I am the party ." Later, he brought disaster to the comrades.
Will C ++ programmers understand object-oriented programming?
Programmers who don't use C ++ certainly don't understand object-oriented programming?
Neither of them is necessary. Just as a bad guy may not become a good guy after joining the party, and a good guy may not become a bad guy if not joining the party.
I am not afraid of breaking the public anger to say: "C ++ has no master, and C language has a master ." After eight years of programming with C and C ++, I am deeply sorry that I am not a master of C language, and I am sorry that no one has asked me how to design object-oriented programs. Like many C ++ programmers, I thought I had understood object-oriented programming when enjoying the benefits of C ++ syntax. It's like squeezing out toothpaste and Selling toothpaste.
People can speak Mandarin even if they do not understand pinyin. If they do, they can speak Mandarin better. C ++ programming can be used if you do not understand object-oriented programming. If you understand object-oriented programming, C ++ programs will be better compiled. This section describes three basic concepts: class and object, inheritance and combination, and virtual function and polymorphism ". Understanding these concepts will help improve the quality of the program, especially improve "reusability" and "scalability ".
1.1 categories and objects
An Object is an Instance of a Class ). If the object is compared to a house, the class is the design drawing of the house. So the focus of object-oriented programming is class design, rather than object design. Classes can encapsulate data and functions, in which functions represent the behavior (or service) of classes ). Class provides public, protected, and private keywords to declare which data and functions are public, protected, or private.
This can achieve the purpose of information hiding, that is, to make the class only public content that must be known to the outside world, while hiding all other content. We cannot abuse the encapsulation function of the class. We should not treat it as a hot pot and throw everything in it.
Is the class designed to be data-centric or behavior-centric?
Advocates the internal data structure of the "data-centered" group of personnel category. They are used to writing private-type data in front and public-type functions in the back, see Table 8.1 (.
What kind of services and interfaces should the "behavior-centric" Group provide? They are used to writing public-type functions in front and private-type data in the back, see Table 8.1 (B.
Many C ++ teaching books advocate "data-centric" in design ". I insist on and recommend that you "act-centric" when designing a class, that is, first consider what functions the class should provide. The core of Microsoft's COM specification is interface design. The COM interface is equivalent to the class's public function [Rogerson 1999]. In terms of program design, we should not doubt Microsoft's style.
Designing isolated classes is easier. It is difficult to correctly design the base classes and Their Derived classes. Some programmers cannot understand the concepts of "Inheritance", "Composition", and "Polymorphism.
1.2 inheritance and combination
If A is A base class and B is A derived class of A, B inherits data and functions of. The example program is as follows:
Class
{
Public:
Void Func1 (void );
Void Func2 (void );
};
Class B: public
{
Public:
Void Func3 (void );
Void Func4 (void );
};
// Example
Main ()
{
B B; // an object of B
B. Func1 (); // B inherits the function Func1 from.
B. Func2 (); // B inherits function Func2 from
B. Func3 ();
B. Func4 ();
}
This simple example program illustrates the fact that the "inheritance" feature of C ++ can improve the reusability of the program. Because "inheritance" is too useful and easy to use, it is necessary to prevent the misuse of "inheritance ". We need to set some rules for "inherit:
1. if Class A and Class B are irrelevant, B cannot inherit the functions of Class A to make B more functional.
Don't think that "don't eat white, don't eat", let a good and strong young people eat ginseng for no reason.
2. If Class B needs to use the function, consider the following two situations:
(1) If logical B is A kind of a, B is allowed to inherit the functions of. For example, a Man is a Human, and a Boy is a Man. Class Man can be derived from class Human, and class Boy can be derived from class Man. The example program is as follows:
Class Human
{
...
};
Class Man: public Human
{
...
};
Class Boy: public Man
{
...
};
(2) If logically A is a part of B, B is not allowed to inherit the function of A, but to combine A with other things. For example, Eye, Nose, Mouth, and Ear are part of the Head, therefore, class Head should be composed of class Eye, Nose, Mouth, and Ear, not derived. The example program is as follows:
Class Eye
{
Public:
Void Look (void );
};
Class Nose
{
Public:
Void Smell (void );
};
Class Mouth
{
Public:
Void Eat (void );
};
Class Ear
{
Public:
Void Listen (void );
};
// Correct design and lengthy procedures
Class Head
{