Polymorphism J EXample void loan check in(LibMat& mat) mat.check inO if (mat is late) mat assess fine( if (mat. waiting listo matnotify available Since libat is an abstract base class and there are only real objects of class Renta/Book or Magazine in the program how could mat be instantiated and referenced?
Polymorphism ◼ Example void loan_check_in(LibMat& mat) { mat.check_in(); if (mat.is_late()) mat.assess_fine(); if (mat.waiting_list()) mat.notify_available(); } ◼ Since LibMat is an abstract base class, and there are only real objects of class RentalBook or Magazine in the program, how could mat be instantiated and referenced?
Polymorphism(cont.) u Polymorphism Use the pointer or reference of the base class to point to any object of its derived class Thus we got transparency that we manipulate those real objects in a type-independent way Same code will represent different objects and make different behaviors Code will remain same if the inheritance hierarchy changes
Polymorphism (cont.) ◼ Polymorphism ◼ Use the pointer or reference of the base class to point to any object of its derived class ◼ Thus we got transparency that we manipulate those real objects in a type-independent way ◼ Same code will represent different objects and make different behaviors ◼ Code will remain same if the inheritance hierarchy changes
Polymorphism(cont.) a Static Binding-common situation Which function entrance to use could be decided before execution a Dynamic Binding- mechanism of polymorphism Which version of the check _in should be called could only be decided during execution by the addressed real object that mat represents a Delay the resolution until run-time
Polymorphism (cont.) ◼ Static Binding – common situation ◼ Which function entrance to use could be decided before execution ◼ Dynamic Binding – mechanism of polymorphism ◼ Which version of the check_in() should be called, could only be decided during execution by the addressed real object that mat represents ◼ Delay the resolution until run-time
Class Hierarchy
Class Hierarchy
Implement a inheritance hierarchy u An example of 3 layers, LibMat derives Book, Book derives AudioBook We define a simple public interface, which containing only constructor, destructor and print function(each class has its own print way class libMan public: LibMatoI cout <<"LibMat Default Constructor!<< end; y virtual -LibMatoi cout << "LibMat Destructor! < endl; y virtual void print0 const{cout≤<“ This is a LibMat object!”<<endl; }
Implement a inheritance hierarchy ◼ An example of 3 layers: LibMat derives Book, Book derives AudioBook ◼ We define a simple public interface, which containing only constructor, destructor and print function (each class has its own print way) class LibMat { public: LibMat() { cout << “LibMat Default Constructor!” << endl; } virtual ~LibMat() { cout << “LibMat Destructor!” << endl; } virtual void print() const { cout << “This is a LibMat object!” << endl; } };