140 likes | 239 Views
ECE 264 Object-Oriented Software Development. Instructor: Dr. Honggang Wang Fall 2012 Lecture 34: Polymorphism. Lecture outline. Announcements / reminders Exam 3 Review next Monday Sample questions will be uploaded next Tuesday Project Demos: December 7 Code: due December 10
E N D
ECE 264Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 34: Polymorphism
Lecture outline • Announcements / reminders • Exam 3 Review next Monday • Sample questions will be uploaded next Tuesday • Project • Demos: December 7 • Code: due December 10 • Tech report/user guide: due December 10 • Lab 10 now due Tuesday • Today • Teaching Evaluations • Polymorphism ECE 264: Lecture 34
Student Evaluation Instructions • Print my last name WANG in the STUDENT NAME box. There is NO need to fill in the corresponding ovals. • Print course and section number26401(for ECE264) in the first 5 positions of the STUDENT ID NUMBER box. There is NO need to fill in the corresponding ovals. • Queries on the Questionnaire are matched to the numbers on the Answer Sheet. E: Strongly Agree; D: Agree; C: Neutral; B: Disagree; A: Strongly Disagree Lecture #21
Polymorphism • Polymorphism: Code/operations behave differently in different contexts • One example: operator overloading • Inheritance-based polymorphism in form of virtual functions • Add virtual to function declaration in .h file • Why use virtual functions? • One benefit of inheritance: code reuse • Another benefit: Can write generic code that works for (hopefully) many specific cases • Take advantage of fact that derived class “is an” object of base class type (with extra/more specific functions) • Virtual functions enable this second benefit ECE 264: Lecture 34
Virtual functions • For example, a base class Animal could have a virtual function eat. • Subclass Fish would implement eat() differently than subclass Wolf • but you can invoke eat() on any class instance referred to as Animal, and get the eat() behaviour of the specific subclass • This allows a programmer to process a list of objects of class Animal, telling each in turn to eat (by calling eat()). ECE 264: Lecture 34
Static binding • All methods are, by default, non-virtual methods. Binding of method call is determined by static type of calling object. Example: Employee e1(“John Smith”, 20); Manager m1(“Bob Jones”, 1500, true); e1 = m1; e1.pay(40); //Calls pay() defined in //Employee ECE 264: Lecture 34
Object pointers & inheritance • Dynamic binding: determine type of object at runtime • With inheritance, pointer to base class supports objects of: • Base class type • Derived class type • Pointers also support dynamic binding! • Allows us to write general code using base class pointers ECE 264: Lecture 34
Static vs. dynamic binding • Methods default to non-virtual type of object determines method called • With virtual method, can use dynamic binding if using pointers or references Example: Employee e1(“John Smith”, 20; Manager m1(“Bob Jones”, 1500, true); Employee *ePtr; e1 = m1; ePtr = &m1; e1.pay(40); // Calls pay() defined in // Employee ePtr->pay(40); // Calls pay() defined in // Manager ECE 264: Lecture 34
References and virtual methods • Remember: passing arguments by reference essentially passes pointer • Can use virtual methods on references • Example: float payAnyone(Employee &e, float h) { return e.pay(h); } int main() { Employee e1(“John Smith”, 20; Manager m1(“Bob Jones”, 1500, true); payAnyone(e1, 40); // Calls pay() defined in // Employee payAnyone(m1, 40); // Calls pay() defined in // Manager return 0; } ECE 264: Lecture 34
Virtual functions and member functions • Calling one member function inside another implicitly uses pointers (and therefore dynamic binding) • If f1 is a function of class C • Calling f1 in another class C function is equivalent to: this->f1(); • Example: assume both Employee & Manager have virtual function printPay(float hoursWorked) void Employee::print(float hoursWorked) { cout << “Name: “ << name << endl; cout << “Pay rate: “ << payRate << endl; printPay(hoursWorked); // Uses dynamic // binding; will call // Manager version in // Manager objects } ECE 264: Lecture 34
Destructors and virtual functions • If a class has virtual functions, the destructor should be virtual • Often use pointers with dynamic allocation: Employee *ePtr; char eType = ‘e’; while (eType != ‘x’) { cin >> eType; if (eType == ‘e’) ePtr = new Employee(“John Smith”, 20); else if (eType == ‘m’) ePtr = new Manager(“Bob Jones”, 1500, true); if (ePtr != NULL) ePtr->print(40); delete ePtr; // Needs virtual destructors to ensure // either ~Employee() or ~Manager() is // called correctly ePtr = NULL; } ECE 264: Lecture 34
Example (functions in red are virtual) If we have: Employee e1(“Bob”,25); Manager m1(“Jim”, 40, true); Employee *ePtr; Manager *mPtr; e1 = m1; ePtr = &m1; mPtr = &m1; Which statements are legal? Which function version gets called (for virtual functions)? e1.isSalaried(); m1.getName(); e1.pay(40); ePtr->pay(40); mPtr->print(40); e1.printPay(40); ECE 264: Lecture 34
Example solution If we have: Employee e1(“Bob”,25); Manager m1(“Jim”, 40, true); Employee* ePtr; Manager *mPtr; e1 = m1; ePtr = &m1; mPtr = &m1; Which statements are legal? Which function version gets called (for virtual functions)? e1.isSalaried(); ILLEGAL m1.getName(); calls Manager.getName() e1.pay(40); calls Employee.pay(40) ePtr->pay(40); calls Manager.pay(40) mPtr->print(40); calls Employee.print(40), which calls Manager.printPay(40) e1.printPay(40); calls Employee.printPay(40) ECE 264: Lecture 34
Final notes • Next time • Abstract classes • Acknowledgements: this lecture borrows heavily from lecture slides provided with the following texts: • Deitel & Deitel, C++ How to Program, 8th ed. • Etter & Ingber, Engineering Problem Solving with C++, 2nd ed. ECE 264: Lecture 34