Aae oop xp_09

1,090 views

Published on

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

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

No notes for slide

Aae oop xp_09

  1. 1. InheritanceObjectivesIn this lesson, you will learn to: Appreciate the following terms: Relationship Generalization Recognize the need for generalization Describe different types of relationship with examples from the real world: Inheritance Composition Utilization Instantiation©NIIT OOPS/Lesson 9/Slide 1 of 29
  2. 2. InheritanceObjectives (Contd.) Implement inheritance in C++ by deriving classes from the base class©NIIT OOPS/Lesson 9/Slide 2 of 29
  3. 3. InheritanceRelationships Are specified among classes based on the behavior of each class Exist between classes because of two reasons: To indicate some sort of sharing To indicate some kind of a connection Are of four types: Inheritance Composition Utilization Instantiation©NIIT OOPS/Lesson 9/Slide 3 of 29
  4. 4. InheritanceInheritance Relationship Between classes or objects means an object or a class inherits a set of attributes from another class Example: M AM M ALS DO G S C ATS HUM ANS L IO N S T IG E R S LEO PARDS©NIIT OOPS/Lesson 9/Slide 4 of 29
  5. 5. InheritanceInheritance Relationship (Contd.) Superclass Is a class from which another class inherits a set of attributes Subclass Is a class that inherits a set of attributes from another class©NIIT OOPS/Lesson 9/Slide 5 of 29
  6. 6. InheritanceInheritance Relationship (Contd.)The types of inheritance are: Single inheritance Is displayed when a class inherits attributes from a single class Multiple inheritance Is displayed when a class inherits attributes from two or more classes©NIIT OOPS/Lesson 9/Slide 6 of 29
  7. 7. InheritanceNeed for Generalization To create programs that are more extensible To provide source reusability©NIIT OOPS/Lesson 9/Slide 7 of 29
  8. 8. InheritanceGeneralization Is done by clubbing the structure and behavior that is common to all the classes Is represented by the superclass Is implemented by using the Abstract class, which Is not an existent entity Is a base from which other classes inherit attributes©NIIT OOPS/Lesson 9/Slide 8 of 29
  9. 9. InheritanceJust a Minute…Given the following objects, build a hierarchy, andgeneralize wherever possible: Mixer, VCR, Colortelevision, Washing machine, Stereo.©NIIT OOPS/Lesson 9/Slide 9 of 29
  10. 10. InheritanceComposition Relationship Occurs when one class is made up of another Example: Relationship between a car and its parts like engine, doors, steering wheel, gear box, seats, and so on©NIIT OOPS/Lesson 9/Slide 10 of 29
  11. 11. InheritanceJust a Minute…State the relationship between the following class pairs: 1. Television – Speaker 2. Mammal – Tiger 3. Garment – Shirt 4. Cup – Tea 5. Computer – Microprocessor©NIIT OOPS/Lesson 9/Slide 11 of 29
  12. 12. InheritanceUtilization Relationship Exists between two or more classes, which make use of other classes Example: Relationship between a driver and a car©NIIT OOPS/Lesson 9/Slide 12 of 29
  13. 13. InheritanceJust a Minute…Identify the classes and the utilization relationships of adepartmental store from the following details:There are several counters, each one manned by asingle salesperson selling a specific product. Thecustomer approaches any counter, depending on whichproduct he or she wishes to purchase. The salespersonhands him or her the product and accepts payment.©NIIT OOPS/Lesson 9/Slide 13 of 29
  14. 14. InheritanceInstantiation Relationship Is a relationship between a class and an instance of that class Example: Relationship between a book and ‘Gone with the Wind’©NIIT OOPS/Lesson 9/Slide 14 of 29
  15. 15. InheritanceUses of Inheritance Reduces redundancy in code Enables easy maintenance of code Extends the functionality of an existing class by adding functions to the subclass©NIIT OOPS/Lesson 9/Slide 15 of 29
  16. 16. InheritanceSequence of Invoking Constructors andDestructors Constructors are called in the order of Base - to - Derived Destructors are called in the order of Derived - to - Base©NIIT OOPS/Lesson 9/Slide 16 of 29
  17. 17. InheritanceBase Class Initialization Is required before the member object initialization of the derived class is doneclass Line : public Figure{ Point p2; public: Line(int x1 = 0,int y1 = 0,int x2 = 0,int y2 = 0): p2(x2, y2), Figure(x1, y1) {} //Member Initialization List};©NIIT OOPS/Lesson 9/Slide 17 of 29
  18. 18. InheritanceAccess Specifiers of Derivation Falls under the following category: public protected private©NIIT OOPS/Lesson 9/Slide 18 of 29
  19. 19. InheritancePublic Access Specifier Defines that all the: private members of a base class remain private in the object protected members remain protected the public members remain public©NIIT OOPS/Lesson 9/Slide 19 of 29
  20. 20. InheritanceProtected Access Specifier Defines that all the: the private members of a base class remain private in the object the protected members remain protected but all the public members of the base class become protected©NIIT OOPS/Lesson 9/Slide 20 of 29
  21. 21. InheritancePrivate Specifier Defines that all the: private members of a base class remain private in the object public and protected members in the base class become privateDepicts a composition relationship between the base and the derived classes©NIIT OOPS/Lesson 9/Slide 21 of 29
  22. 22. InheritanceImplementing Inheritance in C++ by DerivingClasses From the Base Class Syntax: class base_class { … }; class derived_class : access-specifier base_class { ... };©NIIT OOPS/Lesson 9/Slide 22 of 29
  23. 23. InheritanceImplementing Inheritance in C++ by DerivingClasses From the Base Class Sequence of invoking constructors and destructors Constructors are called in the order of Base – to – Derived Destructors are called in the order of Derived – to – Base Access specifiers of derivation The public access specifier The protected access specifier The private access specifier©NIIT OOPS/Lesson 9/Slide 23 of 29
  24. 24. InheritanceProblem Statement 7.D.1Furniture and Fittings Company (FFC) deals withcustom-made furniture. Customers provide theirspecifications for the furniture they want. To cope withthe volume of orders placed, FFC had decided tocomputerize their order-processing system a few monthsago. The code should have representations for abookshelf and a chair. You have to develop the hierarchyand the code.©NIIT OOPS/Lesson 9/Slide 24 of 29
  25. 25. InheritanceProblem Statement 7.P.1Philip Anderson of Mega Technologies has written thefollowing code to implement a graphics package – todraw simple graphic images. However, Philip has beenmoved on to another project and you have been askedto complete the code. Make necessary changes andtest the code.©NIIT OOPS/Lesson 9/Slide 25 of 29
  26. 26. InheritanceSummaryIn this lesson, you learned that: Relationships exist between two classes mainly for two reasons: A class-class relationship, which is a relationship between two classes, indicating some sort of sharing A class-class relationship, indicating some kind of a connection©NIIT OOPS/Lesson 9/Slide 26 of 29
  27. 27. InheritanceSummary (Contd.) The four kinds of relationships that can be identified are: Inheritance Composition Utilization Instantiation Generalization means that multiple classes inherit from the same superclass When a class inherits a set of attributes from another class, the class that inherits is called the subclass and the class from which it inherits is called the superclass©NIIT OOPS/Lesson 9/Slide 27 of 29
  28. 28. InheritanceSummary (Contd.) An abstract superclass is a conceptual class that does not exist in the real world but acts as a base from which other classes inherit properties When a class inherits attributes from two or more classes, it is said to be showing multiple inheritance Composition relationship occurs when one class is made up of another Utilization relationships exist between two or more classes, which make use of other classes Instantiation relationship, as the name suggests, is a relationship between a class and an instance of that class©NIIT OOPS/Lesson 9/Slide 28 of 29
  29. 29. InheritanceSummary (Contd.) In a class hierarchy, constructors are called in the order of Base – to – Derived and destructors are called in the order of Derived – to – Base©NIIT OOPS/Lesson 9/Slide 29 of 29

×