C ++ virtual functions and pure virtual functions

Source: Internet
Author: User

Today we will talk about the usage and differences between virtual functions and pure virtual functions in C ++. The proposal of virtual functions has been mentioned in my previous article on C ++. It is proposed to solve the ambiguity of Multi-inheritance. I will not go into details today; pure virtual functions are special functions. They are used to describe pure virtual functions without meaningful implementations in the base class, its implementation is left to the derived class of the base class. Classes that contain pure virtual functions are abstract classes. abstract classes cannot be instantiated, but you can define pointer variables pointing to abstract class data. After a derived class is called a specific class, you can use this pointer to point to the object of the derived class, and then call the virtual function through this pointer to implement multi-state operations. The writing format of virtual and pure virtual functions is similar to the following: [cpp] class Student {virtual char * test (); // virtual function virtual char * Category () = 0; // pure virtual function}; class Student {virtual char * test (); // virtual function virtual char * Category () = 0; // pure virtual function }; virtual functions can be called directly, or the secondary subclass can be called in the form of polymorphism after being overloaded. Pure virtual functions must be implemented in the subclass before they can be used, because pure virtual functions are declared only in the base class and are not defined. Virtual functions and pure virtual functions do not have static identifiers. The reason is very simple. The static modified functions must be bound before compilation. However, virtual functions are bound dynamically, in addition, the function lifecycles modified by both are different. If a class contains pure virtual functions, any statements that attempt to instantiate the class will produce errors because the abstract base class cannot be called directly, the subclass method must be called as required after the subclass is integrated with the overload. [Cpp] class StudentA {public: virtual void funOne () = 0; // The fun function is a pure virtual function virtual void funTwo () ;}; class StudentB {public: virtual void funOne (); virtual void funTwo () ;}; // CA, CB class implementation... void main () {StudentA xiaoliu; // not allowed, because class CA has pure virtual function StudentB xiaoxin; // yes, because class CB does not have pure virtual function ...} class StudentA {public: virtual void funOne () = 0; // The fun function is a pure virtual function virtual void funTwo () ;}; class StudentB {public: virtual void funOne (); virtual void funTwo () ;}; // CA, CB class implementation... void main () {StudentA xiaoliu; // not allowed, because class CA has pure virtual function StudentB xiaoxin; // yes, because class CB does not have pure virtual function ...}

Related Article

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.