Object-oriented theme of C and C + + (1)-what is object-oriented

Source: Internet
Author: User

Preface

Object-oriented is a kind of thought, not a language

Where are we going to find the object?

List of articles in this column

First, what is object-oriented

Second, C language can also achieve object-oriented

Third, the non-elegant features in C + +

Iv. solve the package and avoid the interface

V. Rational use of templates to avoid code redundancy

VI, C + + can also reflect

Vii. single-Case pattern solving the construction order puzzle of static member objects and global objects

Viii. more advanced preprocessor PHP

First, what is object-oriented

Now learning software development, all talk about object-oriented programming model, in fact, is also very simple. In a word, the object-oriented approach is to integrate the properties of the methods and methods, so that the property values referenced by each method are as close as possible to the inside of the object, keeping the interface simple.

To achieve object-oriented design, the goal is not to write classes, but to design the structure, for each object well-designed interface and encapsulation mode, the elegant interface to the user, the greatest degree of reduced 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

Look at the object from a simple example

Well, then, let's build an object.

class window {public:    window(intintintint height) {        thisthis.y = y;        this.width = width;        this.height = height;    }    void Show();private:    int x, y, width, height;    // 也许你还可以添加更多属性,例如透明度、子元素列表、菜单、状态栏等待};

This is a basic window object, perhaps it does not give you an intuitive impression, but I would like to be able to illustrate some questions through it.
Object has public methods, there are private properties, when we construct this object, we pass some parameters in, we find that this process simplifies our use of some methods.

For example, in the C function, how you want to draw something, what you might need to do is write a function like this:

int Draw(struct window*, int, int, int, int);

All the parameters are passed in, but if we find that the parameters are mostly the same, then we should pack them all into the window structure, so the function becomes this:

int Draw(struct window*);

So we just have to change the value in the struct, this idea is the object-oriented foundation, and C + + is to let this incoming struct automatically pass in, and then evolve for this pointer.

Object-Oriented design principles

Almost all software design principles are high cohesion, low coupling.

The design of C + + is the same, the public methods of the class should be as clear as possible, easy to use, and unnecessary external knowledge of information, and details of the process, are put into the private members, to avoid misuse.

Avoid excessive inheritance and overloading, although inheritance and overloading can greatly reduce the coupling between two modules, but its implementation is complex, the structure is chaotic, it is easy to make the code is not clear. Also, avoid misuse of design patterns.

C + + development should be based on object-oriented thinking, the use of class packaging features, the internal parts of the module is organized in an efficient way, and the interface, you can use polymorphic calls to ensure flexibility.

Object-oriented theme of C and C + + (1)-what is object-oriented

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.