Summary of C + + review points--polymorphism (i)

Source: Internet
Author: User
A polymorphic understanding:

is an object-oriented new requirement:

To determine the invocation of an overriding function based on the actual object type

Call a function defined in the parent class if the parent pointer points to a parent class object

If the parent pointer is to a child class object, the overriding function defined in the child class is called

Solution:

Support for polymorphism with the virtual keyword in øc++

Ø a function that uses the virtual declaration can be overridden to show polymorphic properties

Object-oriented 3 big concept

Packaging

Break the concept of C language function.

Inherited

Code reuse .... I re-use the original written code ...

Polymorphic

Polymorphism can be used in the future ..... The 80 's wrote a framework ... Code written by the 90 generation

Polymorphism is a goal that our software industry pursues ...

Write a framework that can call Yimeimei, write the ability of the Code

A further understanding

3 conditions for the establishment of indirect assignment:

1 definition of two variables ...

2 Establishing an association ....

3 *p

Three conditions for polymorphic formation:

1 to have inheritance

2 to have a function rewrite ... C virtual function

3 to have a parent class pointer (parent class reference) to a subclass object

Polymorphism is the basis of design pattern, polymorphism is the foundation of frame

Knowledge point 1 Virtual destructor

Destructors are designed to release resources,

When resources are freed, there is a need to not directly dispose of objects such as:

c *MYC = new C; C inheriting b,b inheriting Class A

Delete MyC; Free resources directly from subclass objects This situation does not need to write the virtual keyword

You need to execute the destructor of all the subclasses of the object through the parent class pointer once

You want to release all the subclass resources through the parent class pointer (you need to add the virtual keyword to the destructor of the final parent class)

void Howtodelete (A *base) {delete base;//This sentence does not appear polymorphic this property}

Knowledge point 2: overloading and overriding differences

function overloading

Must be in the same class

A subclass cannot overload a function of the parent class, and a function with the same name as the parent class will be overwritten by the names (for example, there is a function a () in the parent class, and a function a () in the subclass, but the function a (int b) is the overloaded parent function that produces an error! Can't appear! )

Overloading is the function call that is determined by the type and number of arguments during compilation

function rewriting

Must occur between the parent class and the child class

and the parent class must have exactly the same prototype as the function in the child class.

Can generate polymorphism after using the virtual declaration (if you don't use virtual, it's called redefinition)

Polymorphism is the function call that is determined by the type of the specific object during the run

Example Analysis:

The 1 C + + compiler sees the Func name, and the Func name in the Factor class already exists (name overrides). So the C + + compiler will not look for the Func function of the parent class's 4 arguments//2 C + + compiler will only find the Func function in the subclass, find two func, one 2 arguments, One is 3 parameters.//3 C + + compiler starts error ... error C2661: "Child::func": no overloaded function accepts 4 parameters//4 if you want to invoke the func of the parent class, you can only add the domain name of the parent class. This goes to call: C1.func (1, 3, 4, 5);//c1.func ();//func the name of the function, the name overrides in the subclass, the name of the function of the subclass, the position of the name of the function that occupies the parent class, or the overloaded form of the Func name already in the subclass .... The compiler starts looking for the Func function in the subclass .... But the Func function with no 0 parameters

3 Highlights:

The implementation principle of polymorphism in C + +

When a virtual function is declared in a class, the compiler generates a virtual function table in the class

A virtual function table is a data structure that stores a pointer to a class member function

Virtual function tables are automatically generated and maintained by the compiler.

The virtual member function is placed in the virtual function table by the compiler

When a virtual function exists, each object has a pointer to the virtual function table (vptr pointer)

Illustration 1:

Calling an overriding function through the virtual function table pointer vptr is done when the program runs, so you need to address it to determine which function is really supposed to be called. The normal member function is a function that determines the call at compile time. In efficiency, the efficiency of virtual function is much lower.

Illustration 2:

For efficiency reasons, it is not necessary to declare all member functions as virtual functions

Description 3:c++ compiler, execute howtoprint function, do not need to distinguish between child class object or parent class object

The above is the summary of C + + review points of the ten-polymorphic (a) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.