Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 264 Object-Oriented Software Development

Similar presentations


Presentation on theme: "ECE 264 Object-Oriented Software Development"— Presentation transcript:

1 ECE 264 Object-Oriented Software Development
Instructor: Dr. Honggang Wang Fall 2012 Lecture 34: Polymorphism

2 Lecture outline Announcements / reminders Today
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 4/11/2017 ECE 264: Lecture 34

3 Student Evaluation Instructions
Fall'09 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 number (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 Dr. Xing Lecture #21

4 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 4/11/2017 ECE 264: Lecture 34

5 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()). 4/11/2017 ECE 264: Lecture 34

6 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 4/11/2017 ECE 264: Lecture 34

7 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 4/11/2017 ECE 264: Lecture 34

8 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 4/11/2017 ECE 264: Lecture 34

9 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; 4/11/2017 ECE 264: Lecture 34

10 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 } 4/11/2017 ECE 264: Lecture 34

11 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; } 4/11/2017 ECE 264: Lecture 34

12 Example (functions in red are virtual)
Employee # name : string # payRate : float + Employee(string theName, float thePayRate) + getName() : string + getPayRate() : float + print(float hoursWorked) + pay(float hoursWorked) : float + printPay(float hoursWorked) Manager # salaried : bool + Manager(string theName, float thePayRate, bool isSalaried) + isSalaried() : bool + pay(float hoursWorked) : float + printPay(float hoursWorked) 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); 4/11/2017 ECE 264: Lecture 34

13 Example solution If we have: Employee Manager
# string name # float payRate + Employee(string theName, float thePayRate) + string getName() + float getPayRate() + void print(float hoursWorked) + float pay(float hoursWorked) + void printPay(float hoursWorked) Manager # bool salaried + Manager(string theName, float thePayRate, bool isSalaried) + bool isSalaried() + float pay(float hoursWorked) + void printPay(float hoursWorked) 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) 4/11/2017 ECE 264: Lecture 34

14 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. 4/11/2017 ECE 264: Lecture 34


Download ppt "ECE 264 Object-Oriented Software Development"

Similar presentations


Ads by Google