Virtual Functions in C++

Sometimes its very difficult to understand which function would be call. So, this example explain you about how member functions are called while using virtual and without virtual.

1) Example of Virtual Function

In the case of virtual function when we call the member functions.The compiler knows which member function has to call it depends upon the type of object.

Like according to the example there is one virtual function "Show" in both the base and derived class.

And the output is :

-- I am in Test1 Class
-- I am in Test Class

First Derived class Show function called because first we pass the Derived class Object to the base class pointer. And i said earlier in case of virtual function complier knows which member function has to call depends upon the type of object.

I believe that the Virtual Function Concept can be implemented in the following way::

In your example you have used virtual keywords in the Derived class also. It does not create any problem while compiling and runnnig. But we do not require the virtual keyword in the
Derived class's function definition.

Over here I would like to show the different scenarios of Inheritance as well.

First Case::

I have inherited the Base Class by Public access specifier. The function(method) is defined under the public class access specifer in the class for both Base and Derived1 and Derived2.

Base Class pointer has no problem while getting assigned in the Derived class object, as compiler is comfortable with this kind of assignment.

I have also shown the result of the show() function written in the Base class. I have not created any object of the Base class. Only the unnamed object of the Base class help me to work with the function written in the Base class. The unnamed object can be declared as the following syntax

<Class Name>::<method Name>(i.e. the Class Name then scope resolution operator [double colon] and the method name in that class)

The compiler chooses the appropriate function at run time. Because the Base class pointer is containing the address of the Derived class object which is chosen at run time. This is called "Dynamic Binding".

Now I will make some small changes in the main function so where I will not assign the address of the Dervied Class to the Base Class Pointer and directly execute the functions with the Derived Class objects.

N.B. As I have used Bloodshed Dev-C++ as the IDE so I had to use getch() in the main function to trap the output. It won't require who are using Linux or Unix systems and please remove <conio.h> header file instruction if you are using Unix or Linux systems.

I will get the same output as above.

Second Case::

Now I will make some changes in the class definitions of the Derived Classes:

The method show() is been written in the private class specifier section of the Derived class and I have declared the object of the Base Class pointer and I have assigned each of the Derived class object one at time to execute the show() method.

The result will be just as above. Here is the definition of the classes::

Now if you do not use Pointers to the Base class object and directly use the Derived class object to get the function show() of the Derived class the compiler will start complaining, because the show() function is defined within the class scope of private.

Third Case:

Same thing will happen if the Class Access specifier is declared as protected for the member function show() in the Derived classes Derived1 and Derived2 and the base class is inherited as public access specifier and in the main function if through the Base class pointer derived class object is accessed.

Fourth Case:
Other than public class access specifier for inheriting the Base class to the Derived class if protected or private access specifier is used for inheritance then nothing can be done. Because Base class objects will be inaccessible to the Derived class.

The other way round of the problem is if in the Base class the methods are written in the private class access specifier then similar kind of thing will happen.

Why we are using virtual functions here? What do you think?
The answer is: we need the ability to call say() function from the Animal class which knows really nothing about those who derive from it.
The example of how to use these classes is:

We may use thousands of animals here and still we call say() function from the array of animals and get different results depending on what animal exactly it is.
Ask me, if something is unclear in the examples above.