COMP 2710 Software Construction Inheritance

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Inheritance Basics Derived classes, with.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Inheritance Juan Marquez 12_inheritance.ppt
CS 211 Inheritance AAA.
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,
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Inheritance.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
Chapter 7 Inheritance Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Computer Science I Inheritance Professor Evan Korth New York University.
Chapter 15 Inheritance. Slide Overview 15.1 Inheritance Basics 15.2 Inheritance Details 15.3 Polymorphism.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Chapter 8 More Object Concepts
CMSC 202 Inheritance II. Version 10/102 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
CMSC 202 Inheritance I Class Reuse with Inheritance.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Creating Classes from Other Classes Appendix D © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
CMSC 202 Polymorphism 2 nd Lecture. Aug 6, Topics Constructors and polymorphism The clone method Abstract methods Abstract classes.
Chapter 16 Exception Handling
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Polymorphism 2nd Lecture
Chapter 15 Inheritance. Chapter 15 Inheritance.
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Constructors and Other Tools Dr.
10.2 Classes Copyright © 2008 Pearson Addison-Wesley. All rights reserved. 1.
Polymorphism 2nd Lecture
Polymorphism 2nd Lecture
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists File I/O Dr. Xiao Qin Auburn University.
Exercise 1 From Lec80b-Ponter.ppt.
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Auburn University COMP 3000 Object-Oriented Programming for Engineers and Scientists Structures Cont. Dr. Xiao Qin Auburn.
An Introduction to Inheritance
COMP 2710 Software Construction Pointers
COMP 2710 Software Construction File I/O
Road Map Inheritance Class hierarchy Overriding methods Constructors
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Comp 249 Programming Methodology
Inheritance November 10, 2006 ComS 207: Programming I (in Java)
Inheritance 2nd Lecture
Chapter 7 Inheritance Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance Basics Programming with Inheritance
Polymorphism 2nd Lecture
OOP and ADTs Chapter 14 Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved.
Comp 249 Programming Methodology
Inheritance I Class Reuse with Inheritance
Learning Objectives Inheritance Virtual Function.
Lecture 22 Inheritance Richard Gesick.
Comp 249 Programming Methodology
Inheritance 2nd Lecture
CMSC 202 Inheritance.
Class Reuse with Inheritance
Polymorphism Polymorphism
Computer Programming with JAVA
Inheritance 2nd Lecture
Overriding Methods & Class Hierarchies
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 8 Inheritance Part 2.
CMSC 202 Inheritance II.
Chapter 8 Abstract Classes.
Chapter 7 Inheritance.
Presentation transcript:

COMP 2710 Software Construction Inheritance Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu Inheritance Basics Derived classes, with constructors protected: qualifier Redefining member functions Non-inherited functions Programming with Inheritance Assignment operators and copy constructors Destructors in derived classes Multiple inheritance These slides are adapted from notes by Dr. Walter Savitch (UCSD)

Introduction to Inheritance Object-oriented programming Powerful programming technique Provides abstraction dimension called inheritance General form of class is defined Specialized versions then inherit properties of general class And add to it/modify it’s functionality for it’s appropriate use Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Inheritance Terminology Common to simulate family relationships Parent class Refers to base class Child class Refers to derived class Ancestor class Class that’s a parent of a parent … Descendant class Opposite of ancestor Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Inheritance Basics New class inherited from another class Base class "General" class from which others derive Derived class New class Automatically has base class’s: Member variables Member functions Can then add additional member functions and variables Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Derived Classes Consider example: Class of "Employees" Composed of: Salaried employees Hourly employees Each is "subset" of employees Another might be those paid fixed wage each month or week Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Employee Class Many members of "employee" class apply to all types of employees Accessor functions Mutator functions Most data items: SSN Name Pay We won’t have "objects" of this class, however Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Employee Class Consider printCheck() function: Will always be "redefined" in derived classes So different employee types can have different checks Makes no sense really for "undifferentiated" employee So function printCheck() in Employee class says just that Error message stating "printCheck called for undifferentiated employee!! Aborting…" Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Deriving from Employee Class Derived classes from Employee class: Automatically have all member variables Automatically have all member functions Derived class said to "inherit" members from base class Can then redefine existing members and/or add new members Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Display 14.3 Interface for the Derived Class HourlyEmployee (1 of 2) Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Display 14.3 Interface for the Derived Class HourlyEmployee (2 of 2) Derived class interface only lists new or "to be redefined" members Since all others inherited are already defined i.e.: "all" employees have ssn, name, etc. HourlyEmployee adds: Constructors wageRate, hours member variables setRate(), getRate(), setHours(), getHours() member functions Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Constructors in Derived Classes Base class constructors are NOT inherited in derived classes! But they can be invoked within derived class constructor Which is all we need! Base class constructor must initialize all base class member variables Those inherited by derived class So derived class constructor simply calls it "First" thing derived class constructor does Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Derived Class Constructor Example Consider syntax for HourlyEmployee constructor: HourlyEmployee::HourlyEmployee(string theName, string theNumber, double theWageRate, double theHours) : Employee(theName, theNumber), wageRate(theWageRate), hours(theHours) { //Deliberately empty } Portion after : is "initialization section" Includes invocation of Employee constructor Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Constructor: No Base Class Call Derived class constructor should always invoke one of the base class’s constructors If you do not: Default base class constructor automatically called Equivalent constructor definition: HourlyEmployee::HourlyEmployee() : wageRate(0), hours(0) { } Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Redefining vs. Overloading Very different! Redefining in derived class: SAME parameter list Essentially "re-writes" same function Overloading: Different parameter list Defined "new" function that takes different parameters Overloaded functions must have different signatures Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Functions Not Inherited All "normal" functions in base class are inherited in derived class Exceptions: Constructors (we’ve seen) Destructors Copy constructor But if not defined, generates "default" one Recall need to define one for pointers! Assignment operator If not defined  default Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Summary 1 Inheritance provides code reuse Allows one class to "derive" from another, adding features Derived class objects inherit members of base class And may add members Private member variables in base class cannot be accessed "by name" in derived Private member functions are not inherited Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

Copyright © 2010 Pearson Addison-Wesley. All rights reserved. Summary 2 Can redefine inherited member functions To perform differently in derived class Protected members in base class: Can be accessed "by name" in derived class member functions Overloaded assignment operator not inherited But can be invoked from derived class Constructors are not inherited Are invoked from derived class’s constructor Copyright © 2010 Pearson Addison-Wesley. All rights reserved.