C + +-virtual functions and pure virtual functions

Source: Internet
Author: User

  1. Virtual function declarations such as the following:
    Virtual returntype functionname (Parameter)
    The virtual function must be implemented, even if the null implementation, if not implemented, the compiler will error. Its purpose is to enable it to be rewritten in subclasses to be polymorphic and, of course, not to rewrite.
  2. Pure virtual function declarations such as the following:
    Virtual returntype functionname (Parameter) = 0;
    A pure virtual function must not be defined, just a function declaration, pure virtual function is used to standardize the behavior of derived classes, that is, interfaces. Classes that include pure virtual functions are abstract classes, and abstract classes cannot be instantiated, but can be used only after being inherited and overriding their pure virtual functions. Pure virtual functions must be implemented within subclasses.
  3. virtual function of the class is used to implement inheritance, the same time inherited interfaces inherit the implementation of the parent class, pure virtual function is concerned about the unity of the interface, the implementation has a subclass complete.
  4. For virtual functions, both the parent and child classes have their own version number. Dynamically bound when called by polymorphic mode.
  5. A subclass of a pure virtual function is implemented, and the pure virtual function becomes a virtual function in the subclass.
  6. Virtual functions are the mechanisms used in C + + to implement polymorphism. The core idea is to access the functions defined by the derived class through the base class.

C + +-virtual functions and pure 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.