Question? Leave a message!




Application of Polymorphism

Application of Polymorphism
Dr.MasonHanks Profile Pic
Dr.MasonHanks,Germany,Teacher
Published Date:23-07-2017
Website URL
Comment
CIS 190: C/C++ Programming Lecture 11 Polymorphism 1 Outline • Review of Inheritance • Polymorphism – Limitations – Virtual Functions – Abstract Classes & Function Types – Virtual Function Tables – Virtual Destructors/Constructors • Application of Polymorphism • Project Alphas 2 Review of Inheritance • specialization through sub classes • child class has direct access to – parent member functions and variables that are • ??? 3 Review of Inheritance • specialization through sub classes • child class has direct access to – parent member functions and variables that are • public • protected 4 Review of Inheritance • specialization through sub classes • child class has direct access to – parent member functions and variables that are: • public • protected • parent class has direct access to: – ??? in the child class 5 Review of Inheritance • specialization through sub classes • child class has direct access to – parent member functions and variables that are: • public • protected • parent class has direct access to: – nothing in the child class 6 What is Inherited Parent Class • public members • private functions • protected • copy constructor members • assignment operator • constructor • private • destructor variables 7 What is Inherited Child Class Parent Class • public members • private functions • child class • protected • copy constructor members members • assignment operator (functions & • constructor variables) • private • destructor variables 8 Outline • Review of Inheritance • Polymorphism – Limitations – Virtual Functions – Abstract Classes & Function Types – Virtual Function Tables – Virtual Destructors/Constructors • Application of Polymorphism • Project Alphas 9 Car Example Car SUV Sedan Van Jeep class SUV: public Car /etc/; class Sedan: public Car /etc/; class Van: public Car /etc/; class Jeep: public Car /etc/; 10 Car Rental Example • we want to implement a catalog of different types of cars available for rental • how could we do this? 11 Car Rental Example • we want to implement a catalog of different types of cars available for rental • how could we do this? • can accomplish this with a single vector – using polymorphism 12 What is Polymorphism? • ability to manipulate objects in a type-independent way 13 What is Polymorphism? • ability to manipulate objects in a type-independent way • already done to an extent via overriding – child class overrides a parent class function 14 What is Polymorphism? • ability to manipulate objects in a type-independent way • already done to an extent via overriding – child class overrides a parent class function • can take it further using subtyping, AKA inclusion polymorphism 15 Using Polymorphism • a pointer of a parent class type can point to an object of a child class type Vehicle vehiclePtr = &myCar; • why is this valid? 16 Using Polymorphism • a pointer of a parent class type can point to an object of a child class type Vehicle vehiclePtr = &myCar; • why is this valid? – because myCar is-a Vehicle 17 Polymorphism: Car Rental vector Car rentalList; vector of Car objects 18 Polymorphism: Car Rental vector Car rentalList; vector of Car objects SUV SUV Jeep Van Jeep Sedan Sedan SUV • can populate the vector with any of Car’s child classes 19 Outline • Review of Inheritance • Polymorphism – Limitations – Virtual Functions – Abstract Classes & Function Types – Virtual Function Tables – Virtual Destructors/Constructors • Application of Polymorphism • Project Alphas 20