Abstraction in C++

Last Updated : 1 Feb, 2026

Abstraction is the concept of exposing only the essential features of an object while hiding the internal implementation details.

  • It improves code maintainability as internal changes can be made without affecting other parts of the code that use the abstraction.
  • Abstract classes may have methods without implementation and must be implemented by subclasses.
  • By abstracting functionality, changes in the implementation do not affect the code that depends on the abstraction.

Abstract Classes

Abstraction is mainly achieved using abstract classes and pure virtual functions. These define a common interface for derived classes while leaving the actual implementation to them.

  • An abstract class contains at least one pure virtual function.
  • Objects of an abstract class cannot be instantiated.
  • Derived classes must implement the pure virtual functions.
  • It provides a common interface to achieve abstraction.
C++
#include <bits/stdc++.h>
using namespace std;

// Abstract base class as there is a
// pure virtual method
class Shape {
protected:
    string color;

public:
    Shape(string color) {
        this->color = color;
    }

    // Abstract or Pure virtual method
    virtual double area() = 0;

    // Concrete method
    string getColor() {
        return color;
    }

    virtual ~Shape() {}
};

// Derived class: Rectangle
class Rectangle : public Shape {
    double length;
    double width;

public:
    Rectangle(string color, double length, double width)
        : Shape(color) {
        this->length = length;
        this->width = width;
    }

    double area() override {
        return length * width;
    }
};

int main() {
    Shape* s = new Rectangle("Yellow", 2, 4);

    cout << "Rectangle color is "
         << s->getColor()
         << " and area is : "
         << s->area() << endl;

    return 0;
}

Output
Drawing a Circle

Explanation:

  • The class "Shape" is an abstract class because it contains a pure virtual function "draw()", which defines what operation every shape must implement.
  • The function "draw()" is declared as virtual and equal to 0, meaning its implementation is hidden and must be provided by derived classes like "Circle".
  • In main(), the program uses a Shape reference to call "draw()", and at runtime the Circle version executes, demonstrating abstraction.

Full Abstraction - Interfaces

In C++, a class can act as an interface if it contains only pure virtual functions and no data members or implemented methods. This enforces that all derived classes must implement every function, achieving full control abstraction. Interfaces are useful when multiple classes need to share a common interface without sharing implementation.

C++
#include <iostream>
using namespace std;

// Pure Abstract Class acting as an Interface
class Printable {
public:
    virtual void print() = 0;  // pure virtual function
    virtual void scan() = 0;   // pure virtual function

    // Virtual destructor is a good practice for base classes
    virtual ~Printable() {}
};

// Derived class must implement all functions
class Document : public Printable {
public:
    void print() override {
        cout << "Printing document..." << endl;
    }
    void scan() override {
        cout << "Scanning document..." << endl;
    }
};

// Another derived class implementing the same interface
class Photo : public Printable {
public:
    void print() override {
        cout << "Printing photo..." << endl;
    }
    void scan() override {
        cout << "Scanning photo..." << endl;
    }
};

int main() {
    // Base class pointer pointing to derived objects
    Printable* p1 = new Document();
    Printable* p2 = new Photo();

    // Call interface functions - runtime polymorphism
    p1->print();
    p1->scan();

    p2->print();
    p2->scan();

    // Free memory
    delete p1;
    delete p2;

    return 0;
}

Output
Printing document...
Scanning document...
Printing photo...
Scanning photo...

Explanation:

  • Printable is a pure abstract class, acts like an interface.
  • All derived classes (Document, Photo) must implement all pure virtual functions.
  • Base class pointers (Printable*) can point to any derived object, allowing runtime polymorphism.
  • Full abstraction or interface achieved: users interact with Printable without knowing the internal implementation in Document or Photo.
Comment