C + + is a typical object-oriented programming language, the most notable feature is encapsulation, inheritance and polymorphism. Fully understand the encapsulation, inheritance, polymorphism is how to achieve, learn C + + is not difficult.
1. Encapsulation
Encapsulation is a class that combines multiple detail elements to hide them. The external cannot understand how the inner class is implemented, just consider its interface.
For example, place information such as the name, age, work number, etc. of a company employee in the private part of the class.
Benefits of Encapsulation:
Avoid unintentional user-level errors within the class that could corrupt the state of the object.
The class implementation can be refined over time based on requirements changes or bug reports without changing user-level code.
2. Inheritance
This section is transferred from Ruyue_ruyue's blog
What is inheritance?
Inheritance refers to the ability to use all the functionality of an existing class and to extend these capabilities without rewriting the original class. The process of its inheritance is from the general to the special process.
New classes created through inheritance are called "subclasses" or "derived classes." The inherited class is called the base class, the parent class, or the superclass. To implement inheritance, it can be implemented through inheritance (inheritance) and combination (composition). In some OOP languages, a subclass can inherit multiple base classes. However, in general, a subclass can have only one base class, and to implement multiple inheritance, it can be implemented by multilevel inheritance.
How is the inheritance implemented?
Inheritance concepts are implemented in three categories: implementation inheritance, interface inheritance, and visual inheritance.
1. Implementation of inheritance refers to the ability to use the properties and methods of the base class without additional coding;
2. Interface inheritance refers to the ability to use only the names of properties and methods, but subclasses must provide the implementation;
3. Visual inheritance refers to the ability of a subform (class) to look and implement code using the base form (class).
3. polymorphic
C + + polymorphism is divided into two types: static polymorphic and dynamic polymorphism. The static polymorphism occurs at the compile time, and the dynamic polymorphism occurs in the running period.
~ Static multi-state ~
Static polymorphism can be achieved through templates and function overloading (the reason why C + + is polymorphic, mainly because of the template).
1) function template
max(constconst T& rsh){ return (lsh > rsh) ? lsh : rsh;}
This is a template that returns the maximum value of an object of any type, provided that the type is able to be compared using the > operator and that the return value is of type bool.
Use of templates:
int23max(a, b) << endl; float2.03.0max(c, d) << endl;
The output is:
33.0
Because the instantiation of the template occurs at compile time, the above bindings occur at compile time, that is, when the compiler discovers that you call Max (A, B), the following function is automatically generated (replace T in the template with int):
intmax(constintconstint& rsh){ return (lsh > rsh) ? lsh:rsh;}
Similarly, when the compiler discovers that you are calling Max (C, D), the T in the template is automatically replaced by float.
2) function overloading
If two function names are the same, their parameter types or number of arguments are not exactly the same, they become function overloads. When a function is called, it determines which function to call, based on the type and number of arguments.
(If two functions are the same function, it must be the function name, the parameter type, the number of arguments, the body of the function is exactly the same.) )
As an example:
intmax (intint// 函数1 return (a > b) ? a:b; }intmax (intintint// 函数2 returnmax(max(a,b),c); }floatmax (float a ,float// 函数3 return (a > b) ? a:b;}
Use:
int234;float2.03.0max(a,b) << endl; // 函数1max(a,b,c) <<endl; // 函数2max(a1,b1) <<endl; // 函数3
The output is:
343.0
The process of determining which function to invoke is also occurring at compile time. Max (A, A, b), the compiler discovers only two parameters of type int, call function 1, max (A,B,C), has three int parameter, call function 2;max (A1,B1), have two float type parameter, call function 3. This procedure is called function matching.
~ Dynamic Polymorphism ~
Dynamic polymorphism is realized by inheritance, virtual function (virtual) and pointers, and the key is the use of virtual function. The implementation mechanism of virtual function is almost always asked in C + + interview, the concrete implementation mechanism I will elaborate in the next article.
The following describes the implementation of polymorphism:
class A {public: virtualvoidfuncconst { "A :: func()" << endl; public A {public: virtualvoidfuncconst { "B :: func()" << endl; }};
Use:
A *a = B(); // 定义 A 类型的指针,指向 B 对象A -> func(); // 指针 A 指向func()函数
In the compilation period is not called any function, the compiler compiles to A->func () only to check for syntax errors, do not know whether to call the Class A or Class B func (), because a is a pointer to the B object, so a only know that it points to a type (or can be converted to a class Type) of the object. Because it is a public inheritance, B is a.
At run time, a to invoke the Func () function of the object to which it points, a command called Func () is placed on the object to which it is pointing, and the result of a refers to a B object, so the Func () function of type B is called, so the output is B::func ().
Summary:
The behavior at compile time is static polymorphism, with template and function overloading;
The behavior at run time is dynamic polymorphism, which is realized by virtual function.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Primer Learning Notes (13)--encapsulation, inheritance, polymorphism