[go: up one dir, main page]

0% found this document useful (0 votes)
3 views4 pages

3 encapsulation

Encapsulation in C++ is the practice of bundling data and methods that manipulate that data within a single class, promoting data protection and information hiding. It restricts direct access to class members, allowing interaction only through public methods, which enhances security and maintainability. Key benefits include improved code maintainability, data hiding, code reuse, and enhanced security.

Uploaded by

Sonu Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

3 encapsulation

Encapsulation in C++ is the practice of bundling data and methods that manipulate that data within a single class, promoting data protection and information hiding. It restricts direct access to class members, allowing interaction only through public methods, which enhances security and maintainability. Key benefits include improved code maintainability, data hiding, code reuse, and enhanced security.

Uploaded by

Sonu Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Encapsulation in C++

Encapsulation in C++ is defined as the wrapping up of


data and information in a single unit. In Object Oriented
Programming, Encapsulation is defined as binding together the data
and the functions that manipulate them. Bundling similar data
members and functions inside a class together also helps in data hiding.

Example
In a company, two software projects are going on with two different
teams. One team requires data from the other team. But this team
cannot access the data from the other team because they do not have
the appropriate permissions. This is Encapsulation.

Encapsulation also leads to data hiding or abstraction.

Two Important property of Encapsulation

1. Data Protection: Encapsulation protects the internal state of


an object by keeping its data members private. Access to and
modification of these data members is restricted to the
class’s public methods, ensuring controlled and secure data
manipulation.

2. Information Hiding: Encapsulation hides the internal


implementation details of a class from external code. Only
the public interface of the class is accessible, providing
abstraction and simplifying the usage of the class while
allowing the internal implementation to be modified without
impacting external code.
Features of Encapsulation
1. We can not access any function from the class directly. We
need an object to access that function that is using the
member variables of that class.
2. The function which we are making inside the class must use
only member variables, only then it is called encapsulation.
3. If we don’t make a function inside the class which is using
the member variable of the class then we don’t call it
encapsulation.
4. Encapsulation improves readability, maintainability, and
security by grouping data and methods together.
5. It helps to control the modification of our data members.

Get and Set


You learned from the previous chapter that private variables can only
be accessed within the same class (an outside class has no access to it).
However, it is possible to access them if we provide
public get and set methods.
The get method returns the variable value, and the set method sets the
value.

Syntax for both is that they start with either get or set, followed by the
name of the variable, with the first letter in upper case:

Example --To access a private attribute, use public "get" and "set"
methods:

#include <iostream>
using namespace std;

class Employee {
private:
int salary; // Private attribute
The output of the above program is a compile time error because
wepublic:
are not allowed to access the private data members of a class
directly from outside the class.
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};

int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
}
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.

Benefits of encapsulation:

o Improved code maintainability: Encapsulation helps in improving


the code maintainability by providing a clear separation between
the implementation details of a class and its clients.
o Data hiding: Encapsulation enables data hiding, which protects
the data members of a class from being accessed and modified by
the clients of the class.
o Code reuse: Encapsulation helps in code reuse by providing a
modular design that can be easily extended and modified.
o Security: Encapsulation provides security by preventing
unauthorized access to the data members of a class.

You might also like