A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/pure-virtual-functions-and-abstract-classes/ below:

Pure Virtual Functions and Abstract Classes in C++

Pure Virtual Functions and Abstract Classes in C++

Last Updated : 07 Aug, 2025

What is an Abstract Class in C++?

An abstract class is a class that cannot be used to create objects directly. It is meant to be a base class that provides a common interface but doesn't define full behavior.

A class becomes abstract if it has at least one pure virtual function.

What is a Pure Virtual Function?

A pure virtual function is a function declared in a base class that must be overridden in any derived class.

It is written like this:

C++
virtual void draw() = 0;  // Pure virtual function
Example of Abstract Class and Pure Virtual Function C++
#include <iostream>
using namespace std;

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

class Circle : public Shape {
public:
    void draw() override {
        cout << "Drawing Circle\n";
    }
};

int main() {
    // Shape s;       Error: Cannot create object of abstract class
    Shape* s = new Circle();  // Pointer to abstract class
    s->draw();                // Output: Drawing Circle
    delete s;
}
Abstract Class Vs Interface

Feature

Abstract Class (C++)

Interface (Java/C++)

Methods

Can have some implementation

All methods are pure virtual

Inheritance

Single or multiple inheritance

Simulates multiple inheritance in C++

Use

Common base with partial code

Contract with no implementation

In C++

Abstract class with pure virtuals

All pure virtual functions= Interface

In Java

Use abstract or interface keyword

Separate interface keyword available.

Summary

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4