Overlay, polymorphic in C + +

Source: Internet
Author: User

One, virtual function, coverage, polymorphism
Virtual function: The member function adds the virtual keyword to the definition, which is called a virtual function
Overwrite: If the implementation in the subclass has the same function as a virtual function in the parent class, the member function in the child class overrides the member function in the parent class
Polymorphic: If a member function in a subclass overrides a member in a parent class, when a pointer to the parent class of a child class or a reference to a parent class reference to a subclass, uses it to invoke a virtual function, and then calls the override function in the subclass based on the actual calling object, rather than the virtual function in the parent class, this syntax is called polymorphic

The meaning of polymorphism is that the same species emit the same kind of invocation and produce different reactions


Second, cover, reload, hide
Conditions to satisfy overrides (Overrides):
A, must be a member function
B. Must be a virtual function
C, the function signature must be the same
D, must be the same if the return value is a basic type (otherwise a compilation error will occur)
E, if the class type must be a pointer or reference to a parent-child class relationship (must be able to process automatic type conversion)

Conditions to satisfy overloading:
A. must be under the same scope
b, the function name is the same but the parameter list is different
C, the const attribute is the same
D, the type of the return value does not affect overloading

To meet hidden conditions:
Identifiers that have the same name between parent and child classes, as long as they form an overlay, must constitute a hidden

Iii. conditions of polymorphism
1, polymorphic properties in addition to the parent-child class to form an overlay, must also be the parent class as a pointer or reference to the child class

2, when the pointer or reference has formed a polymorphic, this time the call member passed the this pointer and then call the member function can also form a polymorphic

3. The constructor of the parent class is called before the constructor of the subclass is executed, and if the overridden virtual function is called, only the virtual function in the parent class can be called because the child class has not yet been constructed.
When the constructor is executed in the function body, the resources seen in the class are all constructed.

4. The destructor of the parent class is called after the destructor of the subclass is completed, and if the virtual function is called, the virtual function can only be called in the parent class, because the child class has already been destroyed by the complete subclass.


Four, pure virtual function and abstract class
1. Pure virtual function
Class A
{
Public:
virtual void Test (void) = 0;
virtual void Test (void) const=0;
}
A, pure virtual functions do not need to be implemented, if not to be implemented or in a class, must be outside the class (virtual function)
b, pure virtual function if you want to call must be overridden in a subclass, and then called in a polymorphic way

2. Abstract class
A class that has a pure virtual function in a member function is called an abstract class, which cannot create an object.
If a subclass inherits an abstract class, it must overwrite the pure virtual function in the parent class, otherwise it becomes an abstract class that cannot be manifested.
Abstract classes can therefore call their own non-pure virtual functions only by pointing to subclasses as pointers or as references

3. Pure abstract class
All of the member functions are pure virtual functions, this class is called pure virtual function
Four characteristics of object-oriented: abstraction, encapsulation, inheritance, polymorphism
A pure abstract class can be a process of encapsulating a class, while an abstract class can also be used as a unified interface class

4, Pure abstract class application scenario
Callback mode:
Function A is accomplished by the programmer xiaoming, if at this time he wants to invoke the function B of the small implementation of 1970
Function B is done by the programmer's little Light, which was born in 1980.
Command mode:
Enter an instruction and perform the corresponding action
Producer and Consumer models
Singleton mode (a Hungry man, lazy)
Factory mode:
A class is dedicated to the manufacture of other classes, a programming pattern called the Factory mode
MVC pattern

V. Coercion type conversions in C + +
1. Forced type conversion in C language can continue to be used, but not secure
2, C + + coercion type conversion use is very troublesome, in fact, the father of C + + does not recommend the use of forced type conversion, once the code needs to use coercion type conversion description code design unreasonable, forced type conversion is a way to mend

static_cast Static type conversions
1, self-health type of forced conversion
2. Conversion of void* and other types of pointers
3. Pointer conversion between parent and child types

Const_cast de-constant type conversion

reinterpret_cast re-interpreting type conversions
Type conversions between integers and pointers

dynamic_cast Dynamic type Conversion
Pointer type conversions between parent and child classes that make up polymorphic

VI. virtual function table
1, what is a virtual function table, when there is a virtual function in a class, the compiler will assign a table for this to specifically record these virtual functions, in the class there will be a hidden pointer member to point to this table
2. How to prove the existence of this table
Classes with virtual functions are 4 bytes more than classes without virtual functions (the same), and there are added and aligned
3, a class has only a virtual function table, all objects share a virtual function table
4. The first four bytes of a generic object are pointer variables that point to the virtual function table

Vii. Dynamic Type Binding
1. When a pointer or reference to a parent class is used to point to a subclass, the compiler does not immediately generate a pointer to the calling function, and a piece of code is generated to check what type of real object the pointer is pointing to
2. When the code is actually running, the pointer to the virtual function is found through the object's pointers
3, then access the virtual function table through the member pointer, and then find the calling function address from
4. Using polymorphism will result in additional code and calls, so using polymorphism will slow down the execution of your code

VIII. Classes of information
1. In C + +, you can use typeID to get some information about a class to determine what class the pointer is pointing to
2, typeID can be used directly to determine whether the same type
typeID (identifier) ==typeid (identifier)
3, typeID (identifier). Name () Gets the type name, rendered as a string
4, if the typeid (pointer) can only get to the type of pointer, typeid (* pointer) can get the actual type information of the object

Nine, virtual destruction
1. If you point to a subclass object through a parent pointer or reference, when you use Delete to dispose of the object, you can only call the parent class's destructor at this point, and if you use the memory resource requested by New/malloc in the subclass, it will cause a memory leak

2. The solution is to set the destructor of the parent class to a virtual function

3, in the design class when the destructor does not need to do anything, the compiler will also generate an empty destructor, but this will let inherit its subclasses have security implications

4, it is best to set all the destructors are virtual functions


Overlay, polymorphic in C + +

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.