AppDividend
Latest Code Tutorials

Virtual Function in C++ Example | C++ Virtual Function Tutorial

0

Virtual Function in C++ Example | C++ Virtual Function Tutorial is today’s topic. A virtual function is a member function which is declared in the base class and redefined by a derived class. This function is used to tell the compiler to perform dynamic linkage or late binding (means, the compiler determines the type of object it is having and then it binds the function call) on the function. We have to create a pointer to the base class, and then that pointer refers to the object of the derived class.

Virtual Function in C++

Polymorphism refers to a property by which objects belonging to a different class can respond to the same message but in the various forms. When there are C++ functions with the same name in both superclass as well as a subclass, virtual functions gives programmer capability to call a member function of a different class by the same function call based upon different context.

Virtual functions ensure that a correct function is called for the object, regardless of an expression used to make a function call.

Suppose the base class contains the function declared as a virtual, and the derived class defines the same function. The function from a derived class is invoked for the objects of a derived class, even if it is called using the pointer or reference to the base class.

They are mainly used to achieve the Runtime polymorphism. Functions are declared with the virtual keyword in the base class. The resolving of a function call is done at the Run-time.

If we want to declare a virtual function, the virtual keyword is used.

#Concept non-virtual function

Before knowing the virtual function, we first have to know what happens when we don’t use the virtual function. First, refer to the below function.

#include<iostream>
using namespace std;
//Bank is Base Class
class Bank
{
    public:
        //declaring non-virtual function here
        void display()
        {
            cout<<"\nI am Reserve Bank of India";
        }
};
//SBI is derived class
class SBI : public Bank
{
    public:
        void display()
        {
            cout<<"\nDetails of SBI is here";
        }
};
int main()
{
    //creating pointer of base class
    Bank *ptr;
    //Creating object of derived class
    SBI S;
    ptr=&S;
    //Trying to printing details, see which one displays
    ptr->display();
    return 0;
}

See the following output.

 

Virtual Function in C++

In the above example, we can see that *ptr is the pointer of the base class. This pointer can only be accessed by the based class, not the members of the derived class members.

#Virtual Function Concept

If you want to overcome the above situation, we virtual function. Using the virtual function, we can access the members of the derived class. See the below example to understand the concept.

#include<iostream>
using namespace std;
//Bank is Base Class
class Bank
{
    public:
        //declaring virtual function here
        virtual void display()
        {
            cout<<"\nI am Reserve Bank of India";
        }
};
//SBI is derived class
class SBI : public Bank
{
    public:
        void display()
        {
            cout<<"\nDetails of SBI is here";
        }
};
int main()
{
    //creating pointer of base class
    Bank *ptr;
    //creating object of derived class
    SBI S;
    ptr=&S;
    //Trying to printing details, see which one displays
    ptr->display();
    return 0;
}

See the following output.

 

Concept non virtual function

So from the above example, we can see that the compiler determines the type of an object at run time and then it is calling the appropriate function.

#Rules of virtual function

  1. This function cannot be static and can not be a friend function of another class.
  2. It must be a member of some other class.
  3. This function can be accessed through object pointers.
  4. The virtual function must be declared in base class only.
  5. This function should be accessed using pointer or reference of base class type to achieve runtime polymorphism.
  6. We cannot have the virtual constructor, but we can have the virtual destructor.
  7. The prototype of this type of functions should be the same in the base as well as derived class.

#Concept of Pure Virtual Function

A virtual function, which is not used to perform any task, only serves as a placeholder, is called Pure Virtual Function. Declaration of Pure Virtual Function ends with = 0 only. See the following code.

class Student
{
	public:
	   virtual void details()=0;
};

This function is also called doing nothing function because although it is declared in the base class, has no definition.

In this function, the main objective of a base class is to provide the traits to a derived classes and to create a base pointer used for achieving a runtime polymorphism.

Important note on the pure virtual function that you always should override the pure virtual function of the base class in the derived class, if you forget to override the derived class will become an abstract class.

Now, we will see one example of a pure virtual function.

See the following program.

#include<iostream>
using namespace std;
//Declaring Base Class
class student
{
  public:
    //creating pure virtual function
    virtual void details()=0;
};
//declaring Derived class
class Info:public student
{
    public:
        void details()
        {
            cout<<"\nName: Debasis\nRoll: 14";
        }
};
int main()
{
    student *s;
    Info i;
    s=&i;
    s->details();
    return 0;
}

See the following output.

 

Concept of Pure Virtual Function

So in the example, we can see that in the base class student we have declared one pure virtual function details() and then in the derived class we have used it. So, here we end the concept of virtual function.

Finally, Virtual Function in C++ Example | C++ Virtual Function Tutorial is over.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.