ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
ECE 264 Object-Oriented Software Development
Inheritance. Today: Inheritance and derived classes Is-A relationship class hierarchies proper inheritance (pure) polymorphism virtual functions protected.
Inheritance, Polymorphism, and Virtual Functions
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
C++ fundamentals.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Programming Languages and Paradigms Object-Oriented Programming.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 4: Continuing with C++ I/O Basics.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 3: Requirements Specification, C++ Basics.
SEN 909 OO Programming in C++ Final Exam Multiple choice, True/False and some minimal programming will be required.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
Monday, Mar 31, 2003Kate Gregory with material from Deitel and Deitel Week 12 Labs 4 and 5 are back File IO Looking ahead to the final.
Chapter 10 Inheritance and Polymorphism
Object-Oriented Programming in C++ More examples of Association.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Exam 3 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with C++ I/O Basics.
Summing Up Object Oriented Design. Four Major Components: Abstraction modeling real-life entities by essential information only Encapsulation clustering.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro.
Lecture 5 : Inheritance & Polymorphism Acknowledgement : courtesy of Prof. Timothy Budd lecture slides.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 22: Pointers.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 11: Class diagrams; class relationships.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 9: Continuing with classes.
Object Oriented Programming
Foundations: Language mechanisms and primitive OO concepts Lecture 1: Classification and Inheritance Michigan State University Spring 2008 E. Kraemer Notes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Inheritance ndex.html ndex.htmland “Java.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 20: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 24: Pointers and Dynamic Allocation.
ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 16: Destructors, Copy Constructors and Exam 2 Review.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 19: Container classes; strings.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC 202 Polymorphism.
ECE 264 Object-Oriented Software Development
7. Inheritance and Polymorphism
Polymorphism &Virtual Functions
group work #hifiTeam
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
Java Programming Language
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Constructors and Destructors
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
CISC/CMPE320 - Prof. McLeod
Polymorphism CMSC 202, Version 4/02.
CIS 199 Final Review.
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
Programming in C# CHAPTER 5 & 6
Presentation transcript:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism

Lecture outline Announcements / reminders  Monday Session(April 15) re-scheduled to April 17, Wednesday; Due April 19, Friday, by 5:00 pm  Project Demonstration: April 25 and April 30 Today  Review inheritance basics  Inheritance examples  Polymorphism 2/24/2016 ECE 264: Lecture 18 2

Reviewing inheritance Can take existing code base and  Produce specialized version Example: Square is a more specific Rectangle  Add extensions Example: Manager class we discussed last time adds extra data/functionality to Employee Existing class: base class New class: derived class  Reuses data/functions from base class Inherits (almost) everything  Access permissions for base class members set in base class Any class can be a base class  can have hierarchy Base class can have multiple derived classes 2/24/2016 ECE 264: Lecture 18 3

4 Constructors and Inheritance Default constructor for a base class is called automatically in the derived class constructor  Ex: Manager() calls Employee()  Will actually traverse inheritance hierarchy, starting at lowest class If a derived class needs the parameterized constructor of a base class, it must explicitly invoke it in an initialization list Manager::Manager(string theName, float thePayRate, bool isSalaried): Employee(theName, thePayRate) { salaried = isSalaried; } 2/24/2016

Inheritance: Manager methods How would you write the Manager pay function? Here’s where we encounter access, reuse issues Would like to:  Return payRate if salaried  Call Employee function if not salaried 2/24/2016 ECE 264: Lecture 18 5

Protected data Problem: Manager pay function can’t access private data in Employee class  Solution: third type of access specifier: protected Protected data in a class can be directly accessed by  Functions within that class  Functions within derived classes of that class Still effectively private data to outside world 2/24/2016 ECE 264: Lecture 18 6

Solution: Manager pay function float Manager::pay(float hrsWorked) { if (salaried) return payRate; else return Employee::pay(hrsWorked); } If Employee data declared protected, not private, can directly access it in Manager function Explicitly call Employee version of pay function 2/24/2016 ECE 264: Lecture 18 7

Inheritance syntax class BClass { protected: int var1; private: int var2; public: BClass(); BClass(int v1, int v2); int sum(); }; class DClass : public BClass { private: int var3; public: DClass(); Dclass(int v1, int v2, int v3); int sum3(); }; DClass inherits from BClass  Has same data members, functions  Can add additional data  May not have access protected data are accessible to derived classes  Still private to outside world 2/24/2016 ECE 264: Lecture 18 8

Inheritance example class BClass { protected: int var1; private: int var2; public: BClass(); BClass(int v1, int v2); int sum(); }; class DClass : public BClass { private: int var3; public: DClass(); Dclass(int v1, int v2, int v3); int sum3(); }; What statements in the program below cause errors? int main() { BClass b1(2,3); DClass d1(3,4,5); int a = b1.sum(); int b = d1.sum(); int c = d1.var1; int d = d1.sum3(); int e = b1.sum3(); return 0; } 2/24/2016 ECE 264: Lecture 18 9

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 2/24/2016 ECE 264: Lecture 18 10

Virtual functions For example, a base class Animal could have a virtual function eat.  Derived class Fish would implement eat() differently than derived class Wolf  but you can invoke eat() on any class instance referred to as Animal, and get the eat() behavior of the specific derived class  This allows a programmer to process a list of objects of class Animal, telling each in turn to eat (by calling eat()). 2/24/2016 ECE 264: Lecture 18 11

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 /24/2016

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 2/24/2016 ECE 264: Lecture 18 13

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 /24/2016

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; } 2/24/2016 ECE 264: Lecture 18 15

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 } 2/24/2016 ECE 264: Lecture 18 16

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; } 2/24/2016 ECE 264: Lecture 18 17

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; 2/24/2016 ECE 264: Lecture 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) Which statements are legal? Which function version gets called (for virtual functions)? a. e1.isSalaried(); b. m1.getName(); c. e1.pay(40); d. ePtr->pay(40); e. mPtr->print(40); f. e1.printPay(40);

Example solution If we have: Employee e1(“Bob”,25); Manager m1(“Jim”, 40, true); Employee* ePtr; Manager *mPtr; e1 = m1; ePtr = &m1; mPtr = &m1; 2/24/2016 ECE 264: Lecture Employee # 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) Which statements are legal? Which function version gets called (for virtual functions)? a. e1.isSalaried();  ILLEGAL b. m1.getName();  calls Manager.getName() c. e1.pay(40);  calls Employee.pay(40) d. ePtr->pay(40);  calls Manager.pay(40) e. mPtr->print(40);  calls Employee.print(40), which calls Manager.printPay(40) f. e1.printPay(40);  calls Employee.printPay(40)

Exam 2 Average Score: 85 STD: 8 Solutions will be uploaded today 2/24/2016 ECE 264: Lecture 18 20

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, 8 th ed. Etter & Ingber, Engineering Problem Solving with C++, 2 nd ed. 2/24/2016 ECE 264: Lecture 18 21