tailieunhanh - Chapter 10 - Object-Oriented Programming Polymorphism

Object-Oriented Programming: Polymorphism1Outline Introduction Relationships Among Objects in an Inheritance Hierarchy Invoking Base­Class Functions from Derived­Class Objects | Object-Oriented Programming: Polymorphism Outline Introduction Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class Objects Aiming Derived-Class Pointers at Base-Class Objects Derived-Class Member-Function Calls via Base-Class Pointers Virtual Functions Polymorphism Examples Type Fields and switch Structures Abstract Classes Case Study: Inheriting Interface and Implementation Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood” Virtual Destructors Introduction Polymorphism “Program in the general” Treat objects in same class hierarchy as if all base class Virtual functions and dynamic binding Will explain how polymorphism works Makes programs extensible New classes added easily, can still be processed In our examples Use abstract base class Shape Defines common interface (functionality) Point, Circle and Cylinder inherit from Shape Class . | Object-Oriented Programming: Polymorphism Outline Introduction Relationships Among Objects in an Inheritance Hierarchy Invoking Base-Class Functions from Derived-Class Objects Aiming Derived-Class Pointers at Base-Class Objects Derived-Class Member-Function Calls via Base-Class Pointers Virtual Functions Polymorphism Examples Type Fields and switch Structures Abstract Classes Case Study: Inheriting Interface and Implementation Polymorphism, Virtual Functions and Dynamic Binding “Under the Hood” Virtual Destructors Introduction Polymorphism “Program in the general” Treat objects in same class hierarchy as if all base class Virtual functions and dynamic binding Will explain how polymorphism works Makes programs extensible New classes added easily, can still be processed In our examples Use abstract base class Shape Defines common interface (functionality) Point, Circle and Cylinder inherit from Shape Class Employee for a natural example Relationships Among Objects in an Inheritance Hierarchy Previously (Section ), Circle inherited from Point Manipulated Point and Circle objects using member functions Now Invoke functions using base-class/derived-class pointers Introduce virtual functions Key concept Derived-class object can be treated as base-class object “is-a” relationship Base class is not a derived class object Invoking Base-Class Functions from Derived-Class Objects Aim pointers (base, derived) at objects (base, derived) Base pointer aimed at base object Derived pointer aimed at derived object Both straightforward Base pointer aimed at derived object “is a” relationship Circle “is a” Point Will invoke base class functions Function call depends on the class of the pointer/handle Does not depend on object to which it points With virtual functions, this can be changed (more later) (1 of 1) 1 // Fig. : 2 // Point class definition represents .

TÀI LIỆU LIÊN QUAN