"C + +" polymorphism (function overloading and virtual functions)

Source: Internet
Author: User

Polymorphism is the phenomenon that the same symbol or name has different interpretations in different situations. Polymorphism has two forms of expression:

  • Compile-time polymorphism: The same object receives the same message but produces a different function call, typically implemented by a function overload, which implements the binding at compile time, which is a static binding.
  • Runtime polymorphism: Different objects produce different actions when they receive the same message, and are usually implemented by virtual functions, which can only be implemented at run time, and are bound dynamically.
Virtual functions

A virtual function is a member function that is identified with the keyword in the base class and can be redefined in one or more derived classes. If a function is defined as a virtual function, even invoking the member function with a pointer to a base class object guarantees that the correct member function that is specific to the actual object is called. This is where the advantages of virtual functions lie.

Once the member function of the base class is defined as a virtual function, the derived class of the base class has the same member function (first name, return value type, number of arguments, and type) regardless of whether the keyword virtual is preceded or not, and it also has a virtual attribute, which is also a virtual function. Define the virtual function syntax format as follows:

Class < category name >
{
Virtual < return value type >< function name > (< formal parameter list >);
};

  virtual function mechanism : If a virtual function is accessed through a reference or a pointer, it will not determine the specific calling function at compile time, and the corresponding function implementation is invoked only at runtime based on the specific object type. Note: The virtual function's "Dynamic bound virtual attribute" must be represented by the base class's pointer to the base class, and the virtual function cannot be dynamically bound through the object call.

"C + +" polymorphism (function overloading and virtual 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.