C + + notes--Virtual functions

Source: Internet
Author: User

Simply put, a virtual function is a member function that is modified by virtual. The goal is to realize the polymorphism of the class and need to understand a few questions:

1. A function is a virtual function, not to say that the function is not implemented;

2. The purpose of defining a function as a virtual function is to invoke a function of the same name derived from the base class;

3. If a function is defined as a pure virtual function, this function is not implemented and requires a subclass implementation.

To illustrate:

classA { Public:     Virtual voidfoo () {cout<<"a::f ()"<<Endl; } }; classD | PublicA { Public:     voidfoo () {cout<<"b::f ()"<<Endl; } };voidMain () {b b;        B.foo (); //1. Sub-class Call function((A) b). Foo (); //2. Converting to a base class callA* p = &b; P->foo ();//3. Calling through a base-class pointerA;        A.foo (); //4. Base class invocation}

the results are as follows :

Although the base class has an implementation of the virtual function foo (), its derived class will still call its own Foo function.

The Foo function of the base class is called by forcing the type conversion to turn the derived class into a base class.

A function with the same name as a derived class is called from a base class and needs to be called as a pointer.

Pure virtual function:

A pure virtual function is a virtual function declared in a base class that is not defined in the base class, but requires that any derived class define its own implementation method. The method of implementing a pure virtual function in a base class is to add "= 0" after the function prototype

To facilitate the use of polymorphic features, we often need to define virtual functions in the base class.
In many cases, the base class itself generates objects that are unreasonable. For example, animals as a base class can be derived from tigers, peacocks and other sub-categories, but the animals themselves generated objects are obviously unreasonable.
In order to solve the above problems, the concept of pure virtual function is introduced, the function is defined as pure virtual function (method: Virtual ReturnType functions () = 0;), the compiler requirements must be overridden in the derived class to achieve polymorphism. A class that contains a purely virtual function is called an abstract class, and it cannot produce an object. This is a good solution to both of these problems.
A class that declares a pure virtual function is an abstract class. Therefore, the user cannot create an instance of the class, only instances of its derived classes can be created.
The most notable feature of pure virtual functions is that they must re-declare the function in the inheriting class (not the next = 0, otherwise the derived class cannot instantiate), and they are often not defined in the abstract class.
The purpose of defining a pure virtual function is to make a derived class simply an interface to an inherited function.
The meaning of pure virtual functions, so that all class objects (primarily derived class objects) can perform actions of pure virtual functions, but the class cannot provide a reasonable default implementation for pure virtual functions.

Let's look at an example:

 class   A { public  :  virtual  void  foo () = 0 ; //  defined as pure virtual function   class  B: public   A { public  :  void   Foo () {cout  <<"  b::f ()   <<ENDL; } };
A;            //  b b; ((A) b). Foo ();     // Error

The correct method to invoke is:

b b; B.foo ();         // 1. Sub-class Call function A* p = &B; p->foo ();        // 2. Calling through a base-class pointer

Results :

Summary :

1. A virtual function allows a derived class to inherit the interface function of the base class, invoking the function of the derived class through the base class, by means of pointers.

2. Classes containing pure virtual functions are abstract classes and cannot be instantiated, and derived classes must implement this pure virtual function.

3. The destructor should be a virtual function, and the destructor for the corresponding object type will be called, so if the pointer is to a subclass object, the destructor for the subclass is called and the destructor for the base class is automatically called.

C + + notes--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.