PDFVersion
No ads? No problem! Download the PDF book of this tutorial for just $24.99. Your support will help us reach more readers.
Thank You!
Encapsulation:
Encapsulation serves as a programming mechanism that securely binds code and the corresponding data it handles, shielding them from external interference or misuse. In object-oriented languages, this binding creates a self-contained unit, often likened to a black box, housing all essential data and code. Consequently, when code and data intertwine in this manner, an object emerges. Put simply, an object acts as a tool supporting encapsulation.
All C++ programs consist of two fundamental elements:
Program Methods or Functions:These sections execute actions within the program and are termed functions.
Program Data:This encompasses the program's information, which undergoes alteration via program functions.
Encapsulation represents a core concept in Object-Oriented Programming (OOP), intertwining data and the functions that manipulate it, while safeguarding both from external interference and misuse.
It's crucial not to conflate Data Encapsulation with Data Abstraction. Data Encapsulation involves bundling data and the functions utilizing them, while Data Abstraction focuses on revealing only interfaces and concealing implementation details from users.
When someone presents us with a class, understanding its inner workings isn't necessary for utilization. All we need to grasp are its public methods/data—its interface. This analogy often aligns with operating a car: when driving, we're indifferent to how the steering wheel maneuvers the wheels; our concern lies solely with the interface the car provides (i.e., the steering wheel) to achieve our objectives. Interfaces abstract the intricacies of actual operation, enabling programmers to concentrate on how objects leverage each other's interfaces and interact.
This rationale underpins C++'s explicit declaration of public and private access specifiers: by default, it assumes that class definitions constitute internal details irrelevant to those employing our code. Concealing these details from client code is referred to as "Data Hiding" or rendering our class a "Black Box."
C++ facilitates encapsulation and data hiding through the creation of user-defined types, known as classes. As we're aware, a class can house private, protected, and public members. By default, all items defined within a class are private. For example:
class Addition {
private:
int UpperLimit;
int sum;
int Number;
public:
Addition() {
// body
}
void showResult() {
// body
}
};
The variables UpperLimit, sum, and Number are private, limiting access solely to other members of the Addition class, not any other section of our program. This exemplifies one approach to achieving encapsulation.
To make parts of a class public (i.e., accessible to other sections of our program), we must declare them after the public keyword. All variables or functions defined after the public specifier are accessible to all other functions in our program. In the above example, "Data Abstraction" abstracts data from outside the class, while "Data Encapsulation" bundles the data and functions.
Consider the following example. This program should return the sum of all numbers up to UpperLimit. For instance, if UpperLimit is 5, it will return 5+4+3+2+1+0=15.
#include<iostream>
using namespace std;
class Addition {
private:
int UpperLimit;
int sum;
int Number;
public:
Addition() {
UpperLimit=0;
sum=0;
Number=0;
}
void showResult() {
cout<<" Enter the Upper Limit to get sum: ";
cin>>UpperLimit;
while(Number<UpperLimit) {
Number=Number+1;
sum=sum+Number;
}
cout<<"The sum of first "<<UpperLimit<<" Number are: "<<sum;
}
};
int main() {
Addition obj;
obj.showResult();
return 0;
}
- Provides abstraction between an object and its clients.
- Shields an object from unwanted access by clients.
- For instance, a bank application forbids a client from altering an account's balance.
One way to conceptualize an object-oriented program is by defining the objects present, their respective knowledge, and subsequently, these objects exchange messages (by invoking each other's methods) to exchange information and instruct each other.
Sardar Omar
I did my hardest to present you with all of the information you need on this subject in a simple and understandable manner. However, if you have any difficulties understanding this concept or have any questions, please do not hesitate to ask. I'll try my best to meet your requirements.