SlideShare una empresa de Scribd logo
1 de 21
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

Más contenido relacionado

La actualidad más candente

Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceOum Saokosal
 
Polymorphism and interface in vb.net
Polymorphism and interface in vb.netPolymorphism and interface in vb.net
Polymorphism and interface in vb.netKarthigaGunasekaran1
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritanceArjun Shanka
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classesAKANSH SINGHAL
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in javaTharuniDiddekunta
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces Tuan Ngo
 
Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with JavaJakir Hossain
 
Abstract_descrption
Abstract_descrptionAbstract_descrption
Abstract_descrptionMahi Mca
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAhmed Nobi
 
Solid Principles
Solid PrinciplesSolid Principles
Solid PrinciplesHitheshh
 

La actualidad más candente (20)

Java Programming - Abstract Class and Interface
Java Programming - Abstract Class and InterfaceJava Programming - Abstract Class and Interface
Java Programming - Abstract Class and Interface
 
Polymorphism and interface in vb.net
Polymorphism and interface in vb.netPolymorphism and interface in vb.net
Polymorphism and interface in vb.net
 
java-06inheritance
java-06inheritancejava-06inheritance
java-06inheritance
 
7494604
74946047494604
7494604
 
Jar chapter 5_part_i
Jar chapter 5_part_iJar chapter 5_part_i
Jar chapter 5_part_i
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
inheritance
inheritanceinheritance
inheritance
 
Interfaces and abstract classes
Interfaces and abstract classesInterfaces and abstract classes
Interfaces and abstract classes
 
Classes, objects, methods, constructors, this keyword in java
Classes, objects, methods, constructors, this keyword  in javaClasses, objects, methods, constructors, this keyword  in java
Classes, objects, methods, constructors, this keyword in java
 
8 abstract classes and interfaces
8   abstract classes and interfaces 8   abstract classes and interfaces
8 abstract classes and interfaces
 
Oops in vb
Oops in vbOops in vb
Oops in vb
 
Object Oriended Programming with Java
Object Oriended Programming with JavaObject Oriended Programming with Java
Object Oriended Programming with Java
 
testtesttest
testtesttesttesttesttest
testtesttest
 
Abstract_descrption
Abstract_descrptionAbstract_descrption
Abstract_descrption
 
Abstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and InterfacesAbstraction in java [abstract classes and Interfaces
Abstraction in java [abstract classes and Interfaces
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Abstract classes and interfaces
Abstract classes and interfacesAbstract classes and interfaces
Abstract classes and interfaces
 
7 ooad
7 ooad7 ooad
7 ooad
 
Dacj 2-1 a
Dacj 2-1 aDacj 2-1 a
Dacj 2-1 a
 
Abstract class
Abstract classAbstract class
Abstract class
 

Similar a Aae oop xp_03

Similar a Aae oop xp_03 (20)

C++ Notes
C++ NotesC++ Notes
C++ Notes
 
lecture3.pptx
lecture3.pptxlecture3.pptx
lecture3.pptx
 
Vb net xp_03
Vb net xp_03Vb net xp_03
Vb net xp_03
 
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
 
Aae oop xp_07
Aae oop xp_07Aae oop xp_07
Aae oop xp_07
 
Aae oop xp_01
Aae oop xp_01Aae oop xp_01
Aae oop xp_01
 
Mca 504 dotnet_unit3
Mca 504 dotnet_unit3Mca 504 dotnet_unit3
Mca 504 dotnet_unit3
 
7 class objects
7 class objects7 class objects
7 class objects
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principles
 
Opp concept in c++
Opp concept in c++Opp concept in c++
Opp concept in c++
 
Lab 4 (1).pdf
Lab 4 (1).pdfLab 4 (1).pdf
Lab 4 (1).pdf
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
Aae oop xp_09
Aae oop xp_09Aae oop xp_09
Aae oop xp_09
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 

Más de Niit Care (20)

Ajs 1 b
Ajs 1 bAjs 1 b
Ajs 1 b
 
Ajs 4 b
Ajs 4 bAjs 4 b
Ajs 4 b
 
Ajs 4 a
Ajs 4 aAjs 4 a
Ajs 4 a
 
Ajs 4 c
Ajs 4 cAjs 4 c
Ajs 4 c
 
Ajs 3 b
Ajs 3 bAjs 3 b
Ajs 3 b
 
Ajs 3 a
Ajs 3 aAjs 3 a
Ajs 3 a
 
Ajs 3 c
Ajs 3 cAjs 3 c
Ajs 3 c
 
Ajs 2 b
Ajs 2 bAjs 2 b
Ajs 2 b
 
Ajs 2 a
Ajs 2 aAjs 2 a
Ajs 2 a
 
Ajs 2 c
Ajs 2 cAjs 2 c
Ajs 2 c
 
Ajs 1 a
Ajs 1 aAjs 1 a
Ajs 1 a
 
Ajs 1 c
Ajs 1 cAjs 1 c
Ajs 1 c
 
Dacj 4 2-c
Dacj 4 2-cDacj 4 2-c
Dacj 4 2-c
 
Dacj 4 2-b
Dacj 4 2-bDacj 4 2-b
Dacj 4 2-b
 
Dacj 4 2-a
Dacj 4 2-aDacj 4 2-a
Dacj 4 2-a
 
Dacj 4 1-c
Dacj 4 1-cDacj 4 1-c
Dacj 4 1-c
 
Dacj 4 1-b
Dacj 4 1-bDacj 4 1-b
Dacj 4 1-b
 
Dacj 4 1-a
Dacj 4 1-aDacj 4 1-a
Dacj 4 1-a
 
Dacj 1-2 b
Dacj 1-2 bDacj 1-2 b
Dacj 1-2 b
 
Dacj 1-3 c
Dacj 1-3 cDacj 1-3 c
Dacj 1-3 c
 

Último

Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationIES VE
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.YounusS2
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024SkyPlanner
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesDavid Newbury
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Adtran
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum ComputingGDSC PJATK
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1DianaGray10
 
Things you didn't know you can use in your Salesforce
Things you didn't know you can use in your SalesforceThings you didn't know you can use in your Salesforce
Things you didn't know you can use in your SalesforceMartin Humpolec
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostMatt Ray
 
Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?SANGHEE SHIN
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7DianaGray10
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintMahmoud Rabie
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesMd Hossain Ali
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfDianaGray10
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...DianaGray10
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...Aggregage
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxYounusS2
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncObject Automation
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URLRuncy Oommen
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAshyamraj55
 

Último (20)

Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve DecarbonizationUsing IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
Using IESVE for Loads, Sizing and Heat Pump Modeling to Achieve Decarbonization
 
Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.Basic Building Blocks of Internet of Things.
Basic Building Blocks of Internet of Things.
 
Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024Salesforce Miami User Group Event - 1st Quarter 2024
Salesforce Miami User Group Event - 1st Quarter 2024
 
Linked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond OntologiesLinked Data in Production: Moving Beyond Ontologies
Linked Data in Production: Moving Beyond Ontologies
 
Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™Meet the new FSP 3000 M-Flex800™
Meet the new FSP 3000 M-Flex800™
 
Introduction to Quantum Computing
Introduction to Quantum ComputingIntroduction to Quantum Computing
Introduction to Quantum Computing
 
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1UiPath Platform: The Backend Engine Powering Your Automation - Session 1
UiPath Platform: The Backend Engine Powering Your Automation - Session 1
 
Things you didn't know you can use in your Salesforce
Things you didn't know you can use in your SalesforceThings you didn't know you can use in your Salesforce
Things you didn't know you can use in your Salesforce
 
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCostKubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
KubeConEU24-Monitoring Kubernetes and Cloud Spend with OpenCost
 
Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?Do we need a new standard for visualizing the invisible?
Do we need a new standard for visualizing the invisible?
 
UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7UiPath Studio Web workshop series - Day 7
UiPath Studio Web workshop series - Day 7
 
Empowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership BlueprintEmpowering Africa's Next Generation: The AI Leadership Blueprint
Empowering Africa's Next Generation: The AI Leadership Blueprint
 
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just MinutesAI Fame Rush Review – Virtual Influencer Creation In Just Minutes
AI Fame Rush Review – Virtual Influencer Creation In Just Minutes
 
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdfUiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
UiPath Solutions Management Preview - Northern CA Chapter - March 22.pdf
 
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
Connector Corner: Extending LLM automation use cases with UiPath GenAI connec...
 
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
The Data Metaverse: Unpacking the Roles, Use Cases, and Tech Trends in Data a...
 
Babel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptxBabel Compiler - Transforming JavaScript for All Browsers.pptx
Babel Compiler - Transforming JavaScript for All Browsers.pptx
 
GenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation IncGenAI and AI GCC State of AI_Object Automation Inc
GenAI and AI GCC State of AI_Object Automation Inc
 
Designing A Time bound resource download URL
Designing A Time bound resource download URLDesigning A Time bound resource download URL
Designing A Time bound resource download URL
 
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPAAnypoint Code Builder , Google Pub sub connector and MuleSoft RPA
Anypoint Code Builder , Google Pub sub connector and MuleSoft RPA
 

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