C ++ Polymorphism

Source: Internet
Author: User

Polymorphism isBinding during runtimeMechanism to bind the function name to the specific implementation of the function.Code. The starting address of a function in memory is called the address of this function.Entry address.

Polymorphism is the runtime binding mechanism that dynamically binds the function name to the function entry address.

C ++Runtime bindingAndBinding during compilation

The name of a function is closely linked to its entry address, which is the starting address of the function in the memory.

# Include <iostream>

Usingnamespace STD;

Void sayhi ();

Int main (){

Sayhi ();

Return 0;

}

Void sayhi (){

Cout <"Hello, wonderful world !" <Endl;

}

During compilation, the code to be executed by the function is determined by the compiler in the compilation phase. That is to say, a function name and its entry address are bound together.

BIND at runtimeProgramThe function name is bound to its entry address only at runtime (not during compilation.

If a function is bound at the runtime rather than the Compilation Time, the function is called a multi-state function.

Notes:

Ü a member function declared as a virtual function in the base class, even if it is not explicitly declared as a virtual function in a derived class after it is inherited, it is also automatically considered as a virtual function. Ü virtual functions are defined outside the class declaration. The virtual keyword is only required in the function declaration and does not need to be used in the function definition. Ü C ++ only allows defining member functions as virtual functions. Top-level functions cannot be virtual functions.

Virtual member function inheritance

Like common member functions, virtual member functions in a derived class can also be inherited from the base class.

Runtime binding and virtual member function tables

C ++ uses vtable (Virtual member function table) to bind virtual member functions at runtime.

Using a dynamically bound program will affect the efficiency, because the virtual member function requires additional storage space and it takes additional time to query the virtual member function table.

Heavy Load

The difference between a overload function and a virtual function is that the former is bound during compilation and the latter is bound during runtime.

Abstract base class and pure virtual member functions

Abstract base classes cannot be instantiated. They can be used to specify some virtual functions that must be overwritten by the derived classes, so that some classes have common interfaces, they all inherit from an abstract base class.

Class Abstract

{

Pulbic:

Virtual void open () = 0;

};

Limits on defining pure virtual member functions

Only virtual functions can become pure virtual member functions. Non-virtual functions or top-level functions cannot be declared as pure virtual member functions.

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.