Encapsulation and AbstractionObjectivesIn this lesson, you will learn to: Define Abstraction Define Encapsulation State th...
Encapsulation and AbstractionAbstraction Denotes the essential characteristics of an object that  distinguishes it from al...
Encapsulation and AbstractionEncapsulation Is a process of hiding all the details of an object that  do not contribute to ...
Encapsulation and AbstractionRelationship Between Abstraction andEncapsulationComplement each otherEncapsulation assists a...
Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing Access Specifiers Public access specifier    ...
Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing Access Specifiers (Contd.)        int main() ...
Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing Access Specifiers (Contd.) Private access spe...
Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing Access Specifiers (Contd.) Protected access s...
Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing Access Specifiers (Contd.)                   ...
Encapsulation and AbstractionJust a Minute...You have defined a Customer class as part ofdeveloping the billing system sof...
Encapsulation and AbstractionJust a Minute...(Contd.)     public:     char billingAddress[51];     char city[25];     char...
Encapsulation and AbstractionStatic Variables Retain their values even after the function to which  they belong has been e...
Encapsulation and AbstractionStatic Functions Can only access static variables Are accessed using class names©NIIT        ...
Encapsulation and AbstractionProblem Statement 3.P.1You have defined a Dealer class as part of developingthe billing syste...
Encapsulation and AbstractionProblem Statement 3.P.1 (Contd.)        public:        static int CompanyID        void showI...
Encapsulation and AbstractionProblem Statement 3.P.1 (Contd.)        void print()        {        //code to print the deal...
Encapsulation and AbstractionFriend Functions and Classes Friend functions     Can be any non-member functions declared by...
Encapsulation and AbstractionSummaryIn this lesson, you learned that:Abstraction denotes the essential characteristics of ...
Encapsulation and AbstractionSummary (Contd.)C++ supports three access specifiers:    public    private    protectedThe pu...
Encapsulation and AbstractionSummary (Contd.)The protected access specifier allows a class to hide its member variables an...
Encapsulation and AbstractionSummary (Contd.) Any non-member function may be declared a friend  by a class, in which case ...
Upcoming SlideShare
Loading in …5
×

Aae oop xp_03

1,050 views

Published on

Published in: Technology, Education
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,050
On SlideShare
0
From Embeds
0
Number of Embeds
102
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Aae oop xp_03

  1. 1. Encapsulation and AbstractionObjectivesIn 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. 2. Encapsulation and AbstractionAbstraction 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. 3. Encapsulation and AbstractionEncapsulation 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. 4. Encapsulation and AbstractionRelationship Between Abstraction andEncapsulationComplement each otherEncapsulation assists abstraction by providing a means of suppressing the non-essential details©NIIT OOPS/Lesson 3/Slide 4 of 21
  5. 5. Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing 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. 6. Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing 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. 7. Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing 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. 8. Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing 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. 9. Encapsulation and AbstractionImplementing Abstraction and Encapsulationusing 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. 10. Encapsulation and AbstractionJust a Minute...You have defined a Customer class as part ofdeveloping the billing system software for DiazTelecommunications Inc. The class, which you’vedefined, 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. 11. Encapsulation and AbstractionJust 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 isincorrect. Identify the errors in the Customer class andwrite the correct definition for it.©NIIT OOPS/Lesson 3/Slide 11 of 21
  12. 12. Encapsulation and AbstractionStatic 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. 13. Encapsulation and AbstractionStatic Functions Can only access static variables Are accessed using class names©NIIT OOPS/Lesson 3/Slide 13 of 21
  14. 14. Encapsulation and AbstractionProblem Statement 3.P.1You have defined a Dealer class as part of developingthe billing system software for Diaz TelecommunicationsInc. 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. 15. Encapsulation and AbstractionProblem 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. 16. Encapsulation and AbstractionProblem 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 acorrect definition for it.©NIIT OOPS/Lesson 3/Slide 16 of 21
  17. 17. Encapsulation and AbstractionFriend 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. 18. Encapsulation and AbstractionSummaryIn 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 viewerEncapsulation is the process of hiding all of the details of an object that do not contribute to its essential characteristicsAn 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. 19. Encapsulation and AbstractionSummary (Contd.)C++ supports three access specifiers: public private protectedThe public access specifier allows a class to subject its member variables and member functions to other functions and objectsThe 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. 20. Encapsulation and AbstractionSummary (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 inheritanceThe static variable retains its value even after the function to which it belongs has been executedStatic functions can only access static variables; non- static variables cannot be accessed using static functions©NIIT OOPS/Lesson 3/Slide 20 of 21
  21. 21. Encapsulation and AbstractionSummary (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

×