When you define a class, you also specify the private members, that is, the members that
are not available for external access, and therefore the public members of that class. An application accesses objects by using the general public methods of the category and thus activating its capacities.

Access to object data is never direct, that is, object data is generally declared as private then read or modified by methods with public declarations to make sure correct
access to the data.

One important aspect of this system is that the incontrovertible fact that application programs needn’t be aware of the internal structure of the data.

If needed, the internal structure of the program data can even be modified. Provided that the interfaces of the public methods remain unchanged, changes like these will not affect the application program.

This allows you to enhance an application by programming an improved class version without changing a single byte of the application.

An object is thus seen to encapsulate its private structure, protecting itself from external influences and managing itself by its own methods. This describes the concept of data
encapsulation concisely.

Defining classes

class Demo
{
private:
// Private data members and methods here
public:
// Public data members and methods here
};

Example of a class

using namespace std;
class Account
{
private:          // Sheltered members:
string name;      // Account holder
unsigned long nr; // Account number
double balance;   // Account balance


public:           //Public interface:
bool init( const string&, unsigned long, double);
void display();
};
#endif        // ACCOUNT

A class definition specifies the name of the class and the names and types of the class
members. The definition begins with the keyword class followed by the category name.

The data members and methods are then declared in the subsequent code block. Data members and member functions can belong to any valid type, even to another previously defined class. At the same time, the class members are divided into:

■ private members, which cannot be accessed externally
■ public members, which are available for external access

The public members form the so-called public interface of the class. The opposite page shows a schematic definition of a class. The private section generally contains data members and therefore the public section contains the access methods for the data.

This provides for data encapsulation. The following example includes a category named Account wont to represent a bank account.

The data members, such as the name of the account holder, the account number, and the account balance, are declared as private.

In addition, there are two public methods, init() for initialization purposes and display(), which is used to display the data on screen. The labels private: and public: can be used at the programmer’s discretion within a class:

■ you can use the labels as often as needed, or not at all, and in any order. A section marked as private: or public: is valid until the next public: or private: label occurs

■ the default value for member access is private. If you omit both the private
and public labels, all the class members are assumed to be private

If you like this post, don’t forget to share 🙂

This article is written by our awesome writer
Comments to: What is Data Encapsulation In Cpp

Your email address will not be published. Required fields are marked *

Attach images - Only PNG, JPG, JPEG and GIF are supported.

New Dark Mode Is Here

Sign In to access the new Dark Mode reading option.

Join our Newsletter

Get our monthly recap with the latest news, articles and resources.

By subscribing you agree to our Privacy Policy.

Latest Articles

Explore Tutorials By Categories

About

Codeverb is simply an all in one interactive learning portal, we regularly add new topics and keep improving the existing ones, if you have any suggestions, questions, bugs issue or any other queries you can simply reach us via the contact page

Login

Welcome to Codeverb

Ready to learn something new?
Join Codeverb!

Read Smart, Save Time
  •  
    Strength indicator
  •  
  •  
    Log In | Lost Password