Title: ENCAPSULATION AND ABSTRACTION IN C++
Names: OLOMOWEWE FATHIA OMOWUNMI – 22/10502
FAYOSE SHADRACH – 22/10105
OSEMWENGIE EFOSA NAOMI – 22/11316
LAWAL OLAMIDE OLUWAFEMI – 22/10137
Course Name: COMPUTER PROGRAMMING II (CSC 202)
Instructors Name:
Date: 10th of June
INTRODUCTION TO ENCAPSULATION AND ABSTRACTION
In the realm of object-oriented programming (OOP), two fundamental principles that play a crucial role
in designing robust and maintainable software are encapsulation and abstraction. These principles help
programmers create modular, flexible, and scalable code by managing the complexity of software
systems.
Encapsulation in C++
Definition
Encapsulation in C++ is referred to the bundling of data and information in a single unit known as an object.
In Object Oriented Programming, this concept helps to hide the internal implementation details of an object
from the outside world, ensuring that the object's state can only be accessed and modified through the
provided methods.
Real-Life Example
Consider a real-life example of encapsulation, in a company, there are different sections like the accounts
section, finance section, sales section, etc.
* The finance section handles all the financial transactions and keeps records of all the data related to
finance.
* Similarly, the sales section handles all the sales-related activities and keeps records of all the sales.
Now there may arise a situation when for some reason an official from the finance section needs all the data
about sales in a particular month. In this case, he is not allowed to directly access the data of the sales
section. He will first have to contact some other officer in the sales section and then request him to give the
particular data.
This is what Encapsulation is. Here the data of the sales section and the employees that can manipulate them
are wrapped under a single name “sales section”.
In C++, encapsulation is achieved through the use of access specifiers, which control the visibility and
accessibility of class members (data and methods). The three main access specifiers in C++ are:
1. Public : Members declared as public are accessible from anywhere in the program, both inside
and outside the class.
2. Private : Members declared as private can only be accessed within the class in which they are
defined. They are not accessible from outside the class.
3. Protected : Members declared as protected can be accessed within the class and by any derived
classes (classes that inherit from the base class).
By using these access specifiers, the class can hide its internal implementation details and expose only
the necessary public interface, which is known as the class's API (Application Programming Interface).
This separation of implementation from the public interface is a key aspect of encapsulation.
➢ Here is an example of encapsulation in C++:
#include <iostream>
using namespace std;
class EncapsulationExample {
private:
int a;
public:
void set(int x) {
a = x;
}
int get() {
return a;
}
};
int main() {
EncapsulationExample e1;
e1.set(10);
cout << e1.get(); // Output: 10
return 0;
}
THE OUTPUT:
In this program, the variable a is private, ensuring that it can only be accessed through the set( ) and get( )
method. This binding of data and methods exemplifies encapsulation.
Abstraction in C++
Definition
Abstraction means displaying only essential information and hiding the unnecessary details. In other words,
abstraction focuses on what an object does rather than how it does it.
Real-Life Example
Consider a real-life example of a man driving a car. The man only knows that pressing the accelerator will
increase the speed of the car or applying brakes will stop the car. However, he does not know how on
pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the
car or the implementation of the accelerator, brakes, etc in the car. This separation of essential information
(accelerator and brakes) from implementation details (engine mechanics) exemplifies data abstraction.
Implementing Data Abstraction in C++
- Using Classes
We can implement Abstraction in C++ using classes. The class helps us to group data members and member
functions using available access specifiers. A Class can decide which data member will be visible to the
outside world and which is not.
➢ Here’s a simple example of abstraction using a class:
#include <iostream>
using namespace std;
class Summation {
private:
int a, b, c;
public:
void sum(int x, int y) {
a = x;
b = y;
c = a + b;
cout << "Sum of the two numbers is: " << c << endl;
}
};
int main() {
Summation s;
s.sum(5, 4);
return 0;
}
THE OUTPUT:
In this example, the class summation encapsulates the private members, a, b, c. These members are only
accessible through the member function sum( ). The implementation details (such as how the sum is
calculated) are hidden from the user, demonstrating abstraction.
- Using Header files
One more type of abstraction in C++ can be header files. For example, consider the pow() method present in
math.h header file.
- Using Access Specifiers
Access specifiers are the main pillar of implementing abstraction in C++. We can use access specifiers to
enforce restrictions on class members.
Why Encapsulation and Abstraction Are Fundamental in
Object-Oriented Programming (OOP)
In object-oriented programming (OOP), both encapsulation and abstraction play crucial roles in designing
robust and maintainable software and they also help maintain and allow the reuse of code.
Encapsulation
It is considered good practice to declare your class attributes as private (as often as you can). By preventing
external entities from altering an object’s state directly, encapsulation enforces a controlled interface for
interacting with the object, thus reducing the risk of bugs and making the system more secure.
Benefits of Encapsulation:
• Data hiding: By encapsulating data within a class, we prevent direct access to it from outside the
class. This ensures that the data remains consistent and valid.
• Access Control: Encapsulation allows us to control how data is accessed and manipulated. We
expose only necessary methods (such as getters and setters) to interact with the data.
• Modularity: Encapsulation promotes modular design by encapsulating related data and behavior
together. Changes to the internal implementation do not affect the external code that uses the class.
Abstraction
Abstract classes and interface define methods without implementing them, leaving the details to be handled
by subclasses and promotes code flexibility.
Benefits of Abstraction:
• Simplification: Abstraction allows developers to work with a simplified view of an object, ignoring
the intricate details. It helps manage program complexity.
• Reusability: It enables the reuse of objects in a program without knowing their internal details
.
In summary, encapsulation protects data and methods within a class, while abstraction simplifies the
interaction with objects by focusing on essential features. Encapsulation and abstraction are closely related
and often used together in C++ programming, together they form the foundation of OOP promoting
modularity, security and maintainability.
REFERENCES
1. Difference between abstraction and encapsulation in C++. GeekforGeeks (n.d.).
Retrieved June 5, 2024, from
https://www.geeksforgeeks.org/difference-between-abstraction-and-encapsulation-in-c/
2. Encapsulation in C++. GeekforGeeks (n.d.).
Retrieved June 3, 2024, from
https://www.geeksforgeeks.org/encapsulation-in-cpp/
3. Abstraction in C++. GeekforGeeks (n.d.).
Retrieved June 3, 2024, from
https://www.geeksforgeeks.org/abstraction-in-cpp/
4. Encapsulation in C++. PrepBytes Blog (n.d.).
Retrieved June 2, 2024, from
https://www.prepbytes.com/blog/cpp-programming/encapsulation-in-cpp/
5. Community Contributors. Difference between abstraction and encapsulation?. Stack Overflow
[Online forum post] (n.d.).
Retrieved June 4, 2024, from
https://stackoverflow.com/questions/15176356/difference-between-encapsulation-and-
abstraction/65543358#65543358