Virtual Function in C++ (original) (raw)

Last Updated : 11 Jun, 2026

A virtual function is a member function declared with the virtual keyword in a base class and overridden in a derived class. It allows function calls through base class pointers or references to invoke the implementation corresponding to the actual object type at runtime.

#include using namespace std;

class Shape { public:

// Virtual function
virtual void calculate()
{
    cout << "Area of your Shape ";
}

// Virtual destructor
virtual ~Shape()
{
    cout << "Shape Destructor called\n";
}

};

// Derived class: Rectangle class Rectangle : public Shape { public: int width, height, area;

void calculate() override
{
    width = 5;
    height = 10;

    area = height * width;
    cout << "Area of Rectangle: " << area << "\n";
}

~Rectangle()
{
    cout << "Rectangle Destructor called\n";
}

};

// Derived class: Square class Square : public Shape { public: int side, area;

void calculate() override
{
    side = 7;
    area = side * side;
    cout << "Area of Square: " << area << "\n";
}

~Square()
{
    cout << "Square Destructor called\n";
}

};

int main() {

Shape *S;

Rectangle r;
S = &r;
S->calculate();

Square sq;
S = &sq;
S->calculate();

return 0;

}

`

Output

Area of Rectangle: 50 Area of Square: 49 Square Destructor called Shape Destructor called Rectangle Destructor called Shape Destructor called

**Explanation

Note: It is a recommended way to use **override identifier to avoid mistakes while redefine virtual function inside the derived class.

Pure Virtual Function

A pure virtual function is a virtual function declared with = 0 in a base class. It is used to enforce that derived classes provide their own implementation.

#include using namespace std;

class Base { public:

// Pure virtual function
virtual void display() = 0;

// Pure virtual destructor
virtual ~Base() = 0;

};

// Definition of pure virtual destructor Base::~Base() { cout << "Base destructor called" << endl; }

class Derived : public Base { public: void display() override { cout << "Derived class display" << endl; }

~Derived()
{
    cout << "Derived destructor called" << endl;
}

};

int main() { Base *basePtr; Derived derivedObj; basePtr = &derivedObj; basePtr->display(); return 0; }

`

Output

Derived class display

**Explanation

Early Binding and Late Binding

Binding is the process of associating a function call with the function definition that will be executed. In C++, binding can occur at either compile time or runtime.

#include using namespace std;

class base { public: virtual void print() { cout << "print base " "class\n"; }

void show()
{
    cout << "show base class\n";
}

};

class derived : public base { public: void print() { cout << "print derived class\n"; }

void show()
{
    cout << "show derived class\n";
}

};

int main() { base *bptr; derived d; bptr = &d;

// Virtual function,binded at runtime
bptr->print();

// Non-virtual function,binded at compile time
bptr->show();
return 0;

}

`

Output

print derived class show base class

**Explanation

Runtime Resolution Using vtable and vptr

Virtual functions achieve runtime polymorphism through two compiler-generated mechanisms: vtable and vptr.

actual_object

Rules for Virtual Functions

Virtual functions follow these important rules:

Limitations of Virtual Functions

While virtual functions enable runtime polymorphism, they also have some drawbacks: