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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++C++11C++14C++17Introduction to C++Learn C++

This Is How To Get A Substring of a Wide String in C++

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

How To Access Individual Character Elements Of A C++ String

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

The Right Way To Access Character Elements Of A Wide String

C++Language FeatureLearn C++

You NEED to Learn To Use JSON (JavaScript Object Notation)

Worth reading...
Learn to Use Object Arrays and Object Pointer Arrays in C++