Virtual functions and polymorphism of C + +

Source: Internet
Author: User
  • Polymorphism can be simply summed up as "one interface, many methods", the program at runtime to determine the function of the call, it is the core concept of object-oriented programming domain. Polymorphic (polymorphism), literally meaning multiple shapes.
  • A virtual function is a member function that is declared as virtual in a base class and redefined in a derived class to implement a dynamic overwrite (override) of a member function.
  • Classes that contain pure virtual functions are called abstract classes. Because an abstract class contains a pure virtual function that is not defined, an object of an abstract class cannot be defined.
  • The function of virtual function in C + + is mainly to realize the mechanism of polymorphism. In terms of polymorphism, in short, a pointer to the parent type points to an instance of its child class, and then invokes the member function of the actual subclass through a pointer to the parent class. This technique allows the parent class to have a "multiple form" pointer, which is a generic technique. The so-called generic technology, plainly, is trying to use immutable code to implement a mutable algorithm. For example: template technology, RTTI technology, virtual function technology, either try to do at compile time resolution, or try to achieve runtime resolution.
  • Any attempt to use a parent pointer to call a member function in a subclass that does not overwrite a parent class is considered illegal by the compiler, so that such a program cannot be compiled at all.
  • Polymorphism refers to the same entity having multiple forms at the same time. It is an important feature of object-oriented programming (OOP). If a language supports only classes and does not support polymorphism, it can only indicate that it is object-based, not object-oriented. The polymorphism of C + + is embodied in two aspects of running and compiling. Run-time polymorphism is dynamic polymorphism, and its specific referenced objects are determined at run time. Compile-time polymorphism is static polymorphism, which can be used to determine the form of an object at compile time. Polymorphism is implemented through virtual functions in C + +.

Virtual functions and polymorphism of 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.