The Wayback Machine - https://web.archive.org/web/20261201203224/https://www.geeksforgeeks.org/cpp/abstraction-in-cpp/

Abstraction in C++

Last Updated : 26 Nov, 2026

Abstraction is an Object-Oriented Programming (OOP) concept that hides implementation details and exposes only essential functionality. It focuses on what an object does rather than how it performs an operation.

  • Reduces dependencies between the user and internal implementation.
  • Allows implementation changes without affecting the public interface.

Real-World Example: An ATM or a coffee machine demonstrates abstraction, where users perform operations such as withdrawing cash or selecting a beverage without knowing the internal implementation.

abstraction

Ways to Achieve Abstraction

C++ supports abstraction through two main mechanisms:

1. Using Access Specifiers

Access specifiers control the accessibility of class members. They help hide internal data and operations while exposing necessary functionality.

  • public allows members to be accessed from outside the class.
  • private restricts access to class members and friends.
  • protected allows access to class members, derived classes and friends, subject to access rules.

Example: Demonstrating abstraction using access specifiers.

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

class Car{
private:
    void startEngine(){
        cout << "Engine started";
    }

public:
    void start(){
        startEngine();
    }
};

int main(){
    Car c;
    c.start();

    return 0;
}

Output
Engine started

Explanation: The private startEngine() function hides the internal operation, while the public start() function provides access to the required functionality.

2. Using Abstract Classes

An abstract class contains or inherits at least one pure virtual function that remains unimplemented in the class's final overrider and cannot be instantiated directly. It defines a common interface for derived classes.

  • A pure virtual function is declared using the virtual keyword followed by = 0.
  • An abstract class can contain constructors, data members and regular member functions.

Syntax of Pure Virtual Function:

virtual return_type function_name() = 0;

A derived class must have non-pure final overriders for all inherited pure virtual functions to become a concrete class.

Example: Demonstrating abstraction using an abstract class.

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

class Vehicle{
public:

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

    // Regular member function
    void stop(){
        cout << "Vehicle stopped" << endl;
    }
};

class Car : public Vehicle{
public:
    void start() override{
        cout << "Car started" << endl;
    }
};

int main(){
    Car c;
    Vehicle& v = c;

    v.start();
    v.stop();

    return 0;
}

Output
Car started
Vehicle stopped

Explanation: The abstract class Vehicle declares the pure virtual function start() and defines the regular function stop(). The Car class implements start(), allowing both operations to be accessed through a Vehicle reference.

Advantages of Abstraction

Abstraction provides several benefits while designing object-oriented applications.

  • Allows implementations to change without requiring changes to code that depends only on the interface.
  • Supports multiple implementations of the same interface.
  • Makes components easier to test by allowing alternative implementations to be substituted.
Comment