Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Data Encapsulation

C++ Data Encapsulation

Data Encapsulation is a fundamental concept of Object-Oriented Programming (OOP). It is achieved by bundling data and the functions that operate on that data within a class. This encapsulation ensures the privacy and integrity of the data, preventing external code from directly accessing or modifying it.

All C++ programs have the following two basic elements:

Encapsulation is the OOP concept of binding data together with the functions that operate on that data, which avoids external interference and misuse, thereby ensuring security. Data encapsulation leads to another important OOP concept, namelydata hiding.

Data encapsulationis a mechanism that bundles data and the functions that operate on the data together,data abstractionwhile data hiding is a mechanism that exposes only the interface to the user while hiding the concrete implementation details.

In C++, by creatingclassclasses, you support encapsulation and data hiding (public, protected, private). We already know that a class contains private members (private), protected members (protected), and public members (public); by default, all items defined in a class are private. For example:

class

Box

{

public

:

double

getVolume

(

void

)

{

return

length

breadth

height

;

}

private

:

double

length

;

// length

double

breadth

;

// width

double

height

;

// height

}

;

The variables length, breadth, and height are all private. This means they can only be accessed by other members of the Box class, not by other parts of the program. This is one way to achieve encapsulation.

To make members of a class public (i.e., accessible to other parts of the program), you must use the public keyword to declare them. All variables or functions defined after the public identifier can be accessed by all other functions in the program.

Defining a class as a friend class of another class exposes implementation details, thereby reducing encapsulation. The ideal approach is to hide each class's implementation details from the outside as much as possible.

Access modifiers:

Example of Data Encapsulation

In a C++ program, any class with public and private members can serve as an example of data encapsulation and data abstraction. See the example below:

Example

#include

<

iostream

using

namespace

std

;

class

Adder

{

public

:

// Constructor

Adder

(

int

i

=

0

)

{

total

=

i

;

}

// Public interface

void

addNum

(

int

number

)

{

total

+=

number

;

}

// Public interface

int

getTotal

(

)

{

return

total

;

}

;

private

:

// Data hidden from the outside

int

total

;

}

;

int

main

(

)

{

Adder

a

;

a

.

addNum

(

10

)

;

a

.

addNum

(

20

)

;

a

.

addNum

(

30

)

;

cout

<<

"

Total

"

<<

a

.

getTotal

(

)

<<

endl

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:



Total 60

The class above adds numbers and returns the sum. The public member addNum and getTotal is the public interface that users need to know in order to use the class. The private member total is hidden from the outside; the user doesn't need to know it, yet it is necessary for the class to function correctly.

Data encapsulation is implemented through classes and access modifiers (public, private, protected). Here is a simple example:

Example

#include

using

namespace

std

;

class

Student

{

private

:

    string name

;

   

int

age

;

public

:

   

// Constructor

    Student

(

string studentName,

int

studentAge

)

{

        name

=

studentName

;

        age

=

studentAge

;

   

}

   

// Accessor function (getter)

    string getName

(

)

{

       

return

name

;

   

}

   

int

getAge

(

)

{

       

return

age

;

   

}

   

// Mutator function (setter)

   

void

setName

(

string studentName

)

{

        name

=

studentName

;

   

}

   

void

setAge

(

int

studentAge

)

{

       

if

(

studentAge

0

)

{

            age

=

studentAge

;

       

}

else

{

           

cout

<<

"Invalid age!"

<<

endl

;

       

}

   

}

   

// Print student information

   

void

printInfo

(

)

{

       

cout

<<

"Name: "

<<

name

<<

", Age: "

<<

age

<<

endl

;

   

}

}

;

int

main

(

)

{

   

// Create a Student object

    Student student1

(

"Alice"

,

20

)

;

   

// Access and modify data

    student1.

printInfo

(

)

;

    student1.

setName

(

"Bob"

)

;

    student1.

setAge

(

22

)

;

    student1.

printInfo

(

)

;

   

return

0

;

}

Design Strategy

Under normal circumstances, we set class member state to private unless we really need to expose it; this ensures goodEncapsulation.

This usually applies to data members, but it also applies to all members, including virtual functions.

Advantages of Data Encapsulation