Imagine using a TV remote.

You press the power button and the TV turns on. You don’t need to know what is happening inside the TV.

You only use the buttons you need.

This is the basic idea of abstraction in C++.

Abstraction means hiding unnecessary details and showing only the important parts.

Why Do We Need Abstraction in C++?

Think about driving a car.

You use:

  • Steering wheel
  • Brake
  • Accelerator
  • Gear

You don’t need to understand every part of the engine to drive the car.

The complicated details are hidden from you.

C++ abstraction works in a similar way.

User
  ↓
Simple Interface
  ↓
Hidden Details

Simple Example

Let’s create a class for a car.

#include <iostream>
using namespace std;

class Car {
public:
    void start() {
        cout << "Car is starting...";
    }
};

int main() {
    Car car;

    car.start();

    return 0;
}

Output:

Car is starting...

When we write:

car.start();

we don’t need to know what happens inside the start() function.

We simply tell the car to start.

The complicated work can stay inside the class.

Abstraction Using private

We can hide internal details using private.

#include <iostream>
using namespace std;

class Car {
private:
    void checkEngine() {
        cout << "Checking engine...\n";
    }

public:
    void start() {
        checkEngine();
        cout << "Car is starting";
    }
};

int main() {
    Car car;

    car.start();

    return 0;
}

Output:

Checking engine...
Car is starting

Here, checkEngine() is private.

The user cannot directly call:

car.checkEngine();

Instead, they simply call:

car.start();

The start() function handles the internal work.

Abstract Classes

C++ also provides a special way to create abstraction using abstract classes.

An abstract class contains at least one pure virtual function.

A pure virtual function is written using = 0.

class Animal {
public:
    virtual void sound() = 0;
};

Here, sound() is a pure virtual function.

This means that the Animal class tells its derived classes:

“You must provide your own version of sound().”

Simple Abstract Class Example

#include <iostream>
using namespace std;

class Animal {
public:
    virtual void sound() = 0;
};

class Dog : public Animal {
public:
    void sound() override {
        cout << "Dog barks";
    }
};

int main() {
    Dog dog;

    dog.sound();

    return 0;
}

Output:

Dog barks

Here, Animal is an abstract class.

It only says that every animal should have a sound() function.

The Dog class decides what that function should actually do.

Can We Create an Object of an Abstract Class in C++?

No.

For example, this is not allowed:

Animal animal;

An abstract class is meant to be used as a base class.

You create objects of the derived classes instead:

Dog dog;

C++ Abstraction vs Encapsulation

These two concepts can look similar at first.

A simple way to remember them is:

EncapsulationAbstraction
Protects dataHides unnecessary details
Controls accessShows only important features
Uses private, public, etc.Often uses abstract classes and pure virtual functions
Focuses on how data is protectedFocuses on what the user needs to see

For example, in a car:

Encapsulation: Keeps the internal parts protected.

Abstraction: Gives you simple controls like start(), stop(), and brake() without showing how everything works internally.

A Simple Real-Life Example

Think about using an ATM.

You can:

Withdraw Money
Check Balance
Deposit Money

You don’t need to know how the bank’s servers check your account or process the transaction.

The ATM gives you simple options and hides the complicated work.

That’s abstraction.

Key Points to Remember

  • Abstraction means hiding unnecessary details.
  • It shows only the important features to the user.
  • private functions can hide internal work.
  • An abstract class contains at least one pure virtual function.
  • A pure virtual function uses = 0.
  • You cannot create an object of an abstract class.
  • Derived classes provide the actual implementation.

What’s Next?

You have now covered the main ideas of Object-Oriented Programming in C++. Next, we will move into C++ Templates.


Frequently Asked Questions (FAQs)

Q1. What is abstraction in C++?

Abstraction in C++ hides unnecessary implementation details and shows only the important features to the user.

Q2. What is an abstract class in C++?

An abstract class contains at least one pure virtual function and cannot be used to create objects directly.

Q3. What is the difference between abstraction and encapsulation in C++?

Abstraction focuses on hiding unnecessary implementation details, while encapsulation focuses on protecting data and controlling access to it.

Q4. Why can’t an abstract class be instantiated in C++?

An abstract class contains at least one pure virtual function without a complete implementation, so C++ does not allow objects of that class to be created directly.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top