The c ++ Programming Language Lecture 5 Object-Oriented Programming
The C++ Programming Language Lecture 5: Object-Oriented Programming
Basic Concepts
Basic Concepts
Object Based object Oriented u Object Based Programming Classes regarded as new data types Single layer in hierarchy Not convenient in"Entity-Relation" relationship s has is implemented of Object Oriented Programming Focus on the relationship between classes Hierarchical in structure Good at Entity-Relation" relationship Main features: inheritance and polymorphism
Object Based & Object Oriented ◼ Object Based Programming ◼ Classes regarded as new data types ◼ Single layer in hierarchy ◼ Not convenient in “Entity-Relation” relationship – “is-a” – “has-a” – “is implemented of” ◼ Object Oriented Programming ◼ Focus on the relationship between classes ◼ Hierarchical in structure ◼ Good at “Entity-Relation” relationship ◼ Main features: inheritance and polymorphism
Inheritance u Organize those related classes and share their common data and operations a Parent/child relationship Parent(Base) class: defined common public interfaces and private implements of all child classes Child (derived) class: could override what is inherited, or add new implementations to fit its speciality
Inheritance ◼ Organize those related classes, and share their common data and operations ◼ Parent/child relationship ◼ Parent (Base) class: defined common public interfaces and private implements of all child classes ◼ Child (Derived) class: could override what is inherited, or add new implementations, to fit its speciality
Inheritance Hierarchy bstract Base Class> Lib Materials Books Magazine Files Rental Books Audio Books E-Books a If the root do not represent a real object, and it is only for design need we could call it Abstract Base Class( conceptual)
Inheritance Hierarchy ◼ If the root do not represent a real object, and it is only for design need, we could call it Abstract Base Class (Conceptual) Lib Materials Books Magazines Files Rental Books Audio Books E-Books Abstract Base Class →
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; } };