Object-oriented topics for C and C ++ (1) -- What is object-oriented
Question:
Object-oriented is an idea, not a language
Where can we find objects? All objects are oriented.
List of articles in this column
1. What is object-oriented?
2. The C language can also implement object-oriented
Iii. Non-elegant features in C ++
4. Solve encapsulation and avoid Interfaces
5. Use templates properly to avoid code Redundancy
6. C ++ reflection
VII. Singleton mode solves the difficult problem of constructing sequence of static member objects and Global Objects
8. More advanced pre-processor PHP
1. What is object-oriented?
Now I am studying software development. I am talking about object-oriented programming models, which is actually quite simple. To sum up in one sentence, object-oriented means to integrate the attributes of methods and methods, so that the attribute values referenced by each method are kept within the object as much as possible, and the interface is concise.
To achieve object-oriented design, the goal is not to write a class, but to design a structure. It designs good interfaces and encapsulation modes for each object and provides elegant interfaces to users, minimize code coupling.
Object-oriented design can be explained in a book, but here we will demonstrate the basic idea of object-oriented and how to implement it.
Looking at objects from a simple example
Well, let's build an object now.
Class window {public: window (int x, int y, int width, int height) {this. x = x; this. y = y; this. width = width; this. height = height;} void Show (); private: int x, y, width, height; // you can add more attributes, for example, transparency, sub-element list, menu, status bar waiting };
This is a basic window object. Maybe it doesn't give you an intuitive impression, but I hope to explain some problems through it.
Objects have public methods and private attributes. When we construct this object and pass in some parameters, we find that this process simplifies the use of some methods.
For example, if you want to draw some content in the C function, you may need to write such a function:
int Draw(struct window*, int, int, int, int);
All parameters are passed in, but if we find that most of the parameters are the same after many times of use, we suggest packing them all into the window struct, so the function becomes like this:
int Draw(struct window*);
In this way, we only need to change the value in the struct. This idea is the basis of object-oriented, and C ++ makes the struct to be passed in automatically, and then evolved to the this pointer.
Object-Oriented Design Principles
Almost all software design principles are high cohesion and low coupling.
The same is true for the C ++ design. The public methods of classes should be as clear as possible, simple and easy to use, without the need to put external information and detailed processes into private members, avoid misuse.
Avoid excessive inheritance and overloading. Although inheritance and overloading can greatly reduce the coupling between the two modules, the implementation is complicated and the structure is chaotic, which easily makes the code unclear. Similarly, we must avoid abuse of design patterns.
C ++ development should be based on the object-oriented idea and utilize the class encapsulation characteristics to efficiently organize some of the modules, while interfaces can adopt the method of multi-state calling, flexibility is guaranteed.