Encapsulation in C++ with a Real-Life Example

Encapsulation in C++ with a Real-Life Example

3 mins read21.2K Views Comment
Updated on Sep 20, 2023 17:32 IST

Encapsulation is a process of combining member functions and data members in a single unit called a class. The purpose is to prevent access to the data directly. In this article, we will learn more about encapsulation, their advantages and disadvantages and how to use encapsulation in C++.

2022_05_Untitled-design-9.jpg

C++ is based on object-oriented programming(OOPs) concepts. One of the OOPs concepts is encapsulation. We have covered an article on OOPs concepts in C++. In this blog, we will be covering encapsulation in detail with real-life examples. 

So letโ€™s get started!!!

Table of contents

Recommended online courses

Best-suited C++ courses for you

Learn C++ with these high-rated online courses

โ€“ / โ€“
4 months
โ‚น4.24 K
6 weeks
โ€“ / โ€“
15 days
โ€“ / โ€“
โ€“ / โ€“
โ€“ / โ€“
70 hours
โ€“ / โ€“
3 months
โ‚น7.14 K
5 weeks
name
SoloLearnCertificateStar Icon4.5
Free
โ€“ / โ€“
โ€“ / โ€“
4 months
โ€“ / โ€“
3 months

What is encapsulation in C++?

Encapsulation is a process of combining member functions and data members in a single unit called a class. The purpose is to prevent access to the data directly. Access to them is provided through the functions of the class. It is one of the popular features of Object-Oriented Programming(OOPs), which helps in data hiding.

Real-life example

Suppose you go to an automatic teller machine(ATM) and request money. The machine processes your request and gives you money.

Here, ATM is a class. It takes data from the user(money amount and PIN) and displays data as icons and options. It processes the request(functions). So, it contains both data and functions wrapped/integrated under a single ATM. This is called Encapsulation.

2. Suppose you have a profile on some social networking website, say Facebook. If your profile password is declared as a public variable, anyone can see your password and log in to your Facebook account. So, would you like it? Obviously No.
So, Facebook declares your password as private to make your account safe so that anyone can not log in to your account. The other details of your account or posts you share can be made public or private by you, which helps you see the data-hiding concept here.

Advantages/disadvantages of Encapsulation in C++

Advantages

  • The main advantage of using Encapsulation is to hide the data from other methods. By making the data private, these data are only used within the class but are not accessible outside the class.
  • Protects data from unauthorized users 
  • This concept is applicable in the marketing and finance sector, where there is a high demand for security and restricted data access to various departments.
  • Encapsulation helps us in binding the member functions and data of a class.
  • Encapsulation also helps us make code flexible, which is easy to change and maintain.

Disadvantage

  • Private data cannot be accessed outside the class.

Access specifiers in C++

We can make our data public, private, and protected in class. This can be done with the help of access specifiers like public, private, and protected. We can change the access specifier according to our needs and data. Using these access specifiers, the programmer gets control over data visibility. That means a programmer can decide what functions or data should be hidden and what to show the user. 

NOTE: By default(if not declared), all the items in a class are private. 

  • Public: The data Members/member functions declared as public can be accessed by the same class and other classes.
  • Private: The data Members/member functions declared as private can be accessed by the same class only. Data cannot be accessed outside the class. If accessed, it will give an error.
  • Protected: The data Members/member functions declared as private can be accessed by the same class and the derived class.
Access specifier Same class Derived class Any other class
Private YES NO NO 
Protected YES YES NO 
Public YES YES YES

How to Use Encapsulation in C++?

  • First, make all the data members private.
  • Then getter(gets the value of data member) and setter (sets the value of data member)functions should be performed for each data member.

Example of Encapsulation

In this, we are adding an employeeโ€™s salary (full-time and overtime). We have a data member salary declared as private, as you can see. That means it can be used within that class only and not even in the main(). If we want to use this function in the main function, we should use the getter and setter functions.So here we have a getter function getSalary() and setter function setsalary().

Inside main(), we create an object of the company class. Now we can use the setSalary() method to set the value of the full-time and overtime salaries. Then we call the getSalary() method on the object to return the value.

Endnotes

Encapsulation is explained with a real-life analogy and a simple programming example. I hope you got an idea. If you liked the blog, then do share it with your friends.

Keep sharing information!!!

Happy Learning!!!

About the Author

This is a collection of insightful articles from domain experts in the fields of Cloud Computing, DevOps, AWS, Data Science, Machine Learning, AI, and Natural Language Processing. The range of topics caters to upski... Read Full Bio