I am not able to understand the concept of virtual functions. When a member function of base class is redefined in derived class,the redefined function can be used to get desired output. Then why there is a need to use virtual functions? For instance, the following two codes produce the same output.

Code 1:

#include<iostream>
using namespace std;
class Window 
     {
       public:
          virtual void Create() 
          {
               cout <<"Base class Window\n";          }
     };

     class CommandButton : public Window
     {
       public:
          void Create()
          {
              cout<<"Derived class Command Button - Overridden C++ virtual function\n";          }
     };

     int main()
     {
         Window  *x, *y;

         x = new Window();
         x->Create();

         y = new CommandButton();
         y->Create();
     }

Code 2:

#include<iostream>
using namespace std;
class Window 
     {
       public:
           void Create() 
          {
               cout <<"Base class Window\n";          }
     };

     class CommandButton : public Window
     {
       public:
          void Create()
          {
              cout<<"Derived class Command Button - Overridden C++ virtual function\n";          }
     };

     int main()
     {
         Window  *x;
         CommandButton *y;
         x->Create();
         y->Create();
     }

In code 2 I have not used virtual function. When the mere redefining of function could achieve desired goal why virtual functions are used?

Agni commented: Asking a question with good examples is the best way ! +8

As you have with your first example you declared 2 Window pointers and made one a Window and one a CommandButton. In your second example you had to have a Window and a CommandButton pointer. So having virtuall functions allow you to cast a derived object to the base and still have the function of the derived class called. This is convient because now you can have an a array of Window pointers and and as long as the class is dervied from Window you can add it to the array. then going throught the array you can call one function and will get the right one called.

Think of it this way:

You have a base class named Button,
that has a Virtual void Action() method.

And lets they you have 3 derived classes: Enter, Space, Numpad,
that are being held in a Button ButtonArr[3].

Each of the buttons has its own action.

void Enter::Action(){
    cout<< "Enter\n";
}

void Space::Action(){
    cout<< "Space\n";
}

void Numpad::Action(){
    cout<< "Numpad On or Off\n";
}

Now check what happens when you use the following code:

for(int i=0;i<3;i++){
    ButtonArr[i].action();
}

when the Button action function is not a virtual function
and what happens when it is.

If you are still not convinced why to use Virtual:

Imagine making a video game with hundreads of different enemies.
each has its own moving technique according to a certain AI
using a for loop you can make them all move
instead of calling the move function of each of them.

good luck

Also when you declare a non-virtual function in a child class which has the same name as the base non-virtual function you end up 'hiding' the base function which might not work as you expect. The base class function is not accessible in the derived object now. Even if the function signature was different. Can read more here Hiding C++

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.