ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro.

Slides:



Advertisements
Similar presentations
ECE 264 Object-Oriented Software Development
Advertisements

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 10: Continuing with classes Constructors, using classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 21: Strings (cont.)
CS 211 Inheritance AAA.
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
CMSC 202 Inheritance. Aug 6, Object Relationships An object can have another object as one of instance variables. The Person class had two Date.
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Derived Classes in C++CS-2303, C-Term Derived Classes in C++ CS-2303 System Programming Concepts (Slides include materials from The C Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 15: Class diagrams; class relationships.
Vocabulary Key Terms polymorphism - Selecting a method among many methods that have the same name. subclass - A class that inherits variables and methods.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
Unit 5 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
Sadegh Aliakbary Sharif University of Technology Fall 2010.
OBJECT ORIENTED PROGRAMMING CONCEPTS ISC 560. Object-oriented Concepts  Objects – things names with nouns  Classes – classifications (groups) of similar.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 6: Continuing with output formatting.
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.
The Queue class //this class implements a Queue class Queue{ int q[100];//an array of integers int sloc, rloc; //start location and rear location public:
MIT AITI 2003 – Lecture 12 Inheritance. What is Inheritance?  Real World Example: We inherit traits from our mother and father. We also inherit traits.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 26: Exam 2 Preview.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Fall 2012 Lecture 8: File I/O; Introduction to classes.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 2: Software Design Cycle.
Chapter Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Peyman Dodangeh Sharif University of Technology Fall 2014.
MIT AITI 2004 – Lecture 12 Inheritance. What is Inheritance?  In the real world: We inherit traits from our mother and father. We also inherit traits.
CMSC 202 Inheritance I Class Reuse with Inheritance.
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.
Inheritance and Access Control CS 162 (Summer 2009)
Inheritance and Design CSIS 3701: Advanced Object Oriented Programming.
Exercise 2 Introduction to C# CIS Create a class called Employee that contains the following private instance variables: Social Securitystring.
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
Cs205: engineering software university of virginia fall 2006 Subtyping and Inheritance David Evans Quiz Friday: classes through.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 29: Operator overloading.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 5: Continuing with output formatting.
Object Inheritance Lecturer: Kalamullah Ramli Electrical Engineering Dept. University of Indonesia Session-4.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 18: More on inheritance and Polymorphism.
Chapter 7 Classes and Methods III: Static Methods and Variables Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition)
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.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
ECE 264 Object-Oriented Software Development
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 11: Examples—creating a basic class.
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.
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.
1 Inheritance One of the goals of object oriented programming is code reuse. Inheritance is one mechanism for accomplishing code reuse. It allows us to.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Advanced Programming in Java
Today’s Objectives 5-Jul-2006 Announcements
ECE 264 Object-Oriented Software Development
COMP 2710 Software Construction Inheritance
Comp 249 Programming Methodology
Inheritance I Class Reuse with Inheritance
Chapter 9 Object-Oriented Programming: Inheritance
Instructor: Dr. Michael Geiger Spring 2017 Lecture 34: Inheritance
Inheritance, Polymorphism, and Interfaces. Oh My
Programming with Microsoft Visual Basic 2008 Fourth Edition
Advanced Programming Behnam Hatami Fall 2017.
Class Reuse with Inheritance
انجمن جاواکاپ تقدیم می‌کند دوره برنامه‌نويسی جاوا
CIS 199 Final Review.
Presentation transcript:

ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Fall 2012 Lecture 31: Operator overloading examples, inheritance intro

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 me with conflicts, preferences Today  Introduce inheritance 12/12/2015 ECE 264: Lecture 31 2

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

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

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

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

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

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 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

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

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