C ++ virtual functions related summary (entry)

C ++ virtual functions which involve the C ++ features: Polymorphism

Here a review of the three C ++ language features: encapsulation, inheritance and polymorphism.
The package may hide implementation details, such code is modular, can inherit an existing expansion modules.
The goal is to code reuse , and polymorphism to the interface reuse . Polymorphism is achieved through the virtual function

a. encapsulating
the encapsulated data and the process to determine which features are visible and which are hidden properties, to ensure the independence of the module, the confidentiality of information, improving program reusability and maintainability.

B. Inheritance
refers to the process of obtaining a target function from another object subclass can be obtained by the same property class inheritance, you can improve code reusability, but also to produce the relationship between the classes, as a multi-state characteristics of the base.

c. polymorphic
polymorphic short, it is the presence of a certain type of thing a variety of forms, under the premise that there is a relationship between class and class, greatly improve the scalability of the program.

Second, virtual function
virtual function must be non-static member function in the base class, i.e. in front of the base class member function with the keyword virtual, for example the use of
class Father
{
public:
Virtual void RUN ()
const {<< COUT "father can run ten thousand meters \ n"};

}
In front of a virtual function declared added keyword indicates that the function is a variety of forms, the function can be owned by a plurality of objects, and different functions.

About three virtual function, several points should be noted:
1. The static binding and dynamic binding
in C ++, refers to build portions of the computer program compiled, linked, forming a process executable program, build points for the static binding and dynamic binding.

Static binding refers to build the work is carried out in connection compile phase, also known as static binding early build, because this build is completed before the program begins.
Features:
call speed, high efficiency

Dynamic binding means that the compiler does not know exactly the function that will be called, only to determine the function that will be called during program execution, this should know exactly the function that will be called, requires at compile build job running the program when , this build work performed at program runtime is called dynamic binding. C ++ provides: Dynamic binding is supported virtual functions implemented in.
Features:
flexibility, abstract problems and issues of ease of maintenance.

2. Relationship constructors, destructors and virtual functions

a. constructor can not be virtual functions, other than any non-static function constructors can be virtual, the virtual function pointer is called, to be used first by the virtual function instance of the compiler.

b. destructor can be virtual, but usually have to define a virtual base class destructor , if you do not, after the object of a derived class to generate new, delete, when the base class will call out structure function, it does not invoke the derived class destructor. In this way, the application of resources in a derived class will not be released, it will cause a memory leak, which is quite dangerous: If there are a large number of derived class object is created and destroyed like this, there will continue to leak memory, Over time, the system will collapse because of lack of memory.

Guess you like

Origin blog.csdn.net/alexhu2010q/article/details/80193275