C++ Abstraction

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.

Mini Quiz

0%

C++ PROGRAMMING - QUIZ 19

1 / 5

Q1. What does abstraction mean in C++?

2 / 5

Q2. In the Car example, which function can the user call to start the car?

3 / 5

Q3. What is special about an abstract class in C++?

4 / 5

Q4. How is a pure virtual function written in C++?

5 / 5

Q5. Can you create an object directly from an abstract class such as Animal?

Your score is

The average score is 80%

0%

Mini Project

Mini Project: Shape Area Calculator

Question:

Write a C++ program that uses abstraction to calculate the area of different shapes.

  • Create an abstract class named Shape.
  • Add a pure virtual function named area().
  • Create Rectangle and Circle classes that inherit from Shape.
  • Ask the user to enter the required values.
  • Implement the area() function differently in each class.
  • Display the area of the selected shape.
View Answer Code
#include <iostream>
using namespace std;

class Shape {
public:
    virtual void area() = 0;
};

class Rectangle : public Shape {
public:
    void area() override {
        double length, width;

        cout << "Enter length: ";
        cin >> length;

        cout << "Enter width: ";
        cin >> width;

        cout << "Rectangle Area: " << length * width;
    }
};

class Circle : public Shape {
public:
    void area() override {
        double radius;

        cout << "Enter radius: ";
        cin >> radius;

        cout << "Circle Area: " << 3.14 * radius * radius;
    }
};

int main() {

    int choice;

    cout << "1. Rectangle\n";
    cout << "2. Circle\n";
    cout << "Enter your choice: ";
    cin >> choice;

    if (choice == 1) {
        Rectangle rectangle;
        rectangle.area();
    }
    else if (choice == 2) {
        Circle circle;
        circle.area();
    }
    else {
        cout << "Invalid choice.";
    }

    return 0;
}

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