Virtual and pure virtual functions

Source: Internet
Author: User
Virtual functions are defined in the base class to meet the needs of overload and polymorphism. Even if the definition is null, this function can be rewritten in the subclass! Pure virtual functions are not defined in the base class and must be implemented in the subclass, much like interface functions in Java! Reason for introduction of virtual functions: to facilitate the use of polymorphism, we often need to define virtual functions in the base class. Class CMAN {public: Virtual void eat (){......}; Void move (); Private :}; class cChild: Public CMAN {public: Virtual void eat (){......}; PRIVATE:}; CMAN m_man; cChild m_child; CMAN * P; // This is the essence of use. If the pointer of the base class is not necessarily used, it does not make much sense. P = & m_man; p-> eat (); // always call the Eat member function of CMAN. It does not call p = & m_child; P-> eat () of cChild (); // If the subclass implements (overwrite) This method, it always calls the cChild eat function // it does not call the CMAN eat method; if the subclass does not implement this function, call the CMAN eat function p-> move (); // The member function is not in the subclass, so the reason for the introduction of pure virtual functions in the base class is called: 1. Similar to "virtual functions". 2. 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. A pure virtual function is a base class that only defines the function body and has no implementation process. The definition method is virtual void eat () = 0. do not define it in CPP. A pure virtual function is equivalent to an interface, if the instance cannot be used directly, a derived class is required to implement function definition. Some people may wonder how to define these functions. I think they are very useful, for example, if you want to describe the attributes of some things to others and do not want to implement them, you can define them as pure virtual functions. More thorough. For example, if you build a building, you are the boss, and you give the construction company a clear description of the characteristics of your building, how many floors, and whether there is a garden or something on the roof, the construction company can follow your methods. If you do not know this clearly, the construction company may not understand the characteristics of the buildings you need. The division of labor can be very good with purely needed functions. Differences between virtual functions and pure virtual functions 1: If the class is declared as a virtual function, this function is implemented, even if it is empty, it is used to enable this function to be reloaded in its subclass. In this way, the compiler can use later binding to achieve polymorphism. Pure virtual functions are just an interface, it is just a declaration of a function, and it should be left in the subclass for implementation. Class A {protected: void Foo (); // common class function virtual void foo1 (); // virtual function virtual void foo2 () = 0; // pure virtual function} viewpoint 2: virtual functions can also be reloaded in the subclass, but pure virtual functions must be implemented in the subclass, just like Java interfaces. It is a good habit to add virtual functions to many functions. Although some performance is sacrificed, the object-oriented polymorphism is increased, it is hard to predict that the function in the parent class does not modify its implementation in the subclass. 3. The class of the virtual function is used to implement inheritance ", the inherited interface also inherits the implementation of the parent class. Of course, we can also complete our own implementation. Classes of pure virtual functions are used for interface inheritance and are mainly used for communication protocols. Focus on the uniformity of interfaces, and the implementation is completed by sub-classes. Generally, only pure virtual functions are involved in the interface class. Viewpoint 4: classes with pure virtual functions are called virtual base classes. These base classes cannot directly generate objects. They can only be used after they are inherited and overwritten. Such classes are also called abstract classes. Virtual functions are used to inherit the interface and default behavior. Pure virtual functions only inherit the interface and the behavior must be redefined.

For projects that use an I _xxxstruct, all of them are pure virtual functions, and then modules that use interfaces to call each other are not familiar with each other. Specifically, I do not understand the advantages of this method,

C ++ is still unfamiliar with object-oriented programming.

Virtual 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: 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.