As you've read in previous tutorials, all you know is that C++ is rich in objects and classes. So, it comes under the Object Oriented Programming category. It is the first successful Object Oriented Programming Language that has all the features of OOP with some diverse concepts. So Abstraction and Encapsulation were the first basic sets of concepts of OOP. Here Abstraction deals with the essential characteristics of an object which distinguish it from other types of objects and therefore allow for clearly defined conceptual boundaries concerning the viewer's perspective.

Concept of Encapsulation

Unable to deal with the complexity of an object, programmers ignore its non-essential details and focus only on what is essential to understanding. You can place a simple context of the object model; that Abstraction is "looking for what you want" within an object or class. But there is another key concept associated with Abstraction, which is called encapsulation.

So basically, encapsulation can be defined as the process of hiding all of the details of an object that do not throw in or deal with its essential characteristics. Encapsulation can also prevent access to non-essential details of classes or their objects. Abstraction and encapsulation have a close bonding with each other. Encapsulation assists Abstraction by providing a means of suppressing the non-essential details.

Use of Access Specifiers

Access specifiers determine whether any other class or function can access member variables and functions of a particular class or within a program.

C++ provides its programmers with three access specifiers. These are:

  • public
  • private
  • protected

Access specifiers create boundaries among the accessible and inaccessible sections of any class. It gives the programmer the privilege of designing the class by determining which section of the classes needs to be obscured from the user's point of view and further which they should be offered as an interface to the class.

  • Public: Here, the class members declared under public are obtainable and accessible to all. Other classes can also access all those public data members and member functions.
  • Private: It is used where no one can obtain or access the class members declared private outside that class. When anyone tries to access or avail the private member of that class, a compile-time error gets generated. By default, all class members remain in 'private' access mode.
  • Protected: It is a special purpose access specifier similar to private but creates class members inaccessible outside that class. But it can get accessed by any subclass of that class.

Example of a C++ Program With the Concept of Encapsulation


#include <iostream>
using namespace std;

class rectangle
    int l,b;
    rectangle(int x=2,int y=4)
        cout<<"i am parametrized";
        cout<<"i am default";
    void area()
        cout<<"\narea is = "<<l*b;

int main()
    rectangle r;
    rectangle r1(3,6);
    rectangle r2(10);

Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram