Polymorphism, virtual function, pure virtual function

Source: Internet
Author: User

1. Virtual functions and pure virtual functions can be defined in the same class, classes containing pure virtual functions are called abstract classes, and classes that contain only virtual functions (class) cannot be called abstract classes.

2. Virtual functions can be used directly, or the Quilt Class (sub Class) overloads are later called in polymorphic form, whereas pure virtual functions must be implemented in subclasses (sub Class) in order to be used, because pure virtual functions are only declared and not defined in the base class.

3. Virtual and pure virtual functions can be overloaded in subclasses (sub Class) and called in polymorphic form.

4. Virtual functions and pure virtual functions usually exist in the abstract base class (abstract base CLASS-ABC) and are inherited subclasses overloaded to provide a uniform interface.

5. Virtual function definition form: Virtual {method Body}

The definition of pure virtual function: virtual {} = 0;

There cannot be a static identifier in the definition of a virtual function or a pure virtual function, for the simple reason that the static modified function requires a prior bind at compile time, but the virtual function is dynamically bound (run-time bind) and is modified by both the function lifecycle (Life recycle ) is not the same.

6. The virtual function must be implemented, if not implemented, the compiler will give an error:

Error lnk****: unresolved external symbol "Public:virtual void __thiscall
Classname::virtualfunctionname (void) "

7. For virtual functions, both the parent and child classes have their own versions. Dynamically bound when called by polymorphic mode.

8. A subclass of a pure virtual function is implemented, and the virtual function is programmed in the subclass, and the subclass of the subclass, that is, the grandson class can override the virtual function and bind dynamically when called by the Polymorphic method.

9. Virtual functions are the mechanisms used to implement polymorphism (polymorphism) in C + +. The core idea is to access the functions defined by the derived class through the base class

10. Polymorphism refers to different implementation actions when the same object receives different messages or different objects receive the same message. C + + supports two kinds of polymorphism: compile-time polymorphism, run-time polymorphism.

A. Compile-time polymorphism: using overloaded functions
B Run-time polymorphism: implemented by virtual functions.

11. If a class contains pure virtual functions, any statements attempting to instantiate the class will result in an error, because the abstract base class (ABC) cannot be called directly. You must invoke a method of its subclass after the class inherits the overload, as required.

Polymorphism, virtual function, pure virtual function

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.