Virtual Function vs Pure Virtual Function in C++

1. Introduction

In C++ programming, virtual functions and pure virtual functions are key concepts in polymorphism and abstraction. They both play crucial roles in defining interfaces and implementing inheritance. A virtual function is a function in a base class that can be overridden in derived classes. A pure virtual function is a virtual function with no implementation, requiring derived classes to provide an implementation.

2. Key Points

1. Virtual functions provide a way to achieve runtime polymorphism.

2. Pure virtual functions make a class abstract, preventing it from being instantiated on its own.

3. A virtual function can have an implementation in a base class.

4. Pure virtual functions have no implementation in the base class; they must be overridden in derived classes.

3. Differences

Virtual Function Pure Virtual Function
Can have an implementation in a base class. Has no implementation in a base class.
Derived class can override it, but it’s not mandatory. Must be overridden in derived classes.
Does not make a class abstract. Makes a class abstract.

4. Example

#include <iostream>
using namespace std;

// Base class with virtual function
class Base {
public:
    virtual void show() { cout << "Base show" << endl; }
};

// Derived class overriding virtual function
class Derived : public Base {
public:
    void show() override { cout << "Derived show" << endl; }
};

// Base class with pure virtual function
class AbstractBase {
public:
    virtual void display() = 0;
};

// Derived class implementing pure virtual function
class Concrete : public AbstractBase {
public:
    void display() override { cout << "Concrete display" << endl; }
};

int main() {
    Derived d;
    d.show();

    Concrete c;
    c.display();

    return 0;
}

Output:

Derived show
Concrete display

Explanation:

1. In the Derived class, the show function overrides the virtual function from Base.

2. In the Concrete class, the display function provides an implementation for the pure virtual function from AbstractBase.

5. When to use?

- Use virtual functions when you want to provide a common interface and default behavior that derived classes can extend or override.

- Use pure virtual functions to define a strict interface in the base class, forcing derived classes to provide their own implementation of these functions.

Related C++/CPP Posts:

Difference Between Struct and Class in C++

Difference Between Pointer and Reference in C++

Difference Between null and nullptr in C++

Difference Between Array and Vector in C++

Difference Between const and constexpr in C++

Difference Between List and Vector in C++

Difference Between C and C++

Difference Between Function Overloading and Operator Overloading in C++

Difference Between Array and List in C++

Difference Between a While Loop and a Do-While Loop in C++

Difference Between new and malloc C++

Virtual Function vs Pure Virtual Function in C++

Compile Time Polymorphism vs Runtime Polymorphism in C++

Difference Between Shallow Copy and Deep Copy in C++

Difference Between Stack and Heap in C++

Copy Constructor vs Parameterized Constructor in C++

Comments