Download presentation
Presentation is loading. Please wait.
Published byEarl Knight Modified over 9 years ago
1
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro
2
Lecture outline Announcements / reminders Project design grading to be done by this evening Project demo scheduling Demos in lecture Friday, 12/7 Entire group must be present E-mail me with conflicts, preferences Today Introduce inheritance 12/12/2015 ECE 264: Lecture 31 2
3
Motivating Inheritance Say we have two classes as part of a company’s payroll system Employee models general company employee who is paid hourly Pay = (pay rate) * (hours worked) Manager models a specific type of employee that may be salaried or hourly Pay = (pay rate) 12/12/2015 ECE 264: Lecture 31 3
4
Inheritance example: class diagram Employee - string name - float payRate + Employee() + Employee(string theName, float thePayRate) + getName() : string + getPayRate() : float + pay(float hrsWorked) : float Manager - string name - float payRate - bool salaried + Manager() + Manager(string theName, float thePayRate, bool isSalaried) + getName() : string + getPayRate() : float + isSalaried() : bool + setSalaried(bool sal) + pay(float hrsWorked) : float 12/12/2015 ECE 264: Lecture 31 4 Functions/data in red completely redundant Functions in blue share some functionality Would like to reuse code wherever possible
5
Inheritance The creation of a new class from an existing class is called inheritance Promotes software reusability Terminology: Base class is existing class Derived class reuses data and functions from base class Can customize base class functions Can add functions to derived class Can have inheritance hierarchy 12/12/2015 ECE 264: Lecture 31 5
6
Inheritance and UML Inheritance models “is a” relationship Example: a square is a rectangle with equal length sides Contrast with composition (“has a”) Example: a rectangle has a point of origin 12/12/2015 ECE 264: Lecture 31 6 Rectangle Square Point
7
Inheritance example: base class class Employee { private: string name; float payRate; public: Employee(); Employee(string n, float pr); string getName(); float getPayRate(); float pay(float hrsWorked); }; 12/12/2015 ECE 264: Lecture 31 7
8
Inheritance example: derived class class Manager : public Employee { private: bool salaried; public: Manager(); Manager(string theName, float thePayRate, bool isSalaried); bool isSalaried(); void setSalaried(bool sal); float pay(float hrsWorked); }; The notation above indicates that Manager inherits from Employee Only declare data/functions that are not shared Access can be tricky 12/12/2015 ECE 264: Lecture 31 8
9
9 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 12/12/2015
10
Inheritance: Manager constructors How would you write the two constructors for the Manager class? Manager(); Manager::Manager() { salaried = false;} Employee default constructor called automatically Manager(string theName, float thePayRate, bool isSalaried); Manager::Manager(string theName, float thePayRate, bool isSalaried): Employee(theName, thePayRate) { salaried = isSalaried; } Explicitly call Employee parameterized constructor 12/12/2015 ECE 264: Lecture 31 10
11
Final notes Next time More on inheritance 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. 12/12/2015 ECE 264: Lecture 31 11
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.