Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Aae oop xp_11

515 views

Published on

Published in: Technology, Education
  • Be the first to comment

Aae oop xp_11

  1. 1. Multiple InheritanceObjectivesIn this lesson, you will learn to: Derive a class from multiple base classes Identify the ambiguities that arise in multiple inheritance Identify the order in which constructors and destructors are invoked©NIIT OOPS/Lesson 11/Slide 1 of 24
  2. 2. Multiple InheritanceMultiple Inheritance Is the phenomenon where a class may inherit from two or more classes©NIIT OOPS/Lesson 11/Slide 2 of 24
  3. 3. Multiple InheritanceDeriving a Class From Multiple Base Classes Syntax: class derived : public base1, public base2 { //Body of class };©NIIT OOPS/Lesson 11/Slide 3 of 24
  4. 4. Multiple InheritanceDeriving a Class From Multiple Base Classes(Contd.)Example: #includeiostream class base1 { protected: int iVar1; // To make it available //to the derived class public: void show_1() { cout iVar1 endl; } };©NIIT OOPS/Lesson 11/Slide 4 of 24
  5. 5. Multiple InheritanceDeriving a Class From Multiple Base Classes(Contd.) class base2 { protected: int iVar2; // To make it available //to the derived class public: void show_2() { cout iVar2 endl; } }; //Inherit multiple base classes©NIIT OOPS/Lesson 11/Slide 5 of 24
  6. 6. Multiple InheritanceDeriving a Class From Multiple Base Classes(Contd.)class derived : public base1,public base2 { public: void set(int iTx, int iTy) { iVar1=iTx;iVar2=iTy; } }; int main() { derived Dvar;Dvar.set(10,20); Dvar.show_1();Dvar.show_2(); return 0; }©NIIT OOPS/Lesson 11/Slide 6 of 24
  7. 7. Multiple InheritanceAmbiguities in Multiple Inheritance Can arise when two base classes contain a function of the same name Can arise when the derived class has multiple copies of the same base class©NIIT OOPS/Lesson 11/Slide 7 of 24
  8. 8. Multiple InheritanceAmbiguities in Multiple Inheritance (Contd.) Can arise when two base classes contain a function of the same name Example: #includeiostream class base1 { public: void disp() { cout Base1 endl; } };©NIIT OOPS/Lesson 11/Slide 8 of 24
  9. 9. Multiple InheritanceAmbiguities in Multiple Inheritance (Contd.) class base2 { public: void disp() { cout Base2endl; } }; class derived : public base1,public base2 { //Empty class };©NIIT OOPS/Lesson 11/Slide 9 of 24
  10. 10. Multiple InheritanceAmbiguities in Multiple Inheritance (Contd.) int main() { derived Dvar; Dvar.disp(); //Ambiguous function call return 0; }©NIIT OOPS/Lesson 11/Slide 10 of 24
  11. 11. Multiple InheritanceAmbiguities in Multiple Inheritance (Contd.) Can be resolved in two ways: By using the scope resolution operator By overriding the function in the derived class©NIIT OOPS/Lesson 11/Slide 11 of 24
  12. 12. Multiple InheritanceAmbiguities in Multiple Inheritance (Contd.) Can arise when the derived class has multiple copies of the same base classExample: Class A Class B Class C Class D©NIIT OOPS/Lesson 11/Slide 12 of 24
  13. 13. Multiple InheritanceAmbiguities in Multiple Inheritance (Contd.) Can be resolved in two ways: By using the scope resolution operator By using a virtual base class©NIIT OOPS/Lesson 11/Slide 13 of 24
  14. 14. Multiple InheritanceAmbiguities in Multiple Inheritance (Contd.) Virtual Base Class Allows to have only one copy of the base class members in memory when a class inherits same properties or methods more than once through multiple paths Is implemented by using the virtual qualifier when inheriting from the base class©NIIT OOPS/Lesson 11/Slide 14 of 24
  15. 15. Multiple InheritanceInvocation of Constructors Is done in the following order: 1. Virtual base class constructors – in the order of inheritance 2. Non-virtual base class constructors – in the order of inheritance 3. Member objects constructors – in the order of declaration 4. Derived class constructors©NIIT OOPS/Lesson 11/Slide 15 of 24
  16. 16. Multiple InheritanceInvocation of Destructors Is done in the reverse order of the invocation of constructors©NIIT OOPS/Lesson 11/Slide 16 of 24
  17. 17. Multiple InheritanceProblem Statement 9.P.1 Consider the given partial class definitions with some errors:#include iostreamclass application{protected: char appcode[4]; int storagespace;public: void getapp(); void showapp()};©NIIT OOPS/Lesson 11/Slide 17 of 24
  18. 18. Multiple InheritanceProblem Statement 9.P.1 (Contd.)class spellchecker: public application{protected: char lang[5];public: void getspell(); void showspell();};©NIIT OOPS/Lesson 11/Slide 18 of 24
  19. 19. Multiple InheritanceProblem Statement 9.P.1 (Contd.)class calculator:virtual application{protected: int noofoperations;public: void getcal(); void showcal();};©NIIT OOPS/Lesson 11/Slide 19 of 24
  20. 20. Multiple InheritanceProblem Statement 9.P.1 (Contd.) class spreadsheet : public spellchecker, calculator{private: int noofsheets;public: void getsheet(); void showsheet();};©NIIT OOPS/Lesson 11/Slide 20 of 24
  21. 21. Multiple InheritanceProblem Statement 9.P.1 (Contd.) int main(){ spreadsheet s; s.getsheet(); s.showsheet(); return 0; }Complete the code for the class definitions and identifyand fix the errors. Compile and execute the code.©NIIT OOPS/Lesson 11/Slide 21 of 24
  22. 22. Multiple InheritanceSummaryIn this lesson, you learned that: Multiple Inheritance is the phenomenon where a class may inherit from two or more classes Ambiguities that arise due to multiple inheritance can be resolved by: Using the scope resolution operator Overriding the members A derived class can inherit the members of a class twice by inheriting base class members through different paths©NIIT OOPS/Lesson 11/Slide 22 of 24
  23. 23. Multiple InheritanceSummary (Contd.) A single copy of base class members can be inherited by using virtual base classes A base class can be specified as virtual by using the virtual keyword The order of execution of the constructors in a hierarchy involving virtual base classes is: Virtual base class constructors – in the order of inheritance Non-virtual base class constructors – in the order of inheritance©NIIT OOPS/Lesson 11/Slide 23 of 24
  24. 24. Multiple InheritanceSummary (Contd.) Member objects constructors – in the order of declaration Derived class constructors Destructors are invoked in reverse order of the invocation of constructors©NIIT OOPS/Lesson 11/Slide 24 of 24

×