Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Encapsulation in C++ programming language

Posted on February 22, 2017February 24, 2020

Table of Contents

  • Encapsulation in C++ programming language
      •  Encapsulation

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++ programming language
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
 
Encapsulation in C++ programming language
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

Encapsulation in C++ programming language
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

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes