SlideShare a Scribd company logo
1 of 14
Encapsulation
(Information Hiding)
Speaker: Muhammad Hammad Waseem
m.hammad.wasim@gmail.com
Encapsulation
• “ to enclose in or as in a capsule ”
• The object-oriented meaning of encapsulation is
• to enclose related data, routines and definitions in a class capsule. This does not
necessarily mean hiding. (Mish)
• Encapsulation is the ‘bundling together’ of data and
behavior so that they are inseparable. (Mcgraw Hill)
Why Encapsulation called Information Hiding
• Encapsulation (also called information hiding) consists of separating
the external aspects of an object, from the internal implementation
details of the object, which are hidden from other objects.
• Encapsulation prevents a program from becoming to interdependent
that a small change has massive ripple effects.
• Encapsulation has ability to combine data structure and behavior in a
single entity makes encapsulation cleaner and more powerful than in
conventional languages that separate data structure and behavior.
Information Hiding
• Information Hiding: a module is characterized by the information it
hides from other modules, which are called its clients. The hidden
information remains a secret to the client modules. (Ghezzi et al)
• Information hiding is the principle that users of a software component
(such as a class) need to know only the essential details of how to
initialize and access the component, and do not need to know the
details of the implementation. (Budd)
What Encapsulation Provides in OO?
• The interface is the visible surface of the capsule.
• The interface describes the essential characteristics of objects of the class
which are visible to the exterior world.
• Interface data – which should be visible from outside/other class or method.
• The implementation is hidden in the capsule.
• The implementation hiding means that data can only be manipulated, that is
updated, within the class, but it does not mean hiding interface data.
• Implementation data – which should be hidden from outside/other class or
method.
General 3 Ways to Encapsulate
Data
Public Member Access Specifier
Private Member Access Specifier
Protected Member Access Specifier
Public Member Access Specifier
• Syntax
• public: <declarations>
• Description:
• A public member can be accessed by any function.
• Members of a struct or union are public by default.
• You can override the default struct access with private or protected but you
cannot override the default union access.
• Friend declarations are not affected by these access specifiers.
Private Member Access Specifier
• Syntax
• private: <declarations>
• Description:
• A private member can be accessed only by member functions and friends of
the class in which it is declared.
• Class members are private by default.
• You can override the default struct access with private or protected but you
cannot override the default union access.
• Friend declarations are not affected by these access specifiers.
Protect Member Access Specifier
• Syntax
• protected: <declarations>
• Description:
• A protected member can be accessed by member functions and friends of the
class in which it was declared, and by classes derived (derived classes) from
the declared class.
• You can override the default struct access with private or protected but you
cannot override the default union access.
• Friend declarations are not affected by these access specifiers.
Example: Access Specifiers
class MyClass
{
public: //access from anywhere
int x;
private: //only access from within a class
int y;
protected: //access from within a class ,or derived class
int z;
};
void main()
{
MyClass CopyClass;
CopyClass.x = 1; //OK, Public Access.
CopyClass.y = 2; //Error! Y isn't a member of MyClass
CopyClass.z = 3; //Error! Z isn't a member of MyClass
}
Advantage of Encapsulation
• It prevents others accessing the insides of an object.
• The only thing that can manipulate the data in an object is that object’s
method or member function.
• It main aim is to prevent accident.
• It builds a protective wall (encapsulation) around the member data and
member function of the class, and hiding implementation details of object. So
It keeps data safe from accident.
Example: Encapsulation Or Information Hiding
(1/3)
#include <iostream>
// Declaration of the Box class.
class Box
{
private:
int height, width, depth; // private data members.
public:
Box(int, int, int);// constructor function.
~Box(); // destructor function.
int volume(); // member function (compute volume).
};
Example: Encapsulation Or Information Hiding
(2/3)
// Definition of the Box class.
Box::Box(int ht, int wd, int dp)// The constructor function.
{
height = ht;
width = wd;
depth = dp;
}
Box::~Box() //The destructor function. Use of scope resolution ‘::’
{
// does nothing
}
int Box::volume() // Member function to compute the Box's volume.
{
return height * width * depth;
}
Example: Encapsulation Or Information Hiding
(3/3)
// The main() function.
int main()
{
// Construct a Box object.
Box thisbox(7, 8, 9); // actual values
// Compute and display the object's volume.
int volume = thisbox.volume();
cout << “output volume is : “ << volume;
return 0;
}
RESULT:
output volume is: 504

More Related Content

What's hot

Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
Abhilash Nair
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
ShahinAhmed49
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in java
Tech_MX
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 

What's hot (20)

XML Schema
XML SchemaXML Schema
XML Schema
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Access specifier
Access specifierAccess specifier
Access specifier
 
OOP Introduction with java programming language
OOP Introduction with java programming languageOOP Introduction with java programming language
OOP Introduction with java programming language
 
Java package
Java packageJava package
Java package
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Lecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptxLecture_7-Encapsulation in Java.pptx
Lecture_7-Encapsulation in Java.pptx
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
Inheritance in OOPS
Inheritance in OOPSInheritance in OOPS
Inheritance in OOPS
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in java
 
encapsulation
encapsulationencapsulation
encapsulation
 
Encapsulation
EncapsulationEncapsulation
Encapsulation
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
 
Classes and Objects
Classes and Objects  Classes and Objects
Classes and Objects
 
Methods in C#
Methods in C#Methods in C#
Methods in C#
 

Viewers also liked

Encapsulation_ Problem and Remedies
Encapsulation_ Problem and RemediesEncapsulation_ Problem and Remedies
Encapsulation_ Problem and Remedies
Bhushan Ghike
 
Small scale and large scale capsule filling machine
Small scale and large scale capsule filling machineSmall scale and large scale capsule filling machine
Small scale and large scale capsule filling machine
ceutics1315
 
Pharmaceutical Capsules
Pharmaceutical CapsulesPharmaceutical Capsules
Pharmaceutical Capsules
jojohen
 

Viewers also liked (17)

Capsule technology [autosaved]
Capsule technology [autosaved]Capsule technology [autosaved]
Capsule technology [autosaved]
 
Soft gelatin capsule
Soft gelatin capsuleSoft gelatin capsule
Soft gelatin capsule
 
Encapsulation_ Problem and Remedies
Encapsulation_ Problem and RemediesEncapsulation_ Problem and Remedies
Encapsulation_ Problem and Remedies
 
An overview of encapsulation technologies for food
An overview of encapsulation technologies for foodAn overview of encapsulation technologies for food
An overview of encapsulation technologies for food
 
Capsules -Pharmaceutics
Capsules -PharmaceuticsCapsules -Pharmaceutics
Capsules -Pharmaceutics
 
Flavor encapsulation assignment
Flavor encapsulation assignmentFlavor encapsulation assignment
Flavor encapsulation assignment
 
Capsule
CapsuleCapsule
Capsule
 
Capsule Products Machines Manufacturer
Capsule Products Machines ManufacturerCapsule Products Machines Manufacturer
Capsule Products Machines Manufacturer
 
Cgn280 semi automatic capsule filling machine
Cgn280 semi automatic capsule filling machineCgn280 semi automatic capsule filling machine
Cgn280 semi automatic capsule filling machine
 
Validation of solid dosage forms in pharmaceutical industries
Validation of solid dosage forms in pharmaceutical industries Validation of solid dosage forms in pharmaceutical industries
Validation of solid dosage forms in pharmaceutical industries
 
Process validation of capsules
Process validation of capsulesProcess validation of capsules
Process validation of capsules
 
Small scale and large scale capsule filling machine
Small scale and large scale capsule filling machineSmall scale and large scale capsule filling machine
Small scale and large scale capsule filling machine
 
Microencapsulation.....in pharmacy by sandeep
Microencapsulation.....in pharmacy   by sandeepMicroencapsulation.....in pharmacy   by sandeep
Microencapsulation.....in pharmacy by sandeep
 
Controlled Release Oral Drug Delivery System
Controlled Release Oral Drug Delivery SystemControlled Release Oral Drug Delivery System
Controlled Release Oral Drug Delivery System
 
Pharmaceutical Capsules
Pharmaceutical CapsulesPharmaceutical Capsules
Pharmaceutical Capsules
 
Capsules
CapsulesCapsules
Capsules
 
Capsule's
Capsule'sCapsule's
Capsule's
 

Similar to [OOP - Lec 08] Encapsulation (Information Hiding)

Abstraction file
Abstraction fileAbstraction file
Abstraction file
James Wong
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
Fraboni Ec
 
Assignment
AssignmentAssignment
Assignment
bhup_289
 
"Study of Java Access Control Mechanism”
"Study of Java Access Control Mechanism”"Study of Java Access Control Mechanism”
"Study of Java Access Control Mechanism”
IOSR Journals
 

Similar to [OOP - Lec 08] Encapsulation (Information Hiding) (20)

Encapsulation and inheritance
Encapsulation and inheritanceEncapsulation and inheritance
Encapsulation and inheritance
 
Encapsulation C++ Piller of OOP it is the important piller
Encapsulation C++ Piller of OOP it is the important pillerEncapsulation C++ Piller of OOP it is the important piller
Encapsulation C++ Piller of OOP it is the important piller
 
Encapsulation C++ Piller of OOP it is the important piller
Encapsulation C++ Piller of OOP it is the important pillerEncapsulation C++ Piller of OOP it is the important piller
Encapsulation C++ Piller of OOP it is the important piller
 
C++Presentation 2.PPT
C++Presentation 2.PPTC++Presentation 2.PPT
C++Presentation 2.PPT
 
27c
27c27c
27c
 
27csharp
27csharp27csharp
27csharp
 
Object Oriented Programming
Object Oriented ProgrammingObject Oriented Programming
Object Oriented Programming
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
Abstraction file
Abstraction fileAbstraction file
Abstraction file
 
OOP.ppt
OOP.pptOOP.ppt
OOP.ppt
 
Assignment
AssignmentAssignment
Assignment
 
Python-Encapsulation.pptx
Python-Encapsulation.pptxPython-Encapsulation.pptx
Python-Encapsulation.pptx
 
"Study of Java Access Control Mechanism”
"Study of Java Access Control Mechanism”"Study of Java Access Control Mechanism”
"Study of Java Access Control Mechanism”
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 

More from Muhammad Hammad Waseem

More from Muhammad Hammad Waseem (20)

[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++[ITP - Lecture 16] Structures in C/C++
[ITP - Lecture 16] Structures in C/C++
 
[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types[ITP - Lecture 15] Arrays & its Types
[ITP - Lecture 15] Arrays & its Types
 
[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion[ITP - Lecture 14] Recursion
[ITP - Lecture 14] Recursion
 
[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers[ITP - Lecture 13] Introduction to Pointers
[ITP - Lecture 13] Introduction to Pointers
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++[ITP - Lecture 11] Loops in C/C++
[ITP - Lecture 11] Loops in C/C++
 
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
[ITP - Lecture 10] Switch Statement, Break and Continue Statement in C/C++
 
[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++[ITP - Lecture 09] Conditional Operator in C/C++
[ITP - Lecture 09] Conditional Operator in C/C++
 
[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)[ITP - Lecture 08] Decision Control Structures (If Statement)
[ITP - Lecture 08] Decision Control Structures (If Statement)
 
[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++[ITP - Lecture 07] Comments in C/C++
[ITP - Lecture 07] Comments in C/C++
 
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
[ITP - Lecture 06] Operators, Arithmetic Expression and Order of Precedence
 
[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes[ITP - Lecture 05] Datatypes
[ITP - Lecture 05] Datatypes
 
[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++[ITP - Lecture 04] Variables and Constants in C/C++
[ITP - Lecture 04] Variables and Constants in C/C++
 
[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++[ITP - Lecture 03] Introduction to C/C++
[ITP - Lecture 03] Introduction to C/C++
 
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
[ITP - Lecture 02] Steps to Create Program & Approaches of Programming
 
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
[ITP - Lecture 01] Introduction to Programming & Different Programming Languages
 
[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance[OOP - Lec 20,21] Inheritance
[OOP - Lec 20,21] Inheritance
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 

Recently uploaded

The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
PECB
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Role Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptxRole Of Transgenic Animal In Target Validation-1.pptx
Role Of Transgenic Animal In Target Validation-1.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Asian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptxAsian American Pacific Islander Month DDSD 2024.pptx
Asian American Pacific Islander Month DDSD 2024.pptx
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 

[OOP - Lec 08] Encapsulation (Information Hiding)

  • 1. Encapsulation (Information Hiding) Speaker: Muhammad Hammad Waseem m.hammad.wasim@gmail.com
  • 2. Encapsulation • “ to enclose in or as in a capsule ” • The object-oriented meaning of encapsulation is • to enclose related data, routines and definitions in a class capsule. This does not necessarily mean hiding. (Mish) • Encapsulation is the ‘bundling together’ of data and behavior so that they are inseparable. (Mcgraw Hill)
  • 3. Why Encapsulation called Information Hiding • Encapsulation (also called information hiding) consists of separating the external aspects of an object, from the internal implementation details of the object, which are hidden from other objects. • Encapsulation prevents a program from becoming to interdependent that a small change has massive ripple effects. • Encapsulation has ability to combine data structure and behavior in a single entity makes encapsulation cleaner and more powerful than in conventional languages that separate data structure and behavior.
  • 4. Information Hiding • Information Hiding: a module is characterized by the information it hides from other modules, which are called its clients. The hidden information remains a secret to the client modules. (Ghezzi et al) • Information hiding is the principle that users of a software component (such as a class) need to know only the essential details of how to initialize and access the component, and do not need to know the details of the implementation. (Budd)
  • 5. What Encapsulation Provides in OO? • The interface is the visible surface of the capsule. • The interface describes the essential characteristics of objects of the class which are visible to the exterior world. • Interface data – which should be visible from outside/other class or method. • The implementation is hidden in the capsule. • The implementation hiding means that data can only be manipulated, that is updated, within the class, but it does not mean hiding interface data. • Implementation data – which should be hidden from outside/other class or method.
  • 6. General 3 Ways to Encapsulate Data Public Member Access Specifier Private Member Access Specifier Protected Member Access Specifier
  • 7. Public Member Access Specifier • Syntax • public: <declarations> • Description: • A public member can be accessed by any function. • Members of a struct or union are public by default. • You can override the default struct access with private or protected but you cannot override the default union access. • Friend declarations are not affected by these access specifiers.
  • 8. Private Member Access Specifier • Syntax • private: <declarations> • Description: • A private member can be accessed only by member functions and friends of the class in which it is declared. • Class members are private by default. • You can override the default struct access with private or protected but you cannot override the default union access. • Friend declarations are not affected by these access specifiers.
  • 9. Protect Member Access Specifier • Syntax • protected: <declarations> • Description: • A protected member can be accessed by member functions and friends of the class in which it was declared, and by classes derived (derived classes) from the declared class. • You can override the default struct access with private or protected but you cannot override the default union access. • Friend declarations are not affected by these access specifiers.
  • 10. Example: Access Specifiers class MyClass { public: //access from anywhere int x; private: //only access from within a class int y; protected: //access from within a class ,or derived class int z; }; void main() { MyClass CopyClass; CopyClass.x = 1; //OK, Public Access. CopyClass.y = 2; //Error! Y isn't a member of MyClass CopyClass.z = 3; //Error! Z isn't a member of MyClass }
  • 11. Advantage of Encapsulation • It prevents others accessing the insides of an object. • The only thing that can manipulate the data in an object is that object’s method or member function. • It main aim is to prevent accident. • It builds a protective wall (encapsulation) around the member data and member function of the class, and hiding implementation details of object. So It keeps data safe from accident.
  • 12. Example: Encapsulation Or Information Hiding (1/3) #include <iostream> // Declaration of the Box class. class Box { private: int height, width, depth; // private data members. public: Box(int, int, int);// constructor function. ~Box(); // destructor function. int volume(); // member function (compute volume). };
  • 13. Example: Encapsulation Or Information Hiding (2/3) // Definition of the Box class. Box::Box(int ht, int wd, int dp)// The constructor function. { height = ht; width = wd; depth = dp; } Box::~Box() //The destructor function. Use of scope resolution ‘::’ { // does nothing } int Box::volume() // Member function to compute the Box's volume. { return height * width * depth; }
  • 14. Example: Encapsulation Or Information Hiding (3/3) // The main() function. int main() { // Construct a Box object. Box thisbox(7, 8, 9); // actual values // Compute and display the object's volume. int volume = thisbox.volume(); cout << “output volume is : “ << volume; return 0; } RESULT: output volume is: 504