How to use C ++ virtual functions and pure virtual functions

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 containing only virtual functions are called abstract classes) cannot be called abstract class ).

2. virtual functions can be directly used, or they can be called in the form of polymorphism after being overloaded by subclasses. Pure virtual functions must be called in sub classes) can be used only when this function is implemented, because the pure virtual function is in the base class)
Only declarations are not defined.

3. Both virtual and pure virtual functions can be overloaded in sub class and called in the form of polymorphism.

4. virtual functions and pure virtual functions are usually stored in abstract base class-ABC. The inherited subclass is overloaded to provide a unified interface.

5. virtual function definition form: virtual {method body}
Definition of pure virtual functions: virtual {} = 0;
There cannot be a static identifier in the definition of virtual and pure virtual functions. The reason is very simple. The static-modified functions require bind in the early stage during compilation, however, virtual functions are dynamically bound (run-time bind), and the life recycle function modified by the two is different.
6. if a class contains pure virtual functions, any statements that attempt to instantiate the class will produce errors, because the abstract base class (ABC) cannot be directly called. The quilt class must inherit from the overload and then call its subclass method as required.

The following is a simple demonstration of virtual functions and pure virtual cold numbers!

# Include <>
// Father class
Class Virtualbase
{
Public:
Virtual void Demon () = 0; // prue virtual function
Virtual void Base () {cout <"this is farther class" <endl };
}

// Sub class
Class SubVirtual: public Virtualbase
{
Public:
Void Demon () {cout <"this is SubVirtual! "<Endl };
Void Base () {cout <"this is subclass Base" <};
}
/* Instance class and sample */
Int main ()
{
Virtualbase * inst = new SubVirtual (); // multstate pointer
Inst-> Demon ();
Inst-> Base ();
}

Differences between virtual functions and pure virtual functions
Viewpoint 1:
If the class is declared as a virtual function, this function is implemented. Even if it is a null implementation, it is used to make this function be reloaded in its subclass. In this case, in this way, the compiler can use later binding to achieve polymorphism.
A pure virtual function is just an interface and a declaration of a function. It must be left in the subclass for implementation.
Class {
Protected:
Void foo (); // common class function
Virtual void foo1 (); // virtual function
Virtual void foo2 () = 0; // pure virtual function
}

Opinion 2:
Virtual functions can not be overloaded 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.

Opinion 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, which cannot directly generate objects (otherwise, an error will occur !) It can be used only after it is inherited and its virtual function is rewritten. Such classes are also called abstract classes. Virtual functions are used to inherit interfaces and default behaviors.
Pure virtual functions only inherit interfaces and must be redefined.

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.