C++C++11C++14C++17Introduction to C++Learn C++

Learn about C++ Encapsulation

Object Oriented Programming is a way to integrate with objects which can contain data (in the form of attributes or properties of objects), and code blocks in the form of procedures (methodsfunctions of objects). These attributes and methods are variables and functions that belong to the class, they are generally referred to as class members. In C++, classes have members (attributesmethods) and we should protect each member inside this class. How we can use these all members inside the class and how we can protect them from other classes, applications, or users? The answer is, you all need to learn Encapsulation.

Encapsulation term in C++ is used to cover attributes (properties) and methods (functions) inside a single statement called Class. In another term, it is wrapping up data and information under a single unit. Encapsulation is needed to be sure that sensitive data is hidden from users. It is to prevent access to the data directly, the access to them is provided through the functions of the class.

Data Encapsulation term is a mechanism of bundling the data, and the functions that use them and Data Abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user. This is mostly related to attributes in a class.

We can do encapsulation by using Classes wisely, we must declare these class attributes or methods as private which means they cannot be accessed from outside the class. They also can be used inside the class or by the class attributes of that class or by the methods in that class.

To do this:
1. We should make all the data members private
2. We should create public setter and getter functions for each data member in such a way that the set function sets the value of the data member and get the function get the value of the data member.

Note that if we want others to read or modify the value of a private member of a class, we can use public get and set methods to reach these private members.

For example, let’s assume that salary of a human is a private thing, and let’s define this as private. In this example, we can define set_salary() and get_salary() public methods and we can set and get private salary value by using these functions. Here is a THuman class with set and get Encapsulation methods used,

Note that these set_salary() and get_salary() methods are defined in public section. Now, we can set salary by using set_salary() method and we can get salary value by using get_salary() methods. Here is the full code to show how to use these Encapsulation methods,

Encapsulation methods are really necessary for the increased security of data. Thus, we highly recommend that declare your class attributes as private as you can. Encapsulation allows you or others to change one part of the data without affecting other parts of data, which means it ensures better control on your data. These kinds of methods show how the C++ Programming language is safe and secure in data-integrated applications.

Get started building powerful apps with C++Builder!

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20

What Is The Stack (std::stack) In Modern C++?

C++C++11C++14C++17C++20Learn C++

What Is The Queue (std::queue) In Modern C++?

C++C++11C++14C++17Learn C++SyntaxTemplates

What Are The Logical Operation Metafunctions In Modern C++?

C++C++14C++17C++20Learn C++

What Are The Deprecated C++14 Features In C++17?

Worth reading...
Learn Classes and Objects in C++