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