SlideShare a Scribd company logo
1 of 29
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

Similar to Aae oop xp_09

Aae oop xp_01
Aae oop xp_01Aae oop xp_01
Aae oop xp_01Niit Care
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdfstudy material
 
9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot missMark Papis
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09Ayesha ch
 
Vb net xp_03
Vb net xp_03Vb net xp_03
Vb net xp_03Niit Care
 
Reuse Contracts
Reuse ContractsReuse Contracts
Reuse ContractsESUG
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxkristinatemen
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Gajendra Singh Thakur
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructorSwarup Boro
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.pptSeethaDinesh
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
Object oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsObject oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsBharat Kalia
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principlesmaznabili
 

Similar to Aae oop xp_09 (20)

Aae oop xp_01
Aae oop xp_01Aae oop xp_01
Aae oop xp_01
 
chapter-10-inheritance.pdf
chapter-10-inheritance.pdfchapter-10-inheritance.pdf
chapter-10-inheritance.pdf
 
9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss9 crucial Java Design Principles you cannot miss
9 crucial Java Design Principles you cannot miss
 
Java htp6e 09
Java htp6e 09Java htp6e 09
Java htp6e 09
 
Inheritance
InheritanceInheritance
Inheritance
 
Vb net xp_03
Vb net xp_03Vb net xp_03
Vb net xp_03
 
Inheritance
InheritanceInheritance
Inheritance
 
Reuse Contracts
Reuse ContractsReuse Contracts
Reuse Contracts
 
ITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptxITTutor Advanced Java (1).pptx
ITTutor Advanced Java (1).pptx
 
Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)Inheritance In C++ (Object Oriented Programming)
Inheritance In C++ (Object Oriented Programming)
 
Inheritance
InheritanceInheritance
Inheritance
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Inheritance in java.ppt
Inheritance in java.pptInheritance in java.ppt
Inheritance in java.ppt
 
Constructor & destructor
Constructor & destructorConstructor & destructor
Constructor & destructor
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Object oriented programming Fundamental Concepts
Object oriented programming Fundamental ConceptsObject oriented programming Fundamental Concepts
Object oriented programming Fundamental Concepts
 
Ppt of c++ vs c#
Ppt of c++ vs c#Ppt of c++ vs c#
Ppt of c++ vs c#
 
Four Pillers Of OOPS
Four Pillers Of OOPSFour Pillers Of OOPS
Four Pillers Of OOPS
 
20 Object-oriented programming principles
20 Object-oriented programming principles20 Object-oriented programming principles
20 Object-oriented programming principles
 
JavaHTP6e_10.ppt
JavaHTP6e_10.pptJavaHTP6e_10.ppt
JavaHTP6e_10.ppt
 

More from 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
 

Recently uploaded

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

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