Differences between virtual functions and pure virtual functions

Source: Internet
Author: User

A pure virtual function is defined to implement an interface and act as a standard. programmers who inherit this class must implement this function.

To implement pure virtual functions in the base class, add "= 0" after the function prototype"
Virtual void funtion1 () = 0

 

In many cases, it is unreasonable for the base class to generate objects. For example, an animal can be derived from sub-classes such as tigers and peacocks as a base class, but the object generated by the animal itself is obviously unreasonable.
To solve the above problem, the concept of pure virtual function is introduced, and the function is defined as a pure virtual function (method: Virtual returntype function () = 0 ;), the compiler requires that the class must be rewritten to implement polymorphism. Classes that contain pure virtual functions are called abstract classes and cannot generate objects.

 

The class that declares pure virtual functions is an abstract class. Therefore, you cannot create a class instance, but only an instance of its derived class.

 

Differences between 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.