Virtual Function in C++

Virtual Function in C++

In this post let’s see the concept of virtual function in C++.

When user use the same function name in both the base and derived classes, the function in base class is declared as virtual using the keyword “virtual” preceeding the normal declaration. When a function is made virtual, C++ determines which function to use at runtime on the basis of type of object base pointer points. Thus by making the base pointer to point to two different objects, it can execute different versions of virtual function. We can access virtual function by using a pointer poiting to the base class. Also the prototype of the base class version of a virtual function and all the child class versions must be identical. If two functions with the same name have different prototypes, C++ considers them as overloaded functions. Thus it ignores the virtual function mechanism.

Now let’s see some rules to declare virtual functions.

Rules to declare virtual functions.

1. The virtual functions must be members of some class.
2. they cannot be static.
3. We can access them by using object pointers.
4. A virtual function can be a friend of another class.
5. A virtual function must have a definition in the base class, even if we don’t use it anywhere in the program.
6. We cannot have virtual constructors, but we can have virtual destructors.
7. A base pointer can point to any type of derived object, but the reverse is not true. i.e. we cannot use a pointer to child class to access an object of the abse class.
8. When base pointer points to child class, the incrementation or decrementation is only relative to its base type.
9. We don’t need to define virtual functions in child class if they are already present in the parent (base) class.

To understand the concept more clearly, let’s see a program on virtual functions.

Q. Write a program in C++ to demonstrate virtual function in C++.

Code :

//without using the virtual keyword
#include <iostream>  

using namespace std;  

class A  
{  
   int x=5;  
    public:  
    void display()  
    {  
        std::cout << "Value of x is : " << x<<std::endl;  
    }  
};  

class B: public A  
{  
    int y = 10;  
    public:  
    void display()  
    {  
        std::cout << "Value of y is : " <<y<< std::endl;  
    }  
};  

int main()  
{  
    A *a;  
    B b;  
    a = &b;  
   a->display();  
    return 0;  
}  

Output :

Virtual Function in C++

Code :

//using virtual keyword
#include <iostream>  

using namespace std;  

class A  
{  
   int x=5;  
    public:  
    virtual void display()  
    {  
        std::cout << "Value of x is : " << x<<std::endl;  
    }  
};  
class B: public A  
{  
    int y = 10;  
    public:  
    void display()  
    {  
        std::cout << "Value of y is : " <<y<< std::endl;  
    }  
};  
int main()  
{  
    A *a;  
    B b;  
    a = &b;  
    a->display();
    return 0;  
}  

Output :

Virtual Function in C++

Program Parts :

1. #include is a pre-processor directive. It is used to include header files.
2. iostream is header file which has certain commands that c++ supports. E.g. return, main, etc.
3. .h is an extension for header file.
4. class is a keyword to create classes.
5. virtual is a keyword to declare functions as virtual.
6. int is data type. It indicates that the program returns a value.
7. main() is main function that indicates the compiler that the user written programs starts from here.
8. cout is the keyword we use to print a message.
9. cin is the keyword we use to store values in variables.
10. return statement is use to return a value. If any error occurs the program will return 0.

Explanation :

In first program (without virtual keyword), the base pointer points to the base class function. Thus it prints the value of x which is 5.

But in second program, which has virtual keyword before display(), the base pointer refers to the child class (B class). And hence the code prints the value of y which is 10.

Conclusion

So this was the concept of virual function in C++. Hope it was clear to you. If you have doubt regarding any of the concept or this post do tell us in the comment section. Also for daily updates do follow us on Instagram. Visit again!

Keep Programming!

ErrorFreeProgram.

Leave a Reply

Your email address will not be published. Required fields are marked *