C++ Encapsulation

As you all know that C++ is rich in Objects and Classes. So, it comes under Object Oriented Programming category. It is the first successful Object Oriented Programming Language, with all the features of OOPs along with some varied concepts. So abstraction and encapsulation were the first basic sets of concepts of OOPs. Here Abstraction deals with the basic characteristics of an object which distinguishes from other types of objects and hence allows crisply defined conceptual boundaries, with respect to the perspective of the viewer.

Concept of Encapsulation

Unable to deal with the complexity of an object, human chooses to ignore its non-essential details and concentrate on the details which are essential to our 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 major concept connected to 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 dealt with its essential characteristics. Encapsulation can also be defined as preventing access to non-essential details of classes or its objects. Abstraction and encapsulation have close bonding among each other. Encapsulation assists abstraction by providing a means of suppressing the non-essential details.

Use of Access Specifiers

Access specifiers are used to determining 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 the boundaries among the accessible and inaccessible sections of any class. It provides with programmer the privilege to design the class for demarcating which section of the class need to be obscured from the user point of view and further which should be offered to them as an interface for the class.

  • Public: Here, the class members declared under public has to be obtainable and accessible to everyone. All those data members and member functions acknowledged as the public can be accessed by other classes also.
  • Private: Private is used where no one can obtain or access the class members declared as 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 members of a class remain in 'private' access mode.
  • Protected: It is a special purpose access specifier which is similar to private but it creates class member inaccessible outside that class. But it can get accessed by any subclass of that class.

Example of C++ Having the Concept of Inheritance

#include <iostream>
using namespace std;

class rectangle
   int l,b;
   rectangle(int x=2,int y=4)
      cout<<"i am parametrized";
   /* rectangle()
      cout<<"i am default";
   }*/   void area()
      cout<<"\narea is = "<<l*b;
int main()
   rectangle r;
   rectangle r1(3,6);
   rectangle r2(10);