DEV Community

Cover image for Virtual Functions, Abstract Classes, and Interfaces in C++
Jyoti Jingar
Jyoti Jingar

Posted on

Virtual Functions, Abstract Classes, and Interfaces in C++

1. Virtual Function

Definition: A virtual function is a member function in a base class that can be overridden in a derived class. It allows runtime polymorphism, meaning the function that gets called is determined at runtime based on the object type.
Syntax:

class Base {
public:
    virtual void show() {
        cout << "Base class function" << endl;
    }
};
Enter fullscreen mode Exit fullscreen mode

Example:

#include <iostream>
using namespace std;

class Base {
public:
    virtual void display() {
        cout << "Display from Base class" << endl;
    }
};

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

int main() {
    Base* ptr;
    Derived d;
    ptr = &d;
    ptr->display(); // Output: Display from Derived class
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Explanation: Since display() is declared virtual, the compiler calls the Derived class version at runtime.

2. Abstract Class (Pure Virtual Function)

Definition:

  • An abstract class is a class that cannot be instantiated directly.
  • It contains at least one pure virtual function, which is a function declared with = 0.
  • Derived classes must override pure virtual functions.

Syntax:

class Base {
public:
    virtual void show() = 0; // Pure virtual function
};

Enter fullscreen mode Exit fullscreen mode

Example:

#include <iostream>
using namespace std;

class Shape {
public:
    virtual void area() = 0; // Pure virtual function
};

class Circle : public Shape {
public:
    void area() override {
        cout << "Area = πr²" << endl;
    }
};

int main() {
    // Shape s; // ❌ Error: cannot create object of abstract class
    Circle c;
    c.area(); // Output: Area = πr²
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Shape is an abstract class because it has a pure virtual function.
  • Circle provides an implementation, so it can be instantiated.

3. Interface

Definition:C++ doesn’t have a keyword interface like Java,
but an interface can be implemented using an abstract class where all functions are pure virtual.

Syntax:

class Interface {
public:
    virtual void func1() = 0;
    virtual void func2() = 0;
};

Enter fullscreen mode Exit fullscreen mode

Example:

#include <iostream>
using namespace std;

class Drawable {
public:
    virtual void draw() = 0;
    virtual void resize() = 0;
};

class Rectangle : public Drawable {
public:
    void draw() override {
        cout << "Drawing Rectangle" << endl;
    }
    void resize() override {
        cout << "Resizing Rectangle" << endl;
    }
};

int main() {
    Rectangle r;
    r.draw();
    r.resize();
    return 0;
}

Enter fullscreen mode Exit fullscreen mode

Explanation:

  • Here, Drawable acts as an interface — all functions are pure virtual.
  • Any class implementing this must define all the functions.

1. Why use Virtual Functions

Purpose:
To achieve runtime polymorphism — that is, making the program decide at runtime which function to call depending on the object type.

Without virtual function:

Base* b = new Derived();
b->show();   // Calls Base version ❌

Enter fullscreen mode Exit fullscreen mode

With virtual function:

Base* b = new Derived();
b->show();   // Calls Derived version ✅

Enter fullscreen mode Exit fullscreen mode

Use case: When you want to write generic code that works with base class pointers or references, but executes derived class behavior dynamically.

2. Why use Abstract Classes (Pure Virtual Functions)

Purpose:
To define a common blueprint for derived classes while forcing them to implement certain behavior.

Key idea:

  • You can’t create objects of an abstract class.
  • You use it to ensure every derived class implements specific functions.

3. Why use Interface

Purpose:
To define a set of rules or contracts that multiple unrelated classes can follow.

In C++, an interface is just an abstract class with only pure virtual functions.

Use case:
Suppose you have different systems —

  • like a Printer, Scanner, and Camera —
  • and all should be “connectable” via a Connectable interface.
class Connectable {
public:
    virtual void connect() = 0;
    virtual void disconnect() = 0;
};

Enter fullscreen mode Exit fullscreen mode

Top comments (0)