Encapsulation, inheritance and polymorphism

Encapsulation : In fact, encapsulation and abstraction can be summed up together. The abstract is the concrete realization, and the encapsulation is the interface. The encapsulation is the combination of the abstracted data and the behavior (or function) to form an organic whole, that is, the data and The source code of operating data is organically combined to form a "class", in which data and functions are members of the class. The purpose of encapsulation is to enhance security and simplify programming. Users do not need to know the specific implementation details, but only use the members of the class through external interfaces and specific access rights. Encapsulation can hide implementation details and make the code modular.
Summary: Encapsulation is to encapsulate the data and behavior (or function) obtained by the instance abstraction into a class.

Inheritance : Inheritance refers to the ability to use all the functions of an existing class and extend these functions without rewriting the original class. The inheritance process is the process from general to special. It is a relationship between class and class. Through inheritance method, one class can obtain the attributes and methods of another class to achieve the purpose of code reuse.
Three inheritance methods: implementation inheritance, interface inheritance, and visual inheritance.
To achieve inheritance: is to rewrite a function to achieve. Realize with pure virtual function.
Interface inheritance: refers to purely inheriting the old roots of the parents, and can also be reimplemented. Realize with virtual function.
Visual inheritance: It is purely to realize all by oneself, solve by oneself function. The corresponding is a non-virtual function.

Polymorphism : C++ polymorphism is achieved through virtual functions. Virtual functions allow subclasses to redefine member functions, and the practice of subclasses to redefine parent classes is called overwriting or rewriting. Overloading allows multiple functions with the same name, and these functions have different parameter lists, allowing different numbers of parameters, different parameter types, or both. Regarding polymorphism, in short, it is to use the pointer of the parent type to point to the instance of its subclass, and then call the member function of the actual subclass through the pointer of the parent class.
Summary: One call, multiple states (one interface, multiple method).

Guess you like

Origin blog.51cto.com/14289397/2540081