ece 264 object-oriented software development instructor: dr. honggang wang spring 2013 lecture 18:...
Post on 18-Jan-2018
218 Views
Preview:
DESCRIPTION
TRANSCRIPT
ECE 264Object-Oriented
Software DevelopmentInstructor: 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
05/06/23 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
05/06/23 ECE 264: Lecture 18 3
ECE 264: Lecture 18 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; }
05/06/23
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
05/06/23 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
05/06/23 ECE 264: Lecture 18 6
Solution: Manager pay functionfloat Manager::pay(float hrsWorked){
if (salaried)return payRate;
elsereturn Employee::pay(hrsWorked);
}
If Employee data declared protected, not private, can directly access it in Manager function
Explicitly call Employee version of pay function
05/06/23 ECE 264: Lecture 18 7
Inheritance syntaxclass 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
05/06/23 ECE 264: Lecture 18 8
Inheritance exampleclass 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;}
05/06/23 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
05/06/23 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()).
05/06/23 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 18 1205/06/23
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
05/06/23 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
// EmployeeePtr->pay(40); // Calls pay() defined in
// Manager
ECE 264: Lecture 18 1405/06/23
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
// EmployeepayAnyone(m1, 40); // Calls pay() defined in
// Managerreturn 0;
}
05/06/23 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
}
05/06/23 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 correctlyePtr = NULL;
}
05/06/23 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;
05/06/23 ECE 264: Lecture 18 18
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;
05/06/23 ECE 264: Lecture 18 19
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
05/06/23 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, 8th ed. Etter & Ingber, Engineering Problem Solving with
C++, 2nd ed.
05/06/23 ECE 264: Lecture 18 21
top related