Encapsulation
Introduction
• Encapsulation is one of the key features of object-
oriented programming.
• It involves the bundling of data members and functions
inside a single class.
• Bundling similar data members and functions inside a
class together also helps in data hiding.
• In general, encapsulation is a process of wrapping
similar code in one place.
• In C++, we can bundle data members and functions
that operate together inside a single class
Example
class Rectangle {
public:
int length;
int breadth;
int getArea() {
return length * breadth;
}
};
• In the above program, the function getArea() calculates
the area of a rectangle.
• To calculate the area, it needs length and breadth.
• Hence, the data members (length and breadth) and the
function getArea() are kept together in the Rectangle
class.
Example
• Program
• In the this example, we are calculating the area of a
rectangle.
• To calculate an area, we need two variables: length and
breadth and a function: getArea().
• Hence, we bundled these variables and function inside a
single class named Rectangle.
• Here, the variables and functions can be accessed from
other classes as well.
• Encapsulation refers to the bundling of related fields and
methods together. This can be used to achieve data
hiding. Encapsulation in itself is not data hiding.
Why Encapsulation
• Encapsulation helps us keep related data and functions
together, which makes our code cleaner and easy to
read.
• It helps to control the modification of our data
members.
• Consider a situation where we want the length field in a
class to be non-negative.
• Here we can make the length variable private and apply
the logic inside the method setAge()
class Rectangle {
private:
int age;
public:
void setLength(int len) {
if (len >= 0)
length = len;
}
};
• The getter and setter functions provide read-only or write-
only access to our class members.
• getLength() // provides read-only access
• setLength() // provides write-only access
• It helps to decouple components of a system. We can
encapsulate code into multiple bundles.
• These decoupled components (bundles) can be
developed, tested, and debugged independently and
concurrently.
• Any changes in a particular component do not have any
effect on other components.
Data Hiding
• Data hiding is a way of restricting the access of our data
members by hiding the implementation details.
• Encapsulation also provides a way for data hiding.
Data Hiding Using the private Specifier
• Here, we have made the length and breadth variables
private.
• This means that these variables cannot be directly
accessed outside of the Rectangle class.
• To access these private variables, we have used public
functions setLength(), getLength(), setBreadth(),
and getBreadth(). These are called getter and setter
functions.
• Making the variables private allowed us to restrict
unauthorized access from outside the class. This is data
hiding.
Friend Function and Friend
Classes
Introduction
• Data hiding is a fundamental concept of object-oriented
programming.
• It restricts the access of private members from outside
of the class.
• Similarly, protected members can only be accessed by
derived classes and are inaccessible from outside
• However, there is a feature in C++ called friend
functions that break this rule and allow us to access
member functions from outside the class.
Friend Function
• A friend function can access the private and protected
data of a class.
• We declare a friend function using the friend keyword
inside the body of the class.
• Syntax
class className {
... .. ...
friend returnType functionName(arguments);
... .. ...
}
An example _friend Function.
• Here, addFive() is a friend function that can access
both private and public data members.
• A more meaningful use would be operating on
objects of two different classes. That's when the
friend function can be very helpful.
• Example-Members of Two Different Classes
• In this program, ClassA and ClassB have declared add()
as a friend function.
• Thus, this function can access private data of both
classes.
• One thing to notice here is the friend function inside
ClassA is using the ClassB.
• However, we haven't defined ClassB at this point.
// inside classA
friend int add(ClassA, ClassB);
• For this to work, we need a forward declaration of
ClassB in our program.
// forward declaration
class ClassB;
Friend Class
class ClassB;
class ClassA {
// ClassB is a friend class of ClassA
friend class ClassB;
... .. ...
}
class ClassB {
... .. ...
}
• When a class is declared a friend class, all the member
functions of the friend class become friend functions.
• Since ClassB is a friend class, we can access all
members of ClassA from inside ClassB.
• However, we cannot access members of ClassB from
inside ClassA.
• It is because friend relation in C++ is only granted, not
taken.
• Friend Class
• Here, ClassB is a friend class of ClassA. So, ClassB has
access to the members of classA.
• In ClassB, we have created a function add() that returns
the sum of numA and numB.
• Since ClassB is a friend class, we can create objects of
ClassA inside of ClassB.