Categories: C++Learn C++

Encapsulation in C++ programming language

Encapsulation in C++ programming language

In this tutorial, we will discuss the OOP concept of Encapsulation in C++ programming language

Encapsulation is the process to enclose data members and methods in a single unit for security purposes. This is also known as data abstraction. This is possible in Oop languages like  C++ and Java. That means encapsulation is the method of combining data and function inside a class. This process helps to hide the data from being accessed from outside of a class directly. Only through the functions inside the class, access to information is available.

  • Data encapsulation is a mechanism used to bundle data and the function.
  • Encapsulation is used for data hiding. Data hiding is a mechanism where the details of the class are hidden from the user.
  • The user can perform only a set of operations in the hidden member of the class.
  • Encapsulation is a powerful feature(in OOP concept) that leads to information hiding for security purposes.

Benefits of encapsulation

  • Data hiding – Restriction of external access to the features of a class.
  • Information hiding – The implementation details are not known to the user.
  • Implementation independence – A change in the implementation is done easily without affecting the user interface.

Access Specifier 

There are three types of access specifier

  • private
  • public
  • protected
Encapsulation in C++

Encapsulation

In Encapsulation of C++ language,
The data members should be lebled as private using the pri vate access modifier
The member function which manipulates the data member sould be labled as public using public accessed modifier
class Class_Name
{
private://data types declared as private
datatype variable;
public://function is declared as public
member function;
};
main()
{
Class_Name objectname;//create object
objectname.functionname();//call the function
}
Example 1
Example

In the above example, variables declared as private(x,y and z are private). This means that they can be accessed only inside the addition class. The private variable can not access other parts of your program or outside of your class.

The only way to access it is by creating a object of class addition;

This is one of the ways to achieve the encapsulation

 

Example 2

This is an example for encapsulation with passing arguments

Example

 

Program 3

#include <iostream>
#include <conio.h>

using namespace std;

class Rectangle{
private: int length;//private variables
private: int breadth;
public:
    void set_Area(int l,int b)//setter method
    {
        length=l;
        breadth=b;
    }
    int get_Area(){//getter method
    return length*breadth;
    }
};
int main()
{
    Rectangle R;//create object
    R.set_Area(9,5);
    //Call the method with argument
    cout << "The area of rectangle is: "<<R.get_Area() << endl;
    getch();
    return 0;
}

The above code is executed, it produces the following result

The area of rectangle is: 45



Abstraction in C++                                           Abstraction in Java

Encapsulation in C++                                       Encapsulation in Java

Polymorphism in C++                                      Methodoverriding in Java

Method overloading in java                             Constructor overloading in Java

Exception Handling in Java

Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.