How To Implement Encapsulation In C++?

Last updated on Jul 01,2020 11.2K Views

How To Implement Encapsulation In C++?

edureka.co

C++ is a very versatile programming language and it has various object oriented programming features. In this article we would learning how to implement Encapsulation In C++. Following pointers will be covered in this article,

So let us get started the,

Encapsulation In C++

When all the data members and member functions are combined in a single unit called class, this process is called Encapsulation. In other words, wrapping the data together and the functions that manipulate them.

Example

In a company, two software projects are going on with two different teams. One team requires data from the other team. But this team cannot access the data from the other team because they do not have the appropriate permissions. This is Encapsulation.

Encapsulation also leads to data hiding or abstraction.

Moving on with this Encapsulation in C++ article,

Access Specifiers

We can implement encapsulation by using access specifiers. They give the programmer the control on what data or functions are to be made visible to the user and what is kept a secret. There are three main access specifiers,

Each of these access specifiers is used to implement Encapsulation. By default, all data members and member function are made private by the compiler.

Private Access Specifiers

When data member or member functions are made private, it can only be accessed inside the class and no one outside the class can access it. When it is accessed by any object outside the class, then an error is generated.

During inheritance, all the private members cannot be directly accessed in the derived class.

Public Access Specifiers

When data member or member functions are made public, it can be accessed by everyone. Even the other classes can access the public data members or member functions

Protected Access Specifiers

Protected Access Specifier is a special kind of access specifier. When data member or member functions are made protected, it works in a similar way to private and it can be accessed to members of the class. It can also be accessed from any subclasses of the class.

Moving on with this Encapsulation in C++ article,

Types Of Encapsulation

There are three types of encapsulations:

Member Variable Encapsulation

In this type of encapsulation, all the data members are declared as private.

Function Encapsulation

In this type of encapsulation, some of the member functions are declared as private. The constructor is public.

Class Encapsulation

 In this type of encapsulation, all a class is declared as private. This is mostly done during nested classes.

Moving on with this Encapsulation in C++ article, we will see an Encapsulation Demo.

Sample Program

#include<iostream>
using namespace std;
class test
{
private:
int x;
public:
test(int a)
{
x =a;
}
int get()
{
return x;
}
};
int main()
{
test a(7);
cout<<"The Number is: "<<a.get();
return 0;
}

Output

Explanation

In the above program, we show the concept of encapsulation. We have a private member x which cannot be accessed from the main function. The only way to access it is by creating a object of class test.

There is a parameterized constructor, which assigns the value we get from main to x. We have a get method, that returns the value of x. Inside the main function, we create an object of test class and assign a parameter. This moment the parameterized constructor is called, and it assigns the parameter value to x.

We have a cout statement that calls the get function and the number is displayed.

This is the basic concept of encapsulation.

With this we come to the end of this blog on ‘Encapsulation In C++’. I hope you found this informative and helpful, stay tuned for more tutorials on similar topics.You may also checkout our training program to get in-depth knowledge on jQuery along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.

Got a question for us? Mention them in the comments section of  this article and we will get back to you.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework