Inheritance

Objectives
In 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
Inheritance


Objectives (Contd.)
 Implement inheritance in C++ by deriving classes from
  the base class




©NIIT                               OOPS/Lesson 9/Slide 2 of 29
Inheritance


Relationships
 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
Inheritance


Inheritance 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
Inheritance


Inheritance 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
Inheritance


Inheritance 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
Inheritance


Need for Generalization
 To create programs that are more extensible
 To provide source reusability




©NIIT                              OOPS/Lesson 9/Slide 7 of 29
Inheritance


Generalization
 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
Inheritance


Just a Minute…
Given the following objects, build a hierarchy, and
generalize wherever possible: Mixer, VCR, Color
television, Washing machine, Stereo.




©NIIT                                 OOPS/Lesson 9/Slide 9 of 29
Inheritance


Composition 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
Inheritance


Just 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
Inheritance


Utilization 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
Inheritance


Just a Minute…
Identify the classes and the utilization relationships of a
departmental store from the following details:
There are several counters, each one manned by a
single salesperson selling a specific product. The
customer approaches any counter, depending on which
product he or she wishes to purchase. The salesperson
hands him or her the product and accepts payment.




©NIIT                                  OOPS/Lesson 9/Slide 13 of 29
Inheritance


Instantiation 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
Inheritance


Uses 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
Inheritance


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




©NIIT                                 OOPS/Lesson 9/Slide 16 of 29
Inheritance


Base Class Initialization
 Is required before the member object initialization of
  the derived class is done
class 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
Inheritance


Access Specifiers of Derivation
 Falls under the following category:
     public
     protected
     private




©NIIT                               OOPS/Lesson 9/Slide 18 of 29
Inheritance


Public 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
Inheritance


Protected 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
Inheritance


Private 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 private
Depicts a composition relationship between the base
 and the derived classes




©NIIT                             OOPS/Lesson 9/Slide 21 of 29
Inheritance

Implementing Inheritance in C++ by Deriving
Classes From the Base Class
 Syntax:
  class base_class
  {
  …
  };
  class derived_class : access-specifier
                    base_class
  {
  ...
  };
©NIIT                              OOPS/Lesson 9/Slide 22 of 29
Inheritance


Implementing Inheritance in C++ by Deriving
Classes 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
Inheritance


Problem Statement 7.D.1
Furniture and Fittings Company (FFC) deals with
custom-made furniture. Customers provide their
specifications for the furniture they want. To cope with
the volume of orders placed, FFC had decided to
computerize their order-processing system a few months
ago. The code should have representations for a
bookshelf and a chair. You have to develop the hierarchy
and the code.




©NIIT                              OOPS/Lesson 9/Slide 24 of 29
Inheritance


Problem Statement 7.P.1
Philip Anderson of Mega Technologies has written the
following code to implement a graphics package – to
draw simple graphic images. However, Philip has been
moved on to another project and you have been asked
to complete the code. Make necessary changes and
test the code.




©NIIT                            OOPS/Lesson 9/Slide 25 of 29
Inheritance


Summary
In 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
Inheritance


Summary (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
Inheritance


Summary (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
Inheritance


Summary (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

More Related Content

PPS
Aae oop xp_11
PPS
Dacj 1-1 c
PPS
Aae oop xp_03
PPT
Generalizations concepts
PPT
Generalization and example
PPT
Generalization
PDF
What is Generalization
Aae oop xp_11
Dacj 1-1 c
Aae oop xp_03
Generalizations concepts
Generalization and example
Generalization
What is Generalization

Similar to Aae oop xp_09 (20)

PDF
OOP Inheritance
PDF
Constructor & destructor
PDF
PPTX
Inheritance
PPTX
Introduction to inheritance and different types of inheritance
PPTX
inheritance in OOPM
PPT
Java inheritance
PDF
Segundo avance
PPS
Dacj 2-1 a
PPTX
Object Oriented Programming using C++: Ch09 Inheritance.pptx
PPSX
PPTX
Lecture 3
PPTX
Inheritance
PPTX
Inheritance
PPTX
Inheritance
PPT
Inheritance in C++
PPT
10.Inheritance.ppt for oops programinggg
PPT
Unit 3 Java
PDF
C++ L10-Inheritance
PPTX
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
OOP Inheritance
Constructor & destructor
Inheritance
Introduction to inheritance and different types of inheritance
inheritance in OOPM
Java inheritance
Segundo avance
Dacj 2-1 a
Object Oriented Programming using C++: Ch09 Inheritance.pptx
Lecture 3
Inheritance
Inheritance
Inheritance
Inheritance in C++
10.Inheritance.ppt for oops programinggg
Unit 3 Java
C++ L10-Inheritance
INHERITANCE, POINTERS, VIRTUAL FUNCTIONS, POLYMORPHISM.pptx
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
Ad

Recently uploaded (20)

PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PDF
Unlock new opportunities with location data.pdf
PDF
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
PDF
Enhancing emotion recognition model for a student engagement use case through...
PPTX
Modernising the Digital Integration Hub
PDF
sustainability-14-14877-v2.pddhzftheheeeee
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
Web Crawler for Trend Tracking Gen Z Insights.pptx
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PPTX
observCloud-Native Containerability and monitoring.pptx
PDF
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
PPTX
Tartificialntelligence_presentation.pptx
PDF
CloudStack 4.21: First Look Webinar slides
PPT
What is a Computer? Input Devices /output devices
PDF
Taming the Chaos: How to Turn Unstructured Data into Decisions
PDF
Architecture types and enterprise applications.pdf
PPTX
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
PPTX
Chapter 5: Probability Theory and Statistics
DOCX
search engine optimization ppt fir known well about this
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
A contest of sentiment analysis: k-nearest neighbor versus neural network
Unlock new opportunities with location data.pdf
TrustArc Webinar - Click, Consent, Trust: Winning the Privacy Game
Enhancing emotion recognition model for a student engagement use case through...
Modernising the Digital Integration Hub
sustainability-14-14877-v2.pddhzftheheeeee
Zenith AI: Advanced Artificial Intelligence
Web Crawler for Trend Tracking Gen Z Insights.pptx
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
observCloud-Native Containerability and monitoring.pptx
A Late Bloomer's Guide to GenAI: Ethics, Bias, and Effective Prompting - Boha...
Tartificialntelligence_presentation.pptx
CloudStack 4.21: First Look Webinar slides
What is a Computer? Input Devices /output devices
Taming the Chaos: How to Turn Unstructured Data into Decisions
Architecture types and enterprise applications.pdf
MicrosoftCybserSecurityReferenceArchitecture-April-2025.pptx
Chapter 5: Probability Theory and Statistics
search engine optimization ppt fir known well about this
Group 1 Presentation -Planning and Decision Making .pptx

Aae oop xp_09

  • 1. Inheritance Objectives In 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. Inheritance Objectives (Contd.) Implement inheritance in C++ by deriving classes from the base class ©NIIT OOPS/Lesson 9/Slide 2 of 29
  • 3. Inheritance Relationships 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. Inheritance Inheritance 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. Inheritance Inheritance 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. Inheritance Inheritance 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. Inheritance Need for Generalization To create programs that are more extensible To provide source reusability ©NIIT OOPS/Lesson 9/Slide 7 of 29
  • 8. Inheritance Generalization 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. Inheritance Just a Minute… Given the following objects, build a hierarchy, and generalize wherever possible: Mixer, VCR, Color television, Washing machine, Stereo. ©NIIT OOPS/Lesson 9/Slide 9 of 29
  • 10. Inheritance Composition 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. Inheritance Just 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. Inheritance Utilization 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. Inheritance Just a Minute… Identify the classes and the utilization relationships of a departmental store from the following details: There are several counters, each one manned by a single salesperson selling a specific product. The customer approaches any counter, depending on which product he or she wishes to purchase. The salesperson hands him or her the product and accepts payment. ©NIIT OOPS/Lesson 9/Slide 13 of 29
  • 14. Inheritance Instantiation 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. Inheritance Uses 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. Inheritance 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 ©NIIT OOPS/Lesson 9/Slide 16 of 29
  • 17. Inheritance Base Class Initialization Is required before the member object initialization of the derived class is done class 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. Inheritance Access Specifiers of Derivation Falls under the following category: public protected private ©NIIT OOPS/Lesson 9/Slide 18 of 29
  • 19. Inheritance Public 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. Inheritance Protected 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. Inheritance Private 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 private Depicts a composition relationship between the base and the derived classes ©NIIT OOPS/Lesson 9/Slide 21 of 29
  • 22. Inheritance Implementing Inheritance in C++ by Deriving Classes From the Base Class Syntax: class base_class { … }; class derived_class : access-specifier base_class { ... }; ©NIIT OOPS/Lesson 9/Slide 22 of 29
  • 23. Inheritance Implementing Inheritance in C++ by Deriving Classes 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. Inheritance Problem Statement 7.D.1 Furniture and Fittings Company (FFC) deals with custom-made furniture. Customers provide their specifications for the furniture they want. To cope with the volume of orders placed, FFC had decided to computerize their order-processing system a few months ago. The code should have representations for a bookshelf and a chair. You have to develop the hierarchy and the code. ©NIIT OOPS/Lesson 9/Slide 24 of 29
  • 25. Inheritance Problem Statement 7.P.1 Philip Anderson of Mega Technologies has written the following code to implement a graphics package – to draw simple graphic images. However, Philip has been moved on to another project and you have been asked to complete the code. Make necessary changes and test the code. ©NIIT OOPS/Lesson 9/Slide 25 of 29
  • 26. Inheritance Summary In 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. Inheritance Summary (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. Inheritance Summary (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. Inheritance Summary (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