Docsity

Understanding Multiple Inheritance in OOP: Data & Function Inheritance, Slides of Object Oriented Programming

Partial preview of the text

Download Understanding Multiple Inheritance in OOP: Data & Function Inheritance and more Slides Object Oriented Programming in PDF only on Docsity!

Object-Oriented Programming

(OOP)

Lecture No. 31

Multiple Inheritance

  • A class can inherit from more then one class

Example

class Phone: public Transmitter, public Receiver

{

...

};

Multiple Inheritance

  • Derived class can inherit from public base class as well as private and protected base classes

class Mermaid:

private Woman, private Fish

Example

int main(){

Phone obj; obj.Transmit(); obj.Receive(); return 0;

}

Multiple Inheritance

  • When using public multiple inheritance, the object of derived class can replace the objects of all the base classes

Multiple Inheritance

  • The pointer of one base class cannot be used to call the function of another base class
  • The functions are called based on static type

Example

int main(){

Phone obj; Transmitter * tPtr = &obj; tPtr->Transmit(); tPtr->Receive(); //Error return 0;

}

Multiple Inheritance

  • If more than one base class have a function with same signature then the child will have two copies of that function
  • Calling such function will result in ambiguity

Multiple Inheritance

Amphibious Vehicle

Land Vehicle Water Vehicle

Car Boat

Example

class AmphibiousVehicle: public LandVehicle, public WaterVehicle{ }; int main(){ AmphibiousVehicle obj; obj.GetMaxLoad(); // Error return 0; }

Multiple Inheritance

  • Programmer must explicitly specify the class name when calling ambiguous function

Multiple Inheritance

  • The ambiguous call problem can arise when dealing with multiple level of multiple inheritance

Multiple Inheritance

Amphibious Vehicle

Land Vehicle Water Vehicle

Vehicle

Car Boat