0
0
C++programming~5 mins

Why abstraction is required in C++

Choose your learning style9 modes available
Introduction

Abstraction helps us hide complex details and show only what is necessary. It makes programs easier to understand and use.

When you want to hide complex code from users and show only simple interfaces.
When you want to change internal details without affecting other parts of the program.
When you want to focus on what an object does, not how it does it.
When you want to reduce code duplication by using common interfaces.
When you want to improve code maintenance and readability.
Syntax
C++
class AbstractExample {
public:
    virtual void show() = 0; // pure virtual function
    virtual ~AbstractExample() = default;
};

class ConcreteExample : public AbstractExample {
public:
    void show() override {
        // implementation details hidden from user
        std::cout << "Showing details" << std::endl;
    }
};

Abstraction is often done using abstract classes or interfaces in C++.

Pure virtual functions (marked with = 0) force derived classes to implement them.

Examples
This example shows an abstract class Shape with a pure virtual function draw(). Circle implements draw().
C++
class Shape {
public:
    virtual void draw() = 0; // abstract method
    virtual ~Shape() = default;
};

class Circle : public Shape {
public:
    void draw() override {
        std::cout << "Drawing Circle" << std::endl;
    }
};
Vehicle is abstract and hides start details. Car provides the actual start behavior.
C++
class Vehicle {
public:
    virtual void start() = 0;
    virtual ~Vehicle() = default;
};

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

This program shows abstraction by using an abstract class AbstractDevice. Fan and Light hide their details but provide turnOn behavior. The main function uses the abstract interface to turn devices on without knowing their inner workings.

C++
#include <iostream>

class AbstractDevice {
public:
    virtual void turnOn() = 0; // pure virtual function
    virtual ~AbstractDevice() = default;
};

class Fan : public AbstractDevice {
public:
    void turnOn() override {
        std::cout << "Fan is now ON" << std::endl;
    }
};

class Light : public AbstractDevice {
public:
    void turnOn() override {
        std::cout << "Light is now ON" << std::endl;
    }
};

int main() {
    AbstractDevice* device1 = new Fan();
    AbstractDevice* device2 = new Light();

    device1->turnOn();
    device2->turnOn();

    delete device1;
    delete device2;

    return 0;
}
OutputSuccess
Important Notes

Time complexity depends on the implementation of the concrete classes, abstraction itself adds no overhead.

Space complexity is minimal; abstraction uses pointers or references to base classes.

Common mistake: Trying to create objects of abstract classes directly causes errors.

Use abstraction when you want to separate interface from implementation and allow flexible code changes.

Summary

Abstraction hides complex details and shows only what is needed.

It helps make code easier to use, maintain, and extend.

In C++, abstraction is done using abstract classes and pure virtual functions.