Abstraction in C++
1. Introduction
Abstraction is one of the important concepts of Object-Oriented Programming (OOP) in C++.
Abstraction means hiding unnecessary implementation details and showing only the essential information to the user.
Simple Example
When you use an ATM:
- You enter your PIN.
- You select an amount.
- You receive money.
You do not need to know the internal details of how the ATM communicates with the bank server.
User ↓ ATM Interface ↓ Bank System ↓ Database
The user sees only the necessary operations. The complex implementation is hidden.
2. Definition
Abstraction is the process of hiding implementation details and providing only the essential features of an object or system.
In simple words:
Abstraction = What to do, not how to do it.
3. Real-Life Examples of Abstraction
ATM
You use:
- Withdraw
- Deposit
- Check Balance
- Change PIN
But the internal banking process is hidden.
Car
You use:
- Steering wheel
- Brake
- Accelerator
- Gear
You do not need to understand the complete internal working of the engine.
Mobile Phone
You use:
- Call
- Camera
- Internet
- Messages
The internal hardware and software processes are hidden.
4. Abstraction in C++
C++ provides abstraction mainly through:
- Classes
- Access Specifiers
- Abstract Classes
- Pure Virtual Functions
5. Abstraction Using Classes
A class can hide implementation details and provide public functions for users.
Example
#include <iostream> using namespace std; class ATM { private: int balance = 50000; public: void checkBalance() { cout << "Balance: " << balance << endl; } }; int main() { ATM atm; atm.checkBalance(); return 0; }
Output
Balance: 50000
Here:
balanceis private.- The user cannot directly access
balance. checkBalance()provides the required functionality.
This is an example of abstraction.
6. Abstraction Using Access Specifiers
Access specifiers help control what users can access.
C++ provides:
publicprivateprotected
Example
class Student { private: int marks; public: void setMarks(int m) { marks = m; } int getMarks() { return marks; } };
Here, the internal variable marks is hidden from direct access.
The user interacts with:
setMarks() getMarks()
7. Abstract Class
An abstract class is a class that contains at least one pure virtual function.
An abstract class cannot normally be used to create objects directly.
Syntax
class ClassName { public: virtual void functionName() = 0; };
The = 0 makes the function a pure virtual function.
8. Pure Virtual Function
A pure virtual function is a virtual function that has no implementation in the base class.
Syntax
virtual void display() = 0;
It tells derived classes that they must provide their own implementation.
9. 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 d; d.sound(); return 0; }
Output
Dog barks
Here:
virtual void sound() = 0;
is a pure virtual function.
Therefore, Animal is an abstract class.
10. Why Can't We Create an Object of an Abstract Class?
Consider:
class Animal { public: virtual void sound() = 0; };
This is an abstract class.
The following is invalid:
Animal a;
Because Animal has a pure virtual function.
Instead, we create an object of a derived class:
Dog d;
11. Abstraction with Base and Derived Classes
A common way to implement abstraction is to create a general base class and provide specific implementations in derived classes.
Example
#include <iostream> using namespace std; class Shape { public: virtual void area() = 0; }; class Rectangle : public Shape { private: int length; int width; public: Rectangle(int l, int w) { length = l; width = w; } void area() override { cout << "Area = " << length * width << endl; } }; int main() { Rectangle r(10, 5); r.area(); return 0; }
Output
Area = 50
The user calls:
r.area();
The implementation of how the area is calculated is contained inside the class.
12. Abstraction Using Interface-Like Design
C++ does not have a separate interface keyword like Java.
However, a class containing only pure virtual functions can be used similarly to an interface.
Example
#include <iostream> using namespace std; class Payment { public: virtual void pay() = 0; }; class Esewa : public Payment { public: void pay() override { cout << "Payment using Esewa"; } }; class Bank : public Payment { public: void pay() override { cout << "Payment using Bank"; } }; int main() { Esewa e; Bank b; e.pay(); cout << endl; b.pay(); return 0; }
Output
Payment using Esewa Payment using Bank
The user only needs to know that pay() performs payment. The specific implementation is provided by each derived class.
13. Abstraction with User Input
#include <iostream> using namespace std; class Calculator { public: void add(int a, int b) { cout << "Sum = " << a + b << endl; } }; int main() { Calculator c; int x, y; cout << "Enter two numbers: "; cin >> x >> y; c.add(x, y); return 0; }
Example Input
Enter two numbers: 10 20
Output
Sum = 30
The user calls add() without needing to know its internal implementation.
14. Abstraction vs Encapsulation
Abstraction and encapsulation are related but different concepts.
| Abstraction | Encapsulation |
|---|---|
| Hides implementation details. | Bundles data and methods together and controls access. |
| Focuses on what an object does. | Focuses on how data is protected and accessed. |
| Usually implemented using abstract classes and pure virtual functions. | Commonly implemented using classes and access specifiers. |
| Reduces complexity for the user. | Protects data from unauthorized direct access. |
Example: pay() without exposing payment implementation. | Example: private balance accessed through methods. |
Simple Difference
Abstraction "What should the user see?" ↓ Essential functionality Encapsulation "How should data be protected?" ↓ Data + methods + access control
15. Abstraction vs Inheritance
| Abstraction | Inheritance |
|---|---|
| Hides implementation details. | Allows a class to acquire properties and behavior from another class. |
| Focuses on essential functionality. | Focuses on code reuse and class relationships. |
| Uses abstract classes and pure virtual functions. | Uses base and derived classes. |
Example: Shape defines area(). | Example: Rectangle inherits from Shape. |
16. Abstraction and Polymorphism
Abstraction and polymorphism are often used together.
Example
#include <iostream> using namespace std; class Animal { public: virtual void sound() = 0; }; class Dog : public Animal { public: void sound() override { cout << "Dog barks"; } }; class Cat : public Animal { public: void sound() override { cout << "Cat meows"; } }; int main() { Animal* a1 = new Dog(); Animal* a2 = new Cat(); a1->sound(); cout << endl; a2->sound(); delete a1; delete a2; return 0; }
Output
Dog barks Cat meows
The base class provides the abstract operation sound(), while each derived class provides its own implementation.
17. Advantages of Abstraction
-
Reduces complexity Users do not need to understand unnecessary implementation details.
-
Improves security Internal implementation and data can be hidden.
-
Improves maintainability Internal implementation can be changed without changing how users interact with the class.
-
Provides a clear interface Users see only the important operations.
-
Supports code organization Complex systems can be divided into manageable components.
-
Supports polymorphism Abstract classes can define common operations for derived classes.
-
Improves reusability Abstract designs can be reused by different derived classes.
18. Disadvantages of Abstraction
- Abstraction can make programs more complex to design initially.
- Too much abstraction can make code difficult for beginners to understand.
- Abstract classes cannot normally be instantiated directly.
- Designing a good abstraction requires careful planning.
- Additional classes and virtual functions can increase program complexity.
19. Real-Life Example: Online Payment
Consider an online payment system.
The user sees:
Choose Payment Method ↓ Enter Amount ↓ Pay
The user does not need to know:
- How the payment server communicates.
- How authentication is performed.
- How the transaction is stored.
- How the bank processes the transaction.
In C++, this can be represented using abstraction:
class Payment { public: virtual void pay(double amount) = 0; };
Different payment methods can implement pay() differently.
20. Complete Example
#include <iostream> using namespace std; class Vehicle { public: virtual void start() = 0; virtual ~Vehicle() {} }; class Car : public Vehicle { public: void start() override { cout << "Car starts using a key or button." << endl; } }; class Bike : public Vehicle { public: void start() override { cout << "Bike starts using a key or button." << endl; } }; int main() { Car car; Bike bike; Vehicle* v1 = &car; Vehicle* v2 = &bike; v1->start(); v2->start(); return 0; }
Output
Car starts using a key or button. Bike starts using a key or button.
Explanation
Vehicleis an abstract class.start()is a pure virtual function.CarandBikeprovide their own implementations.- The user interacts with the common
Vehicleinterface. - The internal implementation is hidden inside the derived classes.
21. Important Terms
| Term | Meaning |
|---|---|
| Abstraction | Hiding implementation details and showing essential features |
| Abstract Class | Class containing at least one pure virtual function |
| Pure Virtual Function | Virtual function declared with = 0 |
| Concrete Class | A class that can be instantiated |
| Interface | A public contract describing operations a class provides |
| Encapsulation | Bundling data and methods and controlling access |
22. Key Points to Remember
- Abstraction means hiding unnecessary implementation details.
- It shows only the essential features to the user.
- C++ supports abstraction through classes, access control, abstract classes, and pure virtual functions.
- A pure virtual function is declared using:
virtual void display() = 0;
- A class with at least one pure virtual function is an abstract class.
- An abstract class cannot normally be instantiated directly.
- Derived classes can provide implementations of pure virtual functions.
- Abstraction helps reduce complexity and supports flexible program design.
23. Quick Example to Remember
#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 d; d.sound(); return 0; }
Output
Dog barkss
Remember
Abstraction ↓ Hide implementation ↓ Show essential functionality ↓ Abstract Class ↓ Pure Virtual Function ↓ Derived Class implements it