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.

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.

C++ Copy to Clipboard  
#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.