Skip to content

Abstraction

Abstraction means hiding the complex implementation details and showing only the essential features to the user.

Think of it like driving a car — you just use the steering wheel, accelerator, and brakes. You don't need to know how the engine works internally.

Two Ways to Achieve Abstraction in C++

  • Abstract Classes — partial abstraction
  • Interfaces — full abstraction

Example (Abstract Class in C++)

cpp
class Shape {
public:
    virtual double area() = 0; // pure virtual — no implementation, just the contract
    void display() {
        cout << "Area: " << area() << endl;
    }
};

class Circle : public Shape {
    double radius;
public:
    Circle(double r) : radius(r) {}
    double area() override {
        return 3.14 * radius * radius; // actual implementation here
    }
};

The user just calls circle.area() — they don't care how it's calculated.

Abstraction vs Encapsulation

AbstractionEncapsulation
FocusHiding complexityHiding data
What it hidesImplementation detailsInternal state
HowAbstract classes, InterfacesPrivate fields, getters/setters
GoalSimplify usageProtect data

Simple way to remember: Abstraction hides "how it works", Encapsulation hides "what the data is".

One-liner: "Abstraction hides the internal complexity and exposes only what is necessary, helping users interact with objects at a higher level."