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.

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

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
Artificial Intelligence TechC++C++11C++14C++17Language FeatureLearn C++

Learn How To Read The WAV Waveform Audio File Format In C++

C++C++11Learn C++

What Is Assignment Operator Overloading?

C++C++11Introduction to C++Learn C++Syntax

Full List Of Features In C++ 11

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

How To Use Basic Methods Of Strings In C++?

Worth reading...
Learn About C++ Functions