SlideShare a Scribd company logo
1 of 15
CS Lecturer: Bibi Maryam
Encapsulation
Encapsulation Def.
 The meaning of Encapsulation, is to make sure that "sensitive" data is
hidden from users. 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.
Encapsulation’s Important points :
 Encapsulation involves combining similar data and functions into a single
unit called a class.
 By encapsulating these functions and data, we protect that data from change.
 This concept is also known as wrapping data or information hiding.
 In Object Oriented Programming, Encapsulation is defined as binding together
the data and the functions that manipulate them.
Two Important properties 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.
A real-life example of Encapsulation
1. In a company, there are different sections like the accounts section, finance
section, sales section, etc. Now,
2. The finance section handles all the financial transactions and keeps records of
all the data related to finance.
3. Similarly, the sales section handles all the sales-related activities and keeps
records of all the sales.
4. Now there may arise a situation when for some reason an official from the
finance section needs all the data about sales in a particular month.
5. In this case, he is not allowed to directly access the data of the sales section.
He will first have to contact some other officer in the sales section and then
request him to give the particular data.
Features of Encapsulation:
 Below are the features of encapsulation:
a. 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.
b. The function which we are making inside the class must use only member
variables, only then it is called encapsulation.
c. If we don’t make a function inside the class which is using the member
variable of the class then we can’t call it encapsulation.
d. Encapsulation improves readability, maintainability, and security by grouping
data and methods together.
e. It helps to control the modification of our data members.
Example:
#include <iostream>
using namespace std;
// declaring class
class Circle {
// access modifier
private:
// Data Member
float area;
float radius;
public:
void getRadius()
{
cout << "Enter radiusn";
cin >> radius;
}
void findArea()
{
area = 3.14 * radius * radius;
cout << "Area of circle=" <<
area;
}
};
int main()
{
// creating instance(object) of class
Circle cir;
cir.getRadius(); // calling function
cir.findArea(); // calling function
}
Output: Enter radius
Area of circle=0
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++:
 Private: Private access specifier means that the member function
or data member can only be accessed by other member functions
of the same class.
 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.
 Public: Public access specifier means that the member function or
data member can be accessed by any code.
 By default, all data members and member functions of a class are
made private by the compiler.
Points to Consider:
 As we have study, access specifiers play an important role in implementing
encapsulation in C++. The process of implementing encapsulation can be sub-
divided into two steps:
1) Creating a class to encapsulate all the data and methods into a single unit.
2) Hiding relevant data using access specifiers.
Getter & Setter methods:
 The getter function is used to retrieve the variable value and the setter
function is used to set the variable value. Remember: You can directly
access public member variables, but private member variables are not
accessible. Therefore, we need getter functions.
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;
}
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.
Encapsulation C++ Piller of OOP it is the important piller

More Related Content

Similar to Encapsulation C++ Piller of OOP it is the important piller

Similar to Encapsulation C++ Piller of OOP it is the important piller (20)

4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
27c
27c27c
27c
 
27csharp
27csharp27csharp
27csharp
 
27c
27c27c
27c
 
27csharp
27csharp27csharp
27csharp
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Classes and objects in c++
Classes and objects in c++Classes and objects in c++
Classes and objects in c++
 
Encapsulation and inheritance
Encapsulation and inheritanceEncapsulation and inheritance
Encapsulation and inheritance
 
Encapsulation and inheritance
Encapsulation and inheritanceEncapsulation and inheritance
Encapsulation and inheritance
 
Oops
OopsOops
Oops
 
Oops
OopsOops
Oops
 
OOP_presentation.pptx
OOP_presentation.pptxOOP_presentation.pptx
OOP_presentation.pptx
 
OOP_presentation.pptx
OOP_presentation.pptxOOP_presentation.pptx
OOP_presentation.pptx
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)Chapter2 Encapsulation (Java)
Chapter2 Encapsulation (Java)
 
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngnClass-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
 
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngnClass-١.pptx vbdbbdndgngngndngnnfndfnngn
Class-١.pptx vbdbbdndgngngndngnnfndfnngn
 

Recently uploaded

WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxAnnaArtyushina1
 
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024VictoriaMetrics
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp Krisztián
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in sowetomasabamasaba
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2
 

Recently uploaded (20)

WSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid EnvironmentsWSO2Con2024 - Software Delivery in Hybrid Environments
WSO2Con2024 - Software Delivery in Hybrid Environments
 
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of TransformationWSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
WSO2CON 2024 - Designing Event-Driven Enterprises: Stories of Transformation
 
WSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AIWSO2CON 2024 Slides - Unlocking Value with AI
WSO2CON 2024 Slides - Unlocking Value with AI
 
Artyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptxArtyushina_Guest lecture_YorkU CS May 2024.pptx
Artyushina_Guest lecture_YorkU CS May 2024.pptx
 
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and ApplicationsWSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
WSO2CON 2024 - Architecting AI in the Enterprise: APIs and Applications
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next IntegrationWSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
WSO2CON2024 - Why Should You Consider Ballerina for Your Next Integration
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAMWSO2Con2024 - Organization Management: The Revolution in B2B CIAM
WSO2Con2024 - Organization Management: The Revolution in B2B CIAM
 
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
WSO2Con2024 - Facilitating Broadband Switching Services for UK Telecoms Provi...
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
WSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration ToolingWSO2Con2024 - Low-Code Integration Tooling
WSO2Con2024 - Low-Code Integration Tooling
 
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
WSO2Con2024 - Navigating the Digital Landscape: Transforming Healthcare with ...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
WSO2CON 2024 - IoT Needs CIAM: The Importance of Centralized IAM in a Growing...
 

Encapsulation C++ Piller of OOP it is the important piller

  • 3. Encapsulation Def.  The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. 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.
  • 4. Encapsulation’s Important points :  Encapsulation involves combining similar data and functions into a single unit called a class.  By encapsulating these functions and data, we protect that data from change.  This concept is also known as wrapping data or information hiding.  In Object Oriented Programming, Encapsulation is defined as binding together the data and the functions that manipulate them.
  • 5.
  • 6. Two Important properties 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.
  • 7. A real-life example of Encapsulation 1. In a company, there are different sections like the accounts section, finance section, sales section, etc. Now, 2. The finance section handles all the financial transactions and keeps records of all the data related to finance. 3. Similarly, the sales section handles all the sales-related activities and keeps records of all the sales. 4. Now there may arise a situation when for some reason an official from the finance section needs all the data about sales in a particular month. 5. In this case, he is not allowed to directly access the data of the sales section. He will first have to contact some other officer in the sales section and then request him to give the particular data.
  • 8. Features of Encapsulation:  Below are the features of encapsulation: a. 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. b. The function which we are making inside the class must use only member variables, only then it is called encapsulation. c. If we don’t make a function inside the class which is using the member variable of the class then we can’t call it encapsulation. d. Encapsulation improves readability, maintainability, and security by grouping data and methods together. e. It helps to control the modification of our data members.
  • 9. Example: #include <iostream> using namespace std; // declaring class class Circle { // access modifier private: // Data Member float area; float radius; public: void getRadius() { cout << "Enter radiusn"; cin >> radius; } void findArea() { area = 3.14 * radius * radius; cout << "Area of circle=" << area; } }; int main() { // creating instance(object) of class Circle cir; cir.getRadius(); // calling function cir.findArea(); // calling function } Output: Enter radius Area of circle=0
  • 10. 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++:  Private: Private access specifier means that the member function or data member can only be accessed by other member functions of the same class.  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.  Public: Public access specifier means that the member function or data member can be accessed by any code.  By default, all data members and member functions of a class are made private by the compiler.
  • 11. Points to Consider:  As we have study, access specifiers play an important role in implementing encapsulation in C++. The process of implementing encapsulation can be sub- divided into two steps: 1) Creating a class to encapsulate all the data and methods into a single unit. 2) Hiding relevant data using access specifiers.
  • 12. Getter & Setter methods:  The getter function is used to retrieve the variable value and the setter function is used to set the variable value. Remember: You can directly access public member variables, but private member variables are not accessible. Therefore, we need getter functions.
  • 13. 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; }
  • 14. 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.