Topic-Encapsultion
Defination of Encapsultion -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.
Consider a real-life example of encapsulation, in a
company, there are different sections like the accounts
section, finance section, sales section, etc. Now,
The finance section handles all the financial transactions
and keeps records of all the data related to finance.
Similarly, the sales section handles all the sales-related
activities and keeps records of all the sales.
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
Below are the 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.
Role of Access Specifiers in Encapsulation
Access specifiers facilitate Data Hiding in C++ programs by
restricting access to the class member functions and data members.
There are three types of access specifiers in C++:
1-Private: Private access specifier means that the member function or
data member can only be accessed by other member functions of the
same class.
2-Protected: A protected access specifier means that the member
function or data member can be accessed by other member functions of
the same class or by derived classes.
3-Public: Public access specifier means that the member function or
data member can be accessed by any code.
SData Hiding: By encapsulating data within a class, you can restrict direct access to it. This
prevents accidental modification and ensures data integrity.
Modularity: Encapsulation promotes modularity by breaking down complex systems into
smaller, self-contained units.
Code Reusability: Encapsulated classes can be reused in different parts of a program or even
in different projects.
Security: By controlling access to data, encapsulation can enhance security by preventing
unauthorized modification.
Maintainability: Encapsulated code is easier to maintain and debug as changes to one part of
the code are less likely to affect other parts.
Advantages of Encapsulation
1-The primary benefit of using Encapsulation is that it hides data from other
methods. By making the data private, the data is only used within the class and is not
available outside of the class.
2-Data is protected from unwanted users.
3-This idea is applicable in the marketing and finance sectors, where security and
restricted data access to different departments are in high demand.
4-Encapsulation assists in the binding of a class's member methods and data.
5-Encapsulation also assists in the creation of flexible code that is simple to change
and manage.
Disadvantage
1-Outside of the class, private info cannot be accessed.
Features of Encapsulation
Encapsulation has the following features:
1-We cannot immediately access any of the class's functions. To reach that function,
we need an object that uses the class's member variables.
2-Encapsulation is defined as the use of only member variables in a method created
within a class.
3-We don't call it encapsulation if we don't create a method within the class that
uses the class's member variable.
4-Data protection should be improved.
6-It aids in the management of data member modification.
Benefits of Encapsulation
1-Data Protection: Encapsulation provides a protective barrier around the class data,
preventing external code from directly accessing or modifying it. This enhances data
integrity and security.
2-Code Organization: By encapsulating data and methods within a class, code
becomes more organized and modular. This makes it easier to understand, maintain,
and update.
3-Flexibility: Encapsulation allows the internal implementation of a class to change
without affecting the external code that uses the class. This improves code flexibility
and promotes easier updates.
4-Code Reusability: Encapsulated classes can be reused in different parts of a
program or in other projects, promoting code reusability.
Encapsulation Program:
#include <iostream>
#include <string>
using namespace std;
class Person {
private:
string name;
int age;
public:
void setName(string name) {
this->name = name;
}
string getName() {
return name;
}
void setAge(int age) {
if (age >= 0) {
this->age = age;
} else {
cout << "Age cannot be negative." << endl;
}
}
int getAge() {
return age;
}
};
int main() {
Person person1;
person1.setName("Alice");
person1.setAge(30);
cout << "Name: " << person1.getName() << endl;
cout << "Age: " << person1.getAge() << endl;
return 0;
}
Explanation:
1-Class Declaration:
1. We define a Person class with two private data members: name and age.
2. We also define public member functions: setName, getName, setAge, and
getAge.
2-Private Members:
1. The name and age members are declared as private. This means they can only be
accessed within the class itself, not from outside. This ensures data encapsulation
and prevents accidental modification.
3-Public Member Functions:
.The public member functions provide a controlled interface to access and modify the private data
members.
.setName and setAge are used to set the values of name and age, respectively. They can perform
validation or other operations before assigning the values.
.getName and getAge are used to retrieve the values of name and age, respectively.
4-Main Function:
1. We create an instance of the Person class called person1.
2. We use the public member functions to set the name and age of person1.
3. We then use sss
4. the getName and getAge functions to retrieve and print the values.
Output:
Name: Alice
Age: 30
Key Points:
Encapsulation promotes code modularity, reusability, and maintainability.
By hiding implementation details and providing a well-defined interface, encapsulation
makes code easier to understand and modify.
It helps in preventing unintended side effects and ensures data integrity.
Proper use of access specifiers (public, private, protected) is crucial for effective
encapsulation.
By understanding and applying encapsulation, you can write more robust, secure, and
well-structured C++ programs.