Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance

Slides:



Advertisements
Similar presentations
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Hash Tables,
Advertisements

ECE 264 Object-Oriented Software Development
Hashing as a Dictionary Implementation
1 Hash Tables Gordon College CS Hash Tables Recall order of magnitude of searches –Linear search O(n) –Binary search O(log 2 n) –Balanced binary.
Introducing Hashing Chapter 21 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Searching:
 2002 Prentice Hall. All rights reserved. 1 Introduction to Inheritance Inheritance: –1 of 3 main features of OOP –Form of software reusability –(Derived)
Hashing Dr. Yingwu Zhu.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Exam 3 Preview.
Hashing as a Dictionary Implementation Chapter 19.
Chapter 11 Hash Tables © John Urrutia 2014, All Rights Reserved1.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro.
Hash Tables © Rick Mercer.  Outline  Discuss what a hash method does  translates a string key into an integer  Discuss a few strategies for implementing.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.
ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 17: Operator overloading and inheritance intro.
2-Oct-16 Basic Object-Oriented Concepts. 2 Concept: An object has behaviors In old style programming, you had: data, which was completely passive functions,
CSCE 210 Data Structures and Algorithms
Advanced Programming in Java
Modern Programming Tools And Techniques-I
Hashing Problem: store and retrieving an item using its key (for example, ID number, name) Linked List takes O(N) time Binary Search Tree take O(logN)
CSCI 210 Data Structures and Algorithms
COMP 2710 Software Construction Inheritance
School of Computer Science and Engineering
Slides by Steve Armstrong LeTourneau University Longview, TX
Prof. Neary Adapted from slides by Dr. Katherine Gibson
Function and class templates
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance I Class Reuse with Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
MSIS 670 Object-Oriented Software Engineering
Lecture 22 Inheritance Richard Gesick.
Chapter 9: Polymorphism and Inheritance
CMSC 202 Inheritance.
Advanced Programming Behnam Hatami Fall 2017.
Class Reuse with Inheritance
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
Hash Tables Chapter 12.7 Wherein we throw all the data into random array slots and somehow obtain O(1) retrieval time Nyhoff, ADTs, Data Structures and.
Controlling Base-Class Creation
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
CS202 - Fundamental Structures of Computer Science II
EECE.2160 ECE Application Programming
Tree traversal preorder, postorder: applies to any kind of tree
Linked Lists.
CIS 199 Final Review.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
Data Structures & Algorithms
EECE.2160 ECE Application Programming
EE 312 Final Exam Review.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Collision Handling Collisions occur when different elements are mapped to the same cell.
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.2160 ECE Application Programming
Instructor: Dr. Michael Geiger Spring 2019 Lecture 23: Exam 2 Preview
EECE.2160 ECE Application Programming
Heaps and priority queues
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Getting queues right … finally (?)
Instructor: Dr. Michael Geiger Spring 2019 Lecture 34: Exam 3 Preview
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017
Instructor: Dr. Michael Geiger Spring 2017 Lecture 30: Sorting & heaps
Instructor: Dr. Michael Geiger Spring 2017 Lecture 33: Hash tables
Programming in C# CHAPTER 5 & 6
EECE.2160 ECE Application Programming
Presentation transcript:

Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance

Data Structures: Lecture 34 Lecture outline Announcements/reminders Program 5 due 4/26 Today’s lecture Review: Hash tables Inheritance in C++ 11/23/2018 Data Structures: Lecture 34

Data Structures: Lecture 34 Review: Hash Tables In some situations faster search is needed Solution is to use a hash function Value of key field given to hash function Location in a hash table is calculated 11/23/2018 Data Structures: Lecture 34

Review: Linear probing Observe the problem with same value returned by h(i) for different values of i Called collisions A simple solution is linear probing Linear search begins at collision location Continues until empty slot found for insertion 11/23/2018 Data Structures: Lecture 34

Review: other collision strategies Linear probing can result in primary clustering Consider quadratic probing Probe sequence from location i is i + 1, i – 1, i + 4, i – 4, i + 9, i – 9, … Secondary clusters can still form Double hashing Use a second hash function to determine probe sequence 11/23/2018 Data Structures: Lecture 34

Review: other collision strategies Chaining Table is a list or vector of head nodes to linked lists When item hashes to location, it is added to that linked list 11/23/2018 Data Structures: Lecture 34

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) 11/23/2018 Data Structures: Lecture 34

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 Functions/data in red completely redundant Functions in blue share some functionality Would like to reuse code wherever possible 11/23/2018 Data Structures: Lecture 34

Data Structures: Lecture 34 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 11/23/2018 Data Structures: Lecture 34

Inheritance vs. composition Inheritance models “is a” relationship Example: a square is a rectangle with equal length sides Contrast with composition (“has a”) (Lec. 13) Example: a rectangle has a point of origin 11/23/2018 Data Structures: Lecture 34

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); }; 11/23/2018 Data Structures: Lecture 34

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 11/23/2018 Data Structures: Lecture 34

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 11/23/2018 Data Structures: Lecture 34

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 11/23/2018 Data Structures: Lecture 34

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 11/23/2018 Data Structures: Lecture 34

Data Structures: Lecture 34 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 11/23/2018 Data Structures: Lecture 34

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 11/23/2018 Data Structures: Lecture 34

Data Structures: Lecture 34 Inheritance syntax class BClass { protected: int var1; private: int var2; public: BClass(); BClass(int v1, int v2); int sum(); }; class DClass : public BClass { int var3; 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 11/23/2018 Data Structures: Lecture 34

Data Structures: Lecture 34 Inheritance example class BClass { protected: int var1; private: int var2; public: BClass(); BClass(int v1, int v2); int sum(); }; class DClass : public BClass { int var3; 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; } 11/23/2018 Data Structures: Lecture 34

Data Structures: Lecture 34 Final notes Next time Balanced binary search trees Reminders: Program 5 due 4/26 11/23/2018 Data Structures: Lecture 34

Data Structures: Lecture 34 Acknowledgements These slides are adapted from slides provided with the course textbook: Larry Nyhoff, ADTs, Data Structures, and Problem Solving with C++, 2nd edition, 2005, Pearson/Prentice Hall. ISBN: 0-13-140909-3 11/23/2018 Data Structures: Lecture 34