Chapter 14 Inheritance. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 14-2 Learning Objectives Inheritance Basics Derived classes, with.

Slides:



Advertisements
Similar presentations
2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Inheritance.
Advertisements

2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
Final and Abstract Classes
Classes and Objects in Java
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 11 Separate Compilation and Namespaces. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Separate Compilation.
Chapter 8 Operator Overloading, Friends, and References.
Chapter 17 Linked Data Structures. Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives Nodes and Linked Lists Creating,
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
© 2008 Pearson Addison Wesley. All rights reserved Chapter Seven Costs.
Copyright © 2003 Pearson Education, Inc. Slide 1 Computer Systems Organization & Architecture Chapters 8-12 John D. Carpinelli.
Copyright © 2003 Pearson Education, Inc. Slide 1.
Copyright © 2011, Elsevier Inc. All rights reserved. Chapter 6 Author: Julia Richards and R. Scott Hawley.
Author: Julia Richards and R. Scott Hawley
UNITED NATIONS Shipment Details Report – January 2006.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chapter 24 Lists, Stacks, and Queues
1 Linked Lists III Template Chapter 3. 2 Objectives You will be able to: Write a generic list class as a C++ template. Use the template in a test program.
Chapter 1 Object Oriented Programming 1. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Object Oriented Programming with Java
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
Analyzing Genes and Genomes
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Chapter 12 OOP: Creating Object- Oriented Programs Copyright © 2011 by The McGraw-Hill Companies, Inc. All Rights Reserved. McGraw-Hill.
PSSA Preparation.
Essential Cell Biology
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 13 - Exception Handling Outline 13.1 Introduction 13.2 Exception-Handling Overview 13.3 Other.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Energy Generation in Mitochondria and Chlorplasts
Chapter 9: Using Classes and Objects. Understanding Class Concepts Types of classes – Classes that are only application programs with a Main() method.
Inheritance Juan Marquez 12_inheritance.ppt
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.
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.
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.
Chapter 15 Inheritance. Chapter 15 Inheritance.
COMP 2710 Software Construction Inheritance
Inheritance 2nd Lecture
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Comp 249 Programming Methodology
Inheritance I Class Reuse with Inheritance
Learning Objectives Inheritance Virtual Function.
Lecture 22 Inheritance Richard Gesick.
Inheritance 2nd Lecture
CMSC 202 Inheritance.
Class Reuse with Inheritance
Inheritance 2nd Lecture
CMSC 202 Inheritance II.
Chapter 7 Inheritance.
Presentation transcript:

Chapter 14 Inheritance

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives 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

Copyright © 2006 Pearson Addison-Wesley. All rights reserved 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 its functionality for its appropriate use

Copyright © 2006 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 classs: Member variables Member functions Can then add additional member functions and variables

Copyright © 2006 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 © 2006 Pearson Addison-Wesley. All rights reserved Derived Classes Dont "need" type of generic "employee" Since no ones just an "employee" General concept of employee helpful! All have names All have social security numbers Associated functions for these "basics" are same among all employees So "general" class can contain all these "things" about employees

Copyright © 2006 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 wont have "objects" of this class, however

Copyright © 2006 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 © 2006 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 © 2006 Pearson Addison-Wesley. All rights reserved Display 14.3 Interface for the Derived Class HourlyEmployee (1 of 2)

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

Copyright © 2006 Pearson Addison-Wesley. All rights reserved HourlyEmployee Class Interface Note definition begins same as any other #ifndef structure Includes required libraries Also includes employee.h! And, the heading: class HourlyEmployee : public Employee { … Specifies "publicly inherited" from Employee class

Copyright © 2006 Pearson Addison-Wesley. All rights reserved HourlyEmployee Class Additions 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 © 2006 Pearson Addison-Wesley. All rights reserved HourlyEmployee Class Redefinitions HourlyEmployee redefines: printCheck() member function This "overrides" the printCheck() function implementation from Employee class Its definition must be in HourlyEmployee classs implementation As do other member functions declared in HourlyEmployees interface New and "to be redefined"

Copyright © 2006 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 thats a parent of a parent … Descendant class Opposite of ancestor

Copyright © 2006 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 © 2006 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 © 2006 Pearson Addison-Wesley. All rights reserved Another HourlyEmployee Constructor A second constructor: HourlyEmployee::HourlyEmployee() : Employee(), wageRate(0), hours(0) { //Deliberately empty } Default version of base class constructor is called (no arguments) Should always invoke one of the base classs constructors

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

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Pitfall: Base Class Private Data Derived class "inherits" private member variables But still cannot directly access them Not even through derived class member functions! Private member variables can ONLY be accessed "by name" in member functions of the class theyre defined in

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Pitfall: Base Class Private Member Functions Same holds for base class member functions Cannot be accessed outside interface and implementation of base class Not even in derived class member function definitions

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Pitfall: Base Class Private Member Functions Impact Larger impact here vs. member variables Member variables can be accessed indirectly via accessor or mutator member functions Member functions simply not available This is "reasonable" Private member functions should be simply "helper" functions Should be used only in class theyre defined

Copyright © 2006 Pearson Addison-Wesley. All rights reserved The protected: Qualifier New classification of class members Allows access "by name" in derived class But nowhere else Still no access "by name" in other classes In class its defined acts like private Considered "protected" in derived class To allow future derivations Many feel this "violates" information hiding

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Redefinition of Member Functions Recall interface of derived class: Contains declarations for new member functions Also contains declarations for inherited member functions to be changed Inherited member functions NOT declared: Automatically inherited unchanged Implementation of derived class will: Define new member functions Redefine inherited functions as declared

Copyright © 2006 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 © 2006 Pearson Addison-Wesley. All rights reserved A Functions Signature Recall definition of a "signature": Functions name Sequence of types in parameter list Including order, number, types Signature does NOT include: Return type const keyword &

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Accessing Redefined Base Function When redefined in derived class, base classs definition not "lost" Can specify its use: Employee JaneE; HourlyEmployee SallyH; JaneE.printCheck(); calls Employees printCheck function SallyH.printCheck(); calls HourlyEmployee printCheck function SallyH.Employee::printCheck(); Calls Employees printCheck function! Not typical here, but useful sometimes

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Functions Not Inherited All "normal" functions in base class are inherited in derived class Exceptions: Constructors (weve 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 © 2006 Pearson Addison-Wesley. All rights reserved Assignment Operators and Copy Constructors Recall: overloaded assignment operators and copy constructors NOT inherited But can be used in derived class definitions Typically MUST be used! Similar to how derived class constructor invokes base class constructor

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Assignment Operator Example Given "Derived" is derived from "Base": Derived& Derived::operator =(const Derived & rightSide) { Base::operator =(rightSide); … } Notice code line Calls assignment operator from base class This takes care of all inherited member variables Would then set new variables from derived class…

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Copy Constructor Example Consider: Derived::Derived(const Derived& Object) : Base(Object), … {…} After : is invocation of base copy constructor Sets inherited member variables of derived class object being created Note Object is of type Derived; but its also of type Base, so argument is valid

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Destructors in Derived Classes If base class destructor functions correctly Easy to write derived class destructor When derived class destructor is invoked: Automatically calls base class destructor! So no need for explicit call So derived class destructors need only be concerned with derived class variables And any data they "point" to Base class destructor handles inherited data automatically

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Destructor Calling Order Consider: class B derives from class A class C derives from class B A B C When object of class C goes out of scope: Class C destructor called 1 st Then class B destructor called Finally class A destructor is called Opposite of how constructors are called

Copyright © 2006 Pearson Addison-Wesley. All rights reserved "Is a" vs. "Has a" Relationships Inheritance Considered an "Is a" class relationship e.g., An HourlyEmployee "is a" Employee A Convertible "is a" Automobile A class contains objects of another class as its member data Considered a "Has a" class relationship e.g., One class "has a" object of another class as its data

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Protected and Private Inheritance New inheritance "forms" Both are rarely used Protected inheritance: class SalariedEmployee : protected Employee {…} Public members in base class become protected in derived class Private inheritance: class SalariedEmployee : private Employee {…} All members in base class become private in derived class

Copyright © 2006 Pearson Addison-Wesley. All rights reserved Multiple Inheritance Derived class can have more than one base class! Syntax just includes all base classes separated by commas: class derivedMulti : public base1, base2 {…} Possibilities for ambiguity are endless! Dangerous undertaking! Some believe should never be used Certainly should only be used be experienced programmers!

Copyright © 2006 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 © 2006 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 classs constructor