Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

C++ Encapsulation

Sit 121 C++ ENCAPSULATION


Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users.
Encapsulation is a process of combining data members and functions in a single unit called class.
This is to prevent the access to the data directly, the access to them is provided through the
functions of the class.
It is one of the popular feature of OOP that helps in data hiding
To achieve this, you must declare class variables/attributes as private (cannot be accessed from
outside the class).
If you want others to read or modify the value of a private member, you can provide public get
and set methods.

C++ ENCAPSULATION
Encapsulation………..
All C++ programs are composed of following two fundamental elements:
Program statements (code): This is the part of a program that performs actions and they are
called functions.
Program data: The data is the information of the program which affected by the program
functions.
Encapsulation is an OOP concept that binds together the data and functions that manipulate the
data, and that keeps both safe from outside interference and misuse.
Data encapsulation led to the important OOP concept of data hiding.
Data encapsulation 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.

C++ ENCAPSULATION
Encapsulation………..
C++ supports the properties of encapsulation and data hiding through the creation of user-
defined types, called classes.
A class can contain private, protected and public members.
By default, all items defined in a class are private.
For example: class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
double setLength()
{
return length ;
}

private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
C++ ENCAPSULATION
Encapsulation………..
The variables length, breadth, and height are private.
◦ This means that they can be accessed only by other members of the Box class, and not by any other
part of your program.
◦ This is one way encapsulation is achieved.

To make parts of a class public (i.e., accessible to other parts of your program), you must declare
them after the public keyword. All variables or functions defined after the public specifier are
accessible by all other functions in your program.
Making one class a friend of another exposes the implementation details and reduces
encapsulation. The ideal is to keep as many of the details of each class hidden from all other
classes as possible.

C++ ENCAPSULATION
How Encapsulation done in C++
To do this:
1) Make all the data members private.
2) Create public setter and getter functions for each data member in such a way that the set
function set the value of data member and get function get the value of data

C++ ENCAPSULATION
Data Encapsulation Example:
Any C++ program where you implement a class with public and private members is an example
of data encapsulation and data abstraction.

C++ ENCAPSULATION
#include <iostream> int main( )
using namespace std; {
class Adder{ Adder a;
public: a.addNum(10);
// constructor a.addNum(20);
Adder(int i = 0) a.addNum(30);
{
total = i; cout << "Total " << a.getTotal() <<endl;
} return 0;
// interface to outside world }
void addNum(int number)
{ When the above code is compiled and executed, it produces
total += number; following result:
}
// interface to outside world Total 60
int getTotal()
{
return total; Above class adds numbers together, and returns the sum. The public
};
private: members addNum and getTotal are the interfaces to the outside world and
// hidden data from outside world
int total; a user needs to know them to use the class. The private membertotal is
}; something that is hidden from the outside world, but is needed for the class
to operate properly.

C++ ENCAPSULATION
Access Private Members
To access a private attribute, use public "get" and "set" methods:
#include <iostream>
using namespace std;
class Employee {
private:
// Private attribute
int salary;
public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};
int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}

C++ ENCAPSULATION
Example explained

 The salary attribute is private, which have restricted access.


 The public setSalary() method takes a parameter (s) and assigns
it to the salary attribute (salary = s).
The public getSalary() method returns the value of the
private salary attribute.
 Inside main(), we create an object of the Employee class.
Now we can use the setSalary() method to set the value of the
private attribute to 50000.
Then we call the getSalary() method on the object to return the
value.

ACSC 328: OOP (C++) C++ ENCAPSULATION


Why Encapsulation?
• It is considered good practice to declare your class attributes as private (as often as you
can).
• Encapsulation ensures better control of your data, because you (or others) can change one
part of the code without affecting other parts
• Increased security of data

C++ ENCAPSULATION

You might also like