Object-Oriented Programming
(OOP)
Lecture No. 31
Multiple Inheritance
• A class can inherit from more
then one class
Multiple Inheritance
Transmitter
...
Transmit()
Receiver
...
Receive()
Phone
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
clas...
Multiple Inheritance
• The derived class inherits data
members and functions form all
the base classes
• Object of derived...
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 ...
Example
int main(){
Phone obj;
Transmitter * tPtr = &obj;
Receiver * rPtr = &obj;
return 0;
}
Multiple Inheritance
• The pointer of one base class
cannot be used to call the
function of another base class
• The funct...
Example
int main(){
Phone obj;
Transmitter * tPtr = &obj;
tPtr->Transmit();
tPtr->Receive(); //Error
return 0;
}
Example
int main(){
Phone obj;
Receiver * rPtr = &obj;
rPtr->Receive();
rPtr->Transmit(); //Error
return 0;
}
Multiple Inheritance
• If more than one base class have
a function with same signature
then the child will have two copies...
Multiple Inheritance
Amphibious
Vehicle
Land Vehicle Water Vehicle
Car Boat
Example
class LandVehicle{
public:
int GetMaxLoad();
};
class WaterVehicle{
public:
int GetMaxLoad();
};
Example
class AmphibiousVehicle:
public LandVehicle,
public WaterVehicle{
};
int main(){
AmphibiousVehicle obj;
obj.GetMax...
Multiple Inheritance
• Programmer must explicitly
specify the class name when
calling ambiguous function
Example
int main(){
AmphibiousVehicle obj;
obj.LandVehicle::GetMaxLoad();
obj.WaterVehicle::GetMaxLoad();
return 0;
}
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
Example
class Vehicle{
public:
int GetMaxLoad();
};
class LandVehicle : public Vehicle{
};
class WaterVehicle : public Veh...
Example
class AmphibiousVehicle:
public LandVehicle,
public WaterVehicle{
};
int main(){
AmphibiousVehicle obj;
obj.GetMax...
Example
int main()
{
AmphibiousVehicle obj;
obj.Vehicle::GetMaxLoad(); //Error
return 0;
}
• Vehicle is accessible through...
Multiple Inheritance
Amphibious
Vehicle
Land Vehicle Water Vehicle
Vehicle
Car Boat
Vehicle
Example
int main(){
AmphibiousVehicle obj;
obj.LandVehicle::GetMaxLoad();
obj.WaterVehicle::GetMaxLoad();
return 0;
}
Multiple Inheritance
• Data member must be used with
care when dealing with more then
one level on inheritance
Example
class Vehicle{
protected:
int weight;
};
class LandVehicle : public Vehicle{
};
class WaterVehicle : public Vehicl...
Example
class AmphibiousVehicle:
public LandVehicle,
public WaterVehicle{
public:
AmphibiousVehicle(){
LandVehicle::weight...
Memory View
Data Members of
Vehicle
Data Members of
LandVehicle
Data Members of
AmphibiousVehicle
Data Members of
Vehicle
...
Virtual Inheritance
• In virtual inheritance there is
exactly one copy of the
anonymous base class object
Example
class Vehicle{
protected:
int weight;
};
class LandVehicle :
public virtual Vehicle{
};
class WaterVehicle :
publi...
Example
class AmphibiousVehicle:
public LandVehicle,
public WaterVehicle{
public:
AmphibiousVehicle(){
weight = 10;
}
};
Memory View
Data Members of Vehicle
Data Members of
LandVehicle
Data Members of
AmphibiousVehicle
Data Members of
WaterVeh...
Virtual Inheritance
• Virtual inheritance must be used
when necessary
• There are situation when
programmer would want to ...
Example
Student
BS Student MS Student PhD Student
MS/PhD Student
GPA
of 35

Polymorphism (oop) lecture31

Published on: Mar 4, 2016
Published in: Education      Technology      
Source: www.slideshare.net


Transcripts - Polymorphism (oop) lecture31

  • 1. Object-Oriented Programming (OOP) Lecture No. 31
  • 2. Multiple Inheritance • A class can inherit from more then one class
  • 3. Multiple Inheritance Transmitter ... Transmit() Receiver ... Receive() Phone
  • 4. Example class Phone: public Transmitter, public Receiver { ... };
  • 5. Multiple Inheritance • Derived class can inherit from public base class as well as private and protected base classes class Mermaid: private Woman, private Fish
  • 6. Multiple Inheritance • The derived class inherits data members and functions form all the base classes • Object of derived class can perform all the tasks that an object of base class can perform
  • 7. Example int main(){ Phone obj; obj.Transmit(); obj.Receive(); return 0; }
  • 8. Multiple Inheritance • When using public multiple inheritance, the object of derived class can replace the objects of all the base classes
  • 9. Example int main(){ Phone obj; Transmitter * tPtr = &obj; Receiver * rPtr = &obj; return 0; }
  • 10. 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
  • 11. Example int main(){ Phone obj; Transmitter * tPtr = &obj; tPtr->Transmit(); tPtr->Receive(); //Error return 0; }
  • 12. Example int main(){ Phone obj; Receiver * rPtr = &obj; rPtr->Receive(); rPtr->Transmit(); //Error return 0; }
  • 13. 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
  • 14. Multiple Inheritance Amphibious Vehicle Land Vehicle Water Vehicle Car Boat
  • 15. Example class LandVehicle{ public: int GetMaxLoad(); }; class WaterVehicle{ public: int GetMaxLoad(); };
  • 16. Example class AmphibiousVehicle: public LandVehicle, public WaterVehicle{ }; int main(){ AmphibiousVehicle obj; obj.GetMaxLoad(); // Error return 0; }
  • 17. Multiple Inheritance • Programmer must explicitly specify the class name when calling ambiguous function
  • 18. Example int main(){ AmphibiousVehicle obj; obj.LandVehicle::GetMaxLoad(); obj.WaterVehicle::GetMaxLoad(); return 0; }
  • 19. Multiple Inheritance • The ambiguous call problem can arise when dealing with multiple level of multiple inheritance
  • 20. Multiple Inheritance Amphibious Vehicle Land Vehicle Water Vehicle Vehicle Car Boat
  • 21. Example class Vehicle{ public: int GetMaxLoad(); }; class LandVehicle : public Vehicle{ }; class WaterVehicle : public Vehicle{ };
  • 22. Example class AmphibiousVehicle: public LandVehicle, public WaterVehicle{ }; int main(){ AmphibiousVehicle obj; obj.GetMaxLoad(); // Error return 0; }
  • 23. Example int main() { AmphibiousVehicle obj; obj.Vehicle::GetMaxLoad(); //Error return 0; } • Vehicle is accessible through two paths
  • 24. Multiple Inheritance Amphibious Vehicle Land Vehicle Water Vehicle Vehicle Car Boat Vehicle
  • 25. Example int main(){ AmphibiousVehicle obj; obj.LandVehicle::GetMaxLoad(); obj.WaterVehicle::GetMaxLoad(); return 0; }
  • 26. Multiple Inheritance • Data member must be used with care when dealing with more then one level on inheritance
  • 27. Example class Vehicle{ protected: int weight; }; class LandVehicle : public Vehicle{ }; class WaterVehicle : public Vehicle{ };
  • 28. Example class AmphibiousVehicle: public LandVehicle, public WaterVehicle{ public: AmphibiousVehicle(){ LandVehicle::weight = 10; WaterVehicle::weight = 10; } }; • There are multiple copies of data member weight
  • 29. Memory View Data Members of Vehicle Data Members of LandVehicle Data Members of AmphibiousVehicle Data Members of Vehicle Data Members of WaterVehicle
  • 30. Virtual Inheritance • In virtual inheritance there is exactly one copy of the anonymous base class object
  • 31. Example class Vehicle{ protected: int weight; }; class LandVehicle : public virtual Vehicle{ }; class WaterVehicle : public virtual Vehicle{ };
  • 32. Example class AmphibiousVehicle: public LandVehicle, public WaterVehicle{ public: AmphibiousVehicle(){ weight = 10; } };
  • 33. Memory View Data Members of Vehicle Data Members of LandVehicle Data Members of AmphibiousVehicle Data Members of WaterVehicle
  • 34. Virtual Inheritance • Virtual inheritance must be used when necessary • There are situation when programmer would want to use two distinct data members inherited from base class rather then one
  • 35. Example Student BS Student MS Student PhD Student MS/PhD Student GPA