SlideShare a Scribd company logo
3
Most read
4
Most read
14
Most read
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

PPTX
Encapsulation
PPTX
Encapsulation C++
PPTX
encapsulation
PDF
Chapter2 Encapsulation (Java)
PPTX
Fundamentals of OOP (Object Oriented Programming)
PPTX
concept of oops
PPTX
Introduction to oop
PPTX
Encapsulation
Encapsulation
Encapsulation C++
encapsulation
Chapter2 Encapsulation (Java)
Fundamentals of OOP (Object Oriented Programming)
concept of oops
Introduction to oop
Encapsulation

What's hot (20)

PPTX
Java program structure
PPTX
Access specifiers(modifiers) in java
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PDF
Class and Objects in Java
PPT
Data members and member functions
PDF
Java I/o streams
PPTX
INLINE FUNCTION IN C++
PPTX
constructors in java ppt
PPTX
Inheritance in java
PDF
C++ OOPS Concept
PPTX
Method overloading
PPTX
Linked list
PPT
standard template library(STL) in C++
PPTX
Classes, objects in JAVA
PPTX
Interfaces in java
PPT
Function overloading(c++)
PPTX
Constructor in java
PDF
Code optimization in compiler design
PPT
Class and object in C++
Java program structure
Access specifiers(modifiers) in java
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Class and Objects in Java
Data members and member functions
Java I/o streams
INLINE FUNCTION IN C++
constructors in java ppt
Inheritance in java
C++ OOPS Concept
Method overloading
Linked list
standard template library(STL) in C++
Classes, objects in JAVA
Interfaces in java
Function overloading(c++)
Constructor in java
Code optimization in compiler design
Class and object in C++
Ad

Viewers also liked (17)

PPTX
Capsule technology [autosaved]
PPT
Soft gelatin capsule
PPTX
Encapsulation_ Problem and Remedies
PPT
An overview of encapsulation technologies for food
PPT
Capsules -Pharmaceutics
PPTX
Flavor encapsulation assignment
PPT
Capsule
PPTX
Capsule Products Machines Manufacturer
PDF
Cgn280 semi automatic capsule filling machine
PPTX
Validation of solid dosage forms in pharmaceutical industries
PPTX
Process validation of capsules
PPTX
Small scale and large scale capsule filling machine
PPT
Microencapsulation.....in pharmacy by sandeep
PPT
Controlled Release Oral Drug Delivery System
PPT
Pharmaceutical Capsules
PPTX
Capsules
PPT
Capsule's
Capsule technology [autosaved]
Soft gelatin capsule
Encapsulation_ Problem and Remedies
An overview of encapsulation technologies for food
Capsules -Pharmaceutics
Flavor encapsulation assignment
Capsule
Capsule Products Machines Manufacturer
Cgn280 semi automatic capsule filling machine
Validation of solid dosage forms in pharmaceutical industries
Process validation of capsules
Small scale and large scale capsule filling machine
Microencapsulation.....in pharmacy by sandeep
Controlled Release Oral Drug Delivery System
Pharmaceutical Capsules
Capsules
Capsule's
Ad

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

PPTX
Module 4 Effect of Reuse on using Encapsulation.pptx
PPT
C# Encapsulation
PPTX
Encapsulation C++ Piller of OOP it is the important piller
PPTX
Prese00yq3whesfthewgdsyuvferwyjhjdfegcyjgfz.pptx
PPTX
Presentation related to Encapsulation and OOPs
PPTX
Presentation - Copy no vaperpoit asd.pptx
PPTX
oop ppt.pptxfwefwefweqwedrqwerwerweewrewe
PPTX
Presen5416846534653416354165341864adeadvdes
PPTX
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
PPT
C++Presentation 2.PPT
PPTX
Encapsulation and inheritance
PDF
encapsulation1-150816163959-lva1-app6891.pdf
PPTX
Encapsulation_in_CPP_Presentation.pptxesfwefrwefrwefwefeqweqweqweqweqweqwe
PPTX
2CPP09 - Encapsulation
PPTX
ENCAPSULATION module for IT or comsci.pptx
PPTX
g3-encapsulation.pptxyyyyyyy66te5ujuuuyyt6
PPTX
g3-encapsulationhshshshshhshshhshshshshshh
PPTX
g3-encapsulation.pptxhhhhhhhsususuududjd
PPTX
g3-encapsulation.pptxgggggghyhftyhfyyghgh
PPTX
SAD05 - Encapsulation
Module 4 Effect of Reuse on using Encapsulation.pptx
C# Encapsulation
Encapsulation C++ Piller of OOP it is the important piller
Prese00yq3whesfthewgdsyuvferwyjhjdfegcyjgfz.pptx
Presentation related to Encapsulation and OOPs
Presentation - Copy no vaperpoit asd.pptx
oop ppt.pptxfwefwefweqwedrqwerwerweewrewe
Presen5416846534653416354165341864adeadvdes
Prese00z213hfcyudegtyfwyyudeqw7tgfi7u.pptx
C++Presentation 2.PPT
Encapsulation and inheritance
encapsulation1-150816163959-lva1-app6891.pdf
Encapsulation_in_CPP_Presentation.pptxesfwefrwefrwefwefeqweqweqweqweqweqwe
2CPP09 - Encapsulation
ENCAPSULATION module for IT or comsci.pptx
g3-encapsulation.pptxyyyyyyy66te5ujuuuyyt6
g3-encapsulationhshshshshhshshhshshshshshh
g3-encapsulation.pptxhhhhhhhsususuududjd
g3-encapsulation.pptxgggggghyhftyhfyyghgh
SAD05 - Encapsulation

More from Muhammad Hammad Waseem (20)

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

Recently uploaded (20)

PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
master seminar digital applications in india
PDF
RMMM.pdf make it easy to upload and study
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Business Ethics Teaching Materials for college
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
PPH.pptx obstetrics and gynecology in nursing
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
master seminar digital applications in india
RMMM.pdf make it easy to upload and study
VCE English Exam - Section C Student Revision Booklet
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
human mycosis Human fungal infections are called human mycosis..pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Supply Chain Operations Speaking Notes -ICLT Program
Business Ethics Teaching Materials for college
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
TR - Agricultural Crops Production NC III.pdf
Microbial diseases, their pathogenesis and prophylaxis
PPH.pptx obstetrics and gynecology in nursing
Abdominal Access Techniques with Prof. Dr. R K Mishra
FourierSeries-QuestionsWithAnswers(Part-A).pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Basic Mud Logging Guide for educational purpose
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx

[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