74
0
C++

Virtual Functions in C++

74
0
Virtual Functions in C++

The virtual functions in c++ also play an important role in object-oriented programming. Ordinary function uses the concept of early binding. However, a virtual function uses the concept of late binding Mainly pointer plays a vital role in a virtual function. A virtual function does not really exist but appears real in some parts of a program. When a virtual function is used in a C++ program, then that program that calls a function of one class, actually in real matters can call a function of a different class. A virtual function is a function that is declared as virtual in a base class. Here we use the pointer to the base class to refer to all the derived objects.

With the help of a virtual function, the compiler selects the suitable member function of the derived class at run time depending upon the contents of the base class pointer and type of pointer. Virtual functions are so called because it seems that the programmer calls a function defined in the base class, but actually a function of the derived class is called.

Definition:

When we use the same function name in both the base and derived classes, the function in the base class is declared as virtual using the keyword “virtual” preceding its normal declaration. In other words, the use of the same function in both the base and derived classes can be done in C++ Programming, but for this purpose, a function in the base class must be declared as virtual. Such a type of function is called a virtual function.

So after the declaration of the virtual function, it is the C++’s job to find which function to use at run time based on the type of the object pointed to by the base pointer, rather than the type of the pointer. Thus, by making the base pointer point to different objects, we can execute different versions of the virtual function.

Virtual function explains the concept of Polymorphism in depth. C++ supports a mechanism of this function to achieve run-time polymorphism or dynamic binding. In other words, run time polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.

Definition:

Virtual functions are public member functions, used for special purposes. These are defined in the base class and again are redefined in the derived class i.e. virtual functions are defined both in the base and derived class with the same name.

Virtual function employs the concept of late binding by allocating memory space during execution time not during compilation time.

The general syntax used for the virtual function declaration in the base class is as:

class class-name
{
private:
    data member;
    member function;

public:
    data member;
    virtual return-type function name(argument list);
};

Here return-type is also called data type like int, float, void, etc. For example, the following procedure also explains the declaration of virtual function:

Limitations of Virtual functions:

The following are the major cons of the virtual function:

  • Slower: The function call takes slightly longer due to the virtual mechanism and makes it more difficult for the compiler to optimize because it doesn’t know exactly which function is going to be called at compile time.
  • Difficult to debug: In a complex system, virtual functions can make it a little more difficult to figure out where a function is being called from.

Program to use the virtual function.

#include <iostream>
#include <math.h>
using namespace std;

class abc
{
public:
    int a, b;
    void read()
    {
        cout << "Enter the integer values: ";
        cin >> a >> b;
    }
    virtual void display()
    {
        cout << "This is base class abc: " << endl;
    }
};

class xyz : public abc
{
private:
    int c;

public:
    void display()
    {
        abc::read();
        c = a + b;
        cout << "The sum is = " << c << endl;
    }
};

class lmn : public abc
{
private:
    int d;

public:
    void display()
    {
        abc::read();
        d = a * b;
        cout << "The  multiplication is = " << d;
    }
};

int main()
{
    class abc *p;
    class xyz m;
    cout << "base class xyz: " << endl;
    p = &m;
    p->display();
    cout << "\nbase class lmn: " << endl;
    class lmn r;
    p = &r;
    p->display();
    return 0;
}

Output:

base class xyz: 
Enter the integer values: 3
2
The sum is = 5

base class lmn:
Enter the integer values: 3
2
The  multiplication is = 6

Program to illustrate the concept of Virtual Functions.

// program to explain the declaration and processing of virtual function
#include <iostream>
using namespace std;

class base
{
public:
    void display()
    {
        cout << "\nDisplay base";
    }
    virtual void show()
    {
        cout << "\nShow base";
    }
};
class derived : public base
{
public:
    void display()
    {
        cout << "\nDisplay derived";
    }
    void show()
    {
        cout << "\nShow derived";
    }
};

int main()
{
    base b;
    derived d;
    base *ptr;
    cout << "\nptr points to base\n";
    ptr = &b;
    ptr->display(); // calls base version
    ptr->show();    // calls base version
    cout << "\n\nptr points to derived\n";
    ptr = &d;
    ptr->display(); // calls base version
    ptr->show();    // calls derived version
}

Output:

ptr points to base

Display base
Show base

ptr points to derived

Display base
Show derived

To call a virtual function in the main () function, the following mechanism should be noted:

  1. A pointer in the base class should be defined. e.g. class abc *p;
  2. Object for different classes linked with derived classes must be declared. e.g. class xyz m, class Imn r.
  3. The address of object should be placed in the base class pointer.
    e.g. p=&m, p= &r.
  4. The pointer is used to call the virtual function.
    e.g. p → display (), p → add ().

Always remember:

  1. It is a syntax error to assign the address of a base class object to a derived class pointer (without explicit casting).
  2. A virtual function remains virtual down the inheritance hierarchy from where it has been declared.
  3. Never try to reference only derived class members through the pointer of the base class when it points to the derived class object.
xalgord
WRITTEN BY

xalgord

Highly motivated, Hard-working, and Resourceful Programmer. I Love To Explain And Teach Technology, Solve Tech Problems And Learn Something New Every Day.

Leave a Reply