SlideShare a Scribd company logo
Encapsulation and Abstraction


Objectives
In this lesson, you will learn to:
 Define Abstraction
 Define Encapsulation
 State the relationship between abstraction and
  encapsulation
 Implement encapsulation in C++ using the private and
  public access specifiers
 Use static variables and functions
 Use friend functions and classes


©NIIT                                  OOPS/Lesson 3/Slide 1 of 21
Encapsulation and Abstraction


Abstraction
 Denotes the essential characteristics of an object that
  distinguishes it from all other kinds of objects
 Provides defined conceptual boundaries relative to the
  perspective of the viewer




©NIIT                                 OOPS/Lesson 3/Slide 2 of 21
Encapsulation and Abstraction


Encapsulation
 Is a process of hiding all the details of an object that
  do not contribute to its essential characteristics
 Prevents access to non-essential details
 Is also called information hiding or data hiding




©NIIT                                   OOPS/Lesson 3/Slide 3 of 21
Encapsulation and Abstraction


Relationship Between Abstraction and
Encapsulation
Complement each other
Encapsulation assists abstraction by providing a
 means of suppressing the non-essential details




©NIIT                               OOPS/Lesson 3/Slide 4 of 21
Encapsulation and Abstraction


Implementing Abstraction and Encapsulation
using Access Specifiers
 Public access specifier
     Allows a class to expose its member variables and
      member functions to other functions and objects
     Example:
      #includeiostream
      class Car
      {
         public:
         char color[21];
      };


©NIIT                               OOPS/Lesson 3/Slide 5 of 21
Encapsulation and Abstraction


Implementing Abstraction and Encapsulation
using Access Specifiers (Contd.)
        int main()
        {
          Car ford;
            /* The . operator is used
            to access member data and
            functions */
            cin  ford.color; /*Since
            the color variable is public, it can
            be accessed outside the class
            definition.*/
            return 0;
        }


©NIIT                            OOPS/Lesson 3/Slide 6 of 21
Encapsulation and Abstraction


Implementing Abstraction and Encapsulation
using Access Specifiers (Contd.)
 Private access specifier
     Allows a class to hide its member variables and
      member functions from other class objects and
      functions
     Offers data hiding by protecting data from external
      alteration




©NIIT                                 OOPS/Lesson 3/Slide 7 of 21
Encapsulation and Abstraction


Implementing Abstraction and Encapsulation
using Access Specifiers (Contd.)
 Protected access specifier
     Allows a class to hide its member variables and
      member functions from other class objects and
      functions just like private access specifier - is
      used while implementing inheritance




©NIIT                                 OOPS/Lesson 3/Slide 8 of 21
Encapsulation and Abstraction

Implementing Abstraction and Encapsulation
using Access Specifiers (Contd.)


                                         Visible to
                       Visible to own    objects of
    Access specifier
                       class members     same/other
                                         class

    public             Yes               Yes

    private            Yes               No

    protected          Yes               No

©NIIT                              OOPS/Lesson 3/Slide 9 of 21
Encapsulation and Abstraction


Just a Minute...
You have defined a Customer class as part of
developing the billing system software for Diaz
Telecommunications Inc. The class, which you’ve
defined, is as follows:
    class Customer
    {
      private:
      char mobileNo[11];
      char name[25];
      char dateOfBirth[11];
      void print()
        {
        //code to print the customer details
        }
©NIIT                           OOPS/Lesson 3/Slide 10 of 21
Encapsulation and Abstraction

Just a Minute...(Contd.)
     public:
     char billingAddress[51];
     char city[25];
     char phoneNo[11];
     float amountOutstanding;
     void get()
     {
     //code to accept the customer details
     }
   };
The Customer class definition shown above is
incorrect. Identify the errors in the Customer class and
write the correct definition for it.
©NIIT                               OOPS/Lesson 3/Slide 11 of 21
Encapsulation and Abstraction

Static Variables
 Retain their values even after the function to which
  they belong has been executed
 Example:
  class staticExample
   {
   int data;
   static int staticVar; // static
     //variable declared
   public:
   //Definition of member function
   };


©NIIT                                OOPS/Lesson 3/Slide 12 of 21
Encapsulation and Abstraction


Static Functions
 Can only access static variables
 Are accessed using class names




©NIIT                                OOPS/Lesson 3/Slide 13 of 21
Encapsulation and Abstraction


Problem Statement 3.P.1
You have defined a Dealer class as part of developing
the billing system software for Diaz Telecommunications
Inc. The class, which you’ve defined, is as follows:

    class Dealer
    {
      private:
      char mobileNo[11];
      char dealerName[25];
      char dealerAddress[51];
      char dealerCity[25];
      char phoneNo[11];

©NIIT                               OOPS/Lesson 3/Slide 14 of 21
Encapsulation and Abstraction

Problem Statement 3.P.1 (Contd.)
        public:
        static int CompanyID
        void showID()
        {
        coutThe dealer name
        isdealerName;
        coutThe company ID isCompanyID;
        }
        void get()
        {
        //code to accept the dealer details
        }
©NIIT                            OOPS/Lesson 3/Slide 15 of 21
Encapsulation and Abstraction


Problem Statement 3.P.1 (Contd.)
        void print()
        {
        //code to print the dealer details
        }
    };
    int CompanyID=6519;


The Dealer class definition shown above is incorrect.
Identify the errors in the Dealer class and write a
correct definition for it.


©NIIT                               OOPS/Lesson 3/Slide 16 of 21
Encapsulation and Abstraction


Friend Functions and Classes
 Friend functions
     Can be any non-member functions declared by a
      class
     May directly access the private member attributes
      and methods of the class objects
 Friend classes
     Can also be made a friend of another class




©NIIT                               OOPS/Lesson 3/Slide 17 of 21
Encapsulation and Abstraction


Summary
In this lesson, you learned that:
Abstraction denotes the essential characteristics of an
 object that distinguishes it from all other kinds of
 objects and thus provides crisply defined conceptual
 boundaries, relative to the perspective of the viewer
Encapsulation is the process of hiding all of the details
 of an object that do not contribute to its essential
 characteristics
An access specifier is used to determine whether any
 other class or function can access the member
 variables and functions of a particular class

©NIIT                                 OOPS/Lesson 3/Slide 18 of 21
Encapsulation and Abstraction


Summary (Contd.)
C++ supports three access specifiers:
    public
    private
    protected
The public access specifier allows a class to subject
 its member variables and member functions to other
 functions and objects
The private access specifier allows a class to hide
 its member variables and member functions from other
 class objects and functions

©NIIT                              OOPS/Lesson 3/Slide 19 of 21
Encapsulation and Abstraction


Summary (Contd.)
The protected access specifier allows a class to
 hide its member variables and member functions from
 other class objects and functions just like private
 access specifier - is used while implementing
 inheritance
The static variable retains its value even after the
 function to which it belongs has been executed
Static functions can only access static variables; non-
 static variables cannot be accessed using static
 functions



©NIIT                                OOPS/Lesson 3/Slide 20 of 21
Encapsulation and Abstraction


Summary (Contd.)
 Any non-member function may be declared a friend
  by a class, in which case the function may directly
  access the private member attributes and methods
  of the class objects
 A class can also be made a friend of another class




©NIIT                             OOPS/Lesson 3/Slide 21 of 21

More Related Content

PPT
9 abstract interface
PPTX
Lecture 18
PDF
Classes And Methods
PDF
What are Abstract Classes in Java | Edureka
PPTX
Abstract & Interface
PPTX
Fundamentals of oops in .Net
PDF
DTS s03e02 Handling the code
DOCX
Corejavainterviewquestions.doc
9 abstract interface
Lecture 18
Classes And Methods
What are Abstract Classes in Java | Edureka
Abstract & Interface
Fundamentals of oops in .Net
DTS s03e02 Handling the code
Corejavainterviewquestions.doc

What's hot (20)

PPT
Java Programming - Abstract Class and Interface
PPTX
Polymorphism and interface in vb.net
PDF
java-06inheritance
PPT
Jar chapter 5_part_i
DOCX
Introduction to object oriented programming concepts
PPT
inheritance
PPTX
Interfaces and abstract classes
PDF
Classes, objects, methods, constructors, this keyword in java
PDF
8 abstract classes and interfaces
PPTX
Oops in vb
PPTX
Object Oriended Programming with Java
PPT
testtesttest
PPT
Abstract_descrption
PPTX
Abstraction in java [abstract classes and Interfaces
PPTX
Solid Principles
PDF
Abstract classes and interfaces
PPT
PPS
Dacj 2-1 a
PPT
Abstract class
Java Programming - Abstract Class and Interface
Polymorphism and interface in vb.net
java-06inheritance
Jar chapter 5_part_i
Introduction to object oriented programming concepts
inheritance
Interfaces and abstract classes
Classes, objects, methods, constructors, this keyword in java
8 abstract classes and interfaces
Oops in vb
Object Oriended Programming with Java
testtesttest
Abstract_descrption
Abstraction in java [abstract classes and Interfaces
Solid Principles
Abstract classes and interfaces
Dacj 2-1 a
Abstract class
Ad

Similar to Aae oop xp_03 (20)

PDF
oopm 2.pdf
PPT
encapsulation and abstraction
PPTX
Presentation on class and object in Object Oriented programming.
PPTX
Encapsulation
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Abstraction file
PPTX
Oop objects_classes
PPTX
Module 4 Effect of Reuse on using Encapsulation.pptx
PPTX
[OOP - Lec 08] Encapsulation (Information Hiding)
PPTX
Lecture 2
PPTX
oop ppt.pptxfwefwefweqwedrqwerwerweewrewe
PPT
4 Classes & Objects
PDF
ClassesandObjects
PDF
ClassesandObjects
PDF
ClassesandObjects
oopm 2.pdf
encapsulation and abstraction
Presentation on class and object in Object Oriented programming.
Encapsulation
Abstraction file
Abstraction file
Abstraction file
Abstraction file
Abstraction file
Abstraction file
Abstraction file
Oop objects_classes
Module 4 Effect of Reuse on using Encapsulation.pptx
[OOP - Lec 08] Encapsulation (Information Hiding)
Lecture 2
oop ppt.pptxfwefwefweqwedrqwerwerweewrewe
4 Classes & Objects
ClassesandObjects
ClassesandObjects
ClassesandObjects
Ad

More from Niit Care (20)

PPS
Ajs 1 b
PPS
Ajs 4 b
PPS
Ajs 4 a
PPS
Ajs 4 c
PPS
Ajs 3 b
PPS
Ajs 3 a
PPS
Ajs 3 c
PPS
Ajs 2 b
PPS
Ajs 2 a
PPS
Ajs 2 c
PPS
Ajs 1 a
PPS
Ajs 1 c
PPS
Dacj 4 2-c
PPS
Dacj 4 2-b
PPS
Dacj 4 2-a
PPS
Dacj 4 1-c
PPS
Dacj 4 1-b
PPS
Dacj 4 1-a
PPS
Dacj 1-2 b
PPS
Dacj 1-3 c
Ajs 1 b
Ajs 4 b
Ajs 4 a
Ajs 4 c
Ajs 3 b
Ajs 3 a
Ajs 3 c
Ajs 2 b
Ajs 2 a
Ajs 2 c
Ajs 1 a
Ajs 1 c
Dacj 4 2-c
Dacj 4 2-b
Dacj 4 2-a
Dacj 4 1-c
Dacj 4 1-b
Dacj 4 1-a
Dacj 1-2 b
Dacj 1-3 c

Recently uploaded (20)

PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Encapsulation theory and applications.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
Cloud computing and distributed systems.
PPTX
Spectroscopy.pptx food analysis technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Electronic commerce courselecture one. Pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
Approach and Philosophy of On baking technology
Building Integrated photovoltaic BIPV_UPV.pdf
Unlocking AI with Model Context Protocol (MCP)
Spectral efficient network and resource selection model in 5G networks
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Encapsulation theory and applications.pdf
Empathic Computing: Creating Shared Understanding
Cloud computing and distributed systems.
Spectroscopy.pptx food analysis technology
MYSQL Presentation for SQL database connectivity
Electronic commerce courselecture one. Pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
MIND Revenue Release Quarter 2 2025 Press Release
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Approach and Philosophy of On baking technology

Aae oop xp_03

  • 1. Encapsulation and Abstraction Objectives In this lesson, you will learn to: Define Abstraction Define Encapsulation State the relationship between abstraction and encapsulation Implement encapsulation in C++ using the private and public access specifiers Use static variables and functions Use friend functions and classes ©NIIT OOPS/Lesson 3/Slide 1 of 21
  • 2. Encapsulation and Abstraction Abstraction Denotes the essential characteristics of an object that distinguishes it from all other kinds of objects Provides defined conceptual boundaries relative to the perspective of the viewer ©NIIT OOPS/Lesson 3/Slide 2 of 21
  • 3. Encapsulation and Abstraction Encapsulation Is a process of hiding all the details of an object that do not contribute to its essential characteristics Prevents access to non-essential details Is also called information hiding or data hiding ©NIIT OOPS/Lesson 3/Slide 3 of 21
  • 4. Encapsulation and Abstraction Relationship Between Abstraction and Encapsulation Complement each other Encapsulation assists abstraction by providing a means of suppressing the non-essential details ©NIIT OOPS/Lesson 3/Slide 4 of 21
  • 5. Encapsulation and Abstraction Implementing Abstraction and Encapsulation using Access Specifiers Public access specifier Allows a class to expose its member variables and member functions to other functions and objects Example: #includeiostream class Car { public: char color[21]; }; ©NIIT OOPS/Lesson 3/Slide 5 of 21
  • 6. Encapsulation and Abstraction Implementing Abstraction and Encapsulation using Access Specifiers (Contd.) int main() { Car ford; /* The . operator is used to access member data and functions */ cin ford.color; /*Since the color variable is public, it can be accessed outside the class definition.*/ return 0; } ©NIIT OOPS/Lesson 3/Slide 6 of 21
  • 7. Encapsulation and Abstraction Implementing Abstraction and Encapsulation using Access Specifiers (Contd.) Private access specifier Allows a class to hide its member variables and member functions from other class objects and functions Offers data hiding by protecting data from external alteration ©NIIT OOPS/Lesson 3/Slide 7 of 21
  • 8. Encapsulation and Abstraction Implementing Abstraction and Encapsulation using Access Specifiers (Contd.) Protected access specifier Allows a class to hide its member variables and member functions from other class objects and functions just like private access specifier - is used while implementing inheritance ©NIIT OOPS/Lesson 3/Slide 8 of 21
  • 9. Encapsulation and Abstraction Implementing Abstraction and Encapsulation using Access Specifiers (Contd.) Visible to Visible to own objects of Access specifier class members same/other class public Yes Yes private Yes No protected Yes No ©NIIT OOPS/Lesson 3/Slide 9 of 21
  • 10. Encapsulation and Abstraction Just a Minute... You have defined a Customer class as part of developing the billing system software for Diaz Telecommunications Inc. The class, which you’ve defined, is as follows: class Customer { private: char mobileNo[11]; char name[25]; char dateOfBirth[11]; void print() { //code to print the customer details } ©NIIT OOPS/Lesson 3/Slide 10 of 21
  • 11. Encapsulation and Abstraction Just a Minute...(Contd.) public: char billingAddress[51]; char city[25]; char phoneNo[11]; float amountOutstanding; void get() { //code to accept the customer details } }; The Customer class definition shown above is incorrect. Identify the errors in the Customer class and write the correct definition for it. ©NIIT OOPS/Lesson 3/Slide 11 of 21
  • 12. Encapsulation and Abstraction Static Variables Retain their values even after the function to which they belong has been executed Example: class staticExample { int data; static int staticVar; // static //variable declared public: //Definition of member function }; ©NIIT OOPS/Lesson 3/Slide 12 of 21
  • 13. Encapsulation and Abstraction Static Functions Can only access static variables Are accessed using class names ©NIIT OOPS/Lesson 3/Slide 13 of 21
  • 14. Encapsulation and Abstraction Problem Statement 3.P.1 You have defined a Dealer class as part of developing the billing system software for Diaz Telecommunications Inc. The class, which you’ve defined, is as follows: class Dealer { private: char mobileNo[11]; char dealerName[25]; char dealerAddress[51]; char dealerCity[25]; char phoneNo[11]; ©NIIT OOPS/Lesson 3/Slide 14 of 21
  • 15. Encapsulation and Abstraction Problem Statement 3.P.1 (Contd.) public: static int CompanyID void showID() { coutThe dealer name isdealerName; coutThe company ID isCompanyID; } void get() { //code to accept the dealer details } ©NIIT OOPS/Lesson 3/Slide 15 of 21
  • 16. Encapsulation and Abstraction Problem Statement 3.P.1 (Contd.) void print() { //code to print the dealer details } }; int CompanyID=6519; The Dealer class definition shown above is incorrect. Identify the errors in the Dealer class and write a correct definition for it. ©NIIT OOPS/Lesson 3/Slide 16 of 21
  • 17. Encapsulation and Abstraction Friend Functions and Classes Friend functions Can be any non-member functions declared by a class May directly access the private member attributes and methods of the class objects Friend classes Can also be made a friend of another class ©NIIT OOPS/Lesson 3/Slide 17 of 21
  • 18. Encapsulation and Abstraction Summary In this lesson, you learned that: Abstraction denotes the essential characteristics of an object that distinguishes it from all other kinds of objects and thus provides crisply defined conceptual boundaries, relative to the perspective of the viewer Encapsulation is the process of hiding all of the details of an object that do not contribute to its essential characteristics An access specifier is used to determine whether any other class or function can access the member variables and functions of a particular class ©NIIT OOPS/Lesson 3/Slide 18 of 21
  • 19. Encapsulation and Abstraction Summary (Contd.) C++ supports three access specifiers: public private protected The public access specifier allows a class to subject its member variables and member functions to other functions and objects The private access specifier allows a class to hide its member variables and member functions from other class objects and functions ©NIIT OOPS/Lesson 3/Slide 19 of 21
  • 20. Encapsulation and Abstraction Summary (Contd.) The protected access specifier allows a class to hide its member variables and member functions from other class objects and functions just like private access specifier - is used while implementing inheritance The static variable retains its value even after the function to which it belongs has been executed Static functions can only access static variables; non- static variables cannot be accessed using static functions ©NIIT OOPS/Lesson 3/Slide 20 of 21
  • 21. Encapsulation and Abstraction Summary (Contd.) Any non-member function may be declared a friend by a class, in which case the function may directly access the private member attributes and methods of the class objects A class can also be made a friend of another class ©NIIT OOPS/Lesson 3/Slide 21 of 21