Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 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.
Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
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,
Before Introducing Inheritance … Here is another way to re-use what have been developed: This is known as the object composition! A real-world example.
Creating Classes from Other Classes Chapter 2 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12A Separate Compilation and Namespaces For classes this time.
CS102--Object Oriented Programming Lecture 7: – Inheritance Copyright © 2008 Xiaoyan Li.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Inheritance.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 17 – Payroll Application: Introducing Inheritance.
CS102--Object Oriented Programming Lecture 8: – More about Inheritance When to use inheritance Relationship between classes Rules to follow Copyright ©
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Inheritance.
CS102--Object Oriented Programming Lecture 10: – Abstract Classes Copyright © 2008 Xiaoyan Li.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Chapter 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Chapter 12: Adding Functionality to Your Classes.
Chapter 15 Inheritance. Slide Overview 15.1 Inheritance Basics 15.2 Inheritance Details 15.3 Polymorphism.
C++ Object Oriented 1. Class and Object The main purpose of C++ programming is to add object orientation to the C programming language and classes are.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Chapter 8 More Object Concepts
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CMSC 202 Inheritance I Class Reuse with Inheritance.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Inheritance Initialization & Destruction of Derived Objects Protected Members Non-public Inheritance Virtual Function Implementation Virtual Destructors.
Chapter 9 Separate Compilation and Namespaces. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Separate Compilation (9.1)
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 12 Separate Compilation and Namespaces.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Separate Compilation and Namespaces.
Inheritance Chapter 7. Outline Inheritance Basics Programming with Inheritance Dynamic Binding and Polymorphism.
Inheritance in C++ Bryce Boe 2012/08/28 CS32, Summer 2012 B.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 15 Inheritance. Chapter 15 Inheritance.
7. Inheritance and Polymorphism
Andy Wang Object Oriented Programming in C++ COP 3330
COMP 2710 Software Construction Inheritance
Chapter 15 & additional topics
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 14 Inheritance Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Inheritance Basics Programming with Inheritance
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
Learning Objectives Inheritance Virtual Function.
Polymorphism Polymorphism
Computer Programming with JAVA
Inheritance Chapter 15 & additional topics.
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Chapter 15 & additional topics
Chapter 8: Class Relationships
Chapter 7 Inheritance.
Presentation transcript:

Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance

Slide Overview 15.1 Inheritance Basics 15.2 Inheritance Details 15.3 Polymorphism- Just a brief mention - we'll return to this if there is time

Copyright © 2008 Pearson Addison-Wesley. All rights reserved Inheritance

Slide Inheritance Basics Inheritance is the process by which a new class, called a derived class, is created from another class, called the base class A derived class automatically has all the member variables and functions of the base class A derived class automatically has all the member variables and functions of the base class A derived class can have additional member variables and/or member functions A derived class can have additional member variables and/or member functions The derived class is a child of the base or parent class The derived class is a child of the base or parent class

Slide Employee Classes To design a record-keeping program with records for salaried and hourly employees… Salaried and hourly employees belong to a class of people who share the property "employee" Salaried and hourly employees belong to a class of people who share the property "employee" A subset of employees are those with a fixed wage A subset of employees are those with a fixed wage Another subset of employees earn hourly wages Another subset of employees earn hourly wages All employees have a name and SSN Functions to manipulate name and SSN are the same for hourly and salaried employees Functions to manipulate name and SSN are the same for hourly and salaried employees

Slide 15- 6

Slide 15- 7

Slide 15- 8

Slide Function print_check Function print_check will have different definitions to print different checks for each type of employee An Employee object lacks sufficient information to print a check An Employee object lacks sufficient information to print a check Each derived class will have sufficient information to print a check Each derived class will have sufficient information to print a check

HourlyEmployee is derived from Class Employee HourlyEmployee inherits all member functions and member variables of Employee HourlyEmployee inherits all member functions and member variables of Employee The class definition begins class HourlyEmployee : public Employee The class definition begins class HourlyEmployee : public Employee :public Employee shows that HourlyEmployee is derived from class Employee HourlyEmployee declares additional member variables wage_rate and hours HourlyEmployee declares additional member variables wage_rate and hours Slide Class HourlyEmployee

Slide

Slide Inherited Members A derived class inherits all the members of the parent class The derived class does not re-declare or re- define members inherited from the parent, except… The derived class does not re-declare or re- define members inherited from the parent, except… The derived class re-declares and re-defines member functions of the parent class that will have a different definition in the derived class The derived class re-declares and re-defines member functions of the parent class that will have a different definition in the derived class The derived class can add member variables and functions The derived class can add member variables and functions

Any member functions added in the derived class are defined in the implementation file for the derived class Definitions are not given for inherited functions that are not to be changed Definitions are not given for inherited functions that are not to be changed The HourlyEmployee class is defined in next two slides Slide Implementing a Derived Class

Slide

Slide

The class SalariedEmployee is also derived from Employee Function print_check is redefined to have a meaning specific to salaried employees Function print_check is redefined to have a meaning specific to salaried employees SalariedEmployee adds a member variable salary SalariedEmployee adds a member variable salary The interface for SalariedEmployee is found in the next slide. The slide after that contains the implementation Slide Class SalariedEmployee

Slide

Slide

Slide Parent and Child Classes Recall that a child class automatically has all the members of the parent class The parent class is an ancestor of the child class The child class is a descendent of the parent class The parent class (Employee) contains all the code common to the child classes You do not have to re-write the code for each child You do not have to re-write the code for each child

Slide Derived Class Types An hourly employee is an employee In C++, an object of type HourlyEmployee can be used where an object of type Employee can be used In C++, an object of type HourlyEmployee can be used where an object of type Employee can be used An object of a class type can be used wherever any of its ancestors can be used An object of a class type can be used wherever any of its ancestors can be used An ancestor cannot be used wherever one of its descendents can be used An ancestor cannot be used wherever one of its descendents can be used

Slide A base class constructor is not inherited in a derived class The base class constructor can be invoked by the constructor of the derived class The base class constructor can be invoked by the constructor of the derived class The constructor of a derived class begins by invoking the constructor of the base class in the initialization section: HourlyEmployee::HourlyEmployee : Employee( ), wage_rate( 0), hours( ) { //no code needed } The constructor of a derived class begins by invoking the constructor of the base class in the initialization section: HourlyEmployee::HourlyEmployee : Employee( ), wage_rate( 0), hours( ) { //no code needed } Any Employee constructor could be invoked Derived Class Constructors

Slide Default Initialization If a derived class constructor does not invoke a base class constructor explicitly, the base class default constructor will be used If class B is derived from class A and class C is derived from class B When a object of class C is created When a object of class C is created The base class A's constructor is the first invoked Class B's constructor is invoked next C's constructor completes execution

Slide Private is Private A member variable (or function) that is private in the parent class is not accessible to the child class The parent class member functions must be used to access the private members of the parent The parent class member functions must be used to access the private members of the parent This code would be illegal: void HourlyEmployee::print_check( ) { net_pay = hours * wage_rage; This code would be illegal: void HourlyEmployee::print_check( ) { net_pay = hours * wage_rage; net_pay is a private member of Employee!

Slide The protected Qualifier protected members of a class appear to be private outside the class, but are accessible by derived classes If member variables name, net_pay, and ssn are listed as protected (not private) in the Employee class, this code, illegal on the previous slide, becomes legal: HourlyEmployee::print_check( ) { net_pay = hours * wage_rage; If member variables name, net_pay, and ssn are listed as protected (not private) in the Employee class, this code, illegal on the previous slide, becomes legal: HourlyEmployee::print_check( ) { net_pay = hours * wage_rage;

Slide Programming Style Using protected members of a class is a convenience to facilitate writing the code of derived classes. Protected members are not necessary Derived classes can use the public methods of their ancestor classes to access private members Derived classes can use the public methods of their ancestor classes to access private members Many programming authorities consider it bad style to use protected member variables

When defining a derived class, only list the the inherited functions that you wish to change for the derived class The function is declared in the class definition The function is declared in the class definition HourlyEmployee and SalariedEmployee each have their own definitions of print_check HourlyEmployee and SalariedEmployee each have their own definitions of print_check The next two slides demonstrate the use of the derived classes defined in earlier displays. Slide Redefinition of Member Functions

Slide

Slide

Slide Redefining or Overloading A function redefined in a derived class has the same number and type of parameters The derived class has only one function with the same name as the base class The derived class has only one function with the same name as the base class An overloaded function has a different number and/or type of parameters than the base class The derived class has two functions with the same name as the base class The derived class has two functions with the same name as the base class One is defined in the base class, one in the derived class

Slide Function Signatures A function signature is the function's name with the sequence of types in the parameter list, not including any const or '&' An overloaded function has multiple signatures An overloaded function has multiple signatures Some compilers allow overloading based on including const or not including const

Slide Access to a Redefined Base Function When a base class function is redefined in a derived class, the base class function can still be used To specify that you want to use the base class version of the redefined function: HourlyEmployee sally_h; sally_h.Employee::print_check( ); To specify that you want to use the base class version of the redefined function: HourlyEmployee sally_h; sally_h.Employee::print_check( );

Slide Section 15.1 Conclusion Can you Explain why the declaration for get_name is not part of the definition of SalariedEmployee? Explain why the declaration for get_name is not part of the definition of SalariedEmployee? Give a definition for a class TitledEmployee derived from class SalariedEmployee with one additional string called title? Add two member functions get_title and set_title. It should redefine set_name. Give a definition for a class TitledEmployee derived from class SalariedEmployee with one additional string called title? Add two member functions get_title and set_title. It should redefine set_name.

Copyright © 2008 Pearson Addison-Wesley. All rights reserved Inheritance Details

Slide Inheritance Details Some special functions are, for all practical purposes, not inherited by a derived class Some of the special functions that are not effectively inherited by a derived class include Some of the special functions that are not effectively inherited by a derived class includeDestructors Copy constructors The assignment operator

Slide Copy Constructors and Derived Classes If a copy constructor is not defined in a derived class, C++ will generate a default copy constructor This copy constructor copies only the contents of member variables and will not work with pointers and dynamic variables This copy constructor copies only the contents of member variables and will not work with pointers and dynamic variables The base class copy constructor will not be used The base class copy constructor will not be used

Slide Operator = and Derived Classes If a base class has a defined assignment operator = and the derived class does not: C++ will use a default operator that will have nothing to do with the base class assignment operator C++ will use a default operator that will have nothing to do with the base class assignment operator

Slide Destructors and Derived Classes A destructor is not inherited by a derived class The derived class should define its own destructor

Slide The Assignment Operator In implementing an overloaded assignment operator in a derived class: It is normal to use the assignment operator from the base class in the definition of the derived class's assignment operator It is normal to use the assignment operator from the base class in the definition of the derived class's assignment operator Recall that the assignment operator is written as a member function of a class Recall that the assignment operator is written as a member function of a class

Slide The Operator = Implementation This code segment shows how to begin the implementation of the = operator for a derived class: Derived& Derived::operator= (const Derived& rhs) { Base::operator=(rhs) This line handles the assignment of the inherited member variables by calling the base class assignment operator This line handles the assignment of the inherited member variables by calling the base class assignment operator The remaining code would assign the member variables introduced in the derived class The remaining code would assign the member variables introduced in the derived class

Slide The Copy Constructor Implementation of the derived class copy constructor is much like that of the assignment operator: Derived::Derived(const Derived& object) :Base(object), {…} Invoking the base class copy constructor sets up the inherited member variables Invoking the base class copy constructor sets up the inherited member variables Since object is of type Derived it is also of type Base

Slide Destructors in Derived Classes If the base class has a working destructor, defining the destructor for the defined class is relatively easy When the destructor for a derived class is called, the destructor for the base class is automatically called When the destructor for a derived class is called, the destructor for the base class is automatically called The derived class destructor need only use delete on dynamic variables added in the derived class, and data they may point to The derived class destructor need only use delete on dynamic variables added in the derived class, and data they may point to

Slide Destruction Sequence If class B is derived from class A and class C is derived from class B… When the destructor of an object of class C goes out of scope When the destructor of an object of class C goes out of scope The destructor of class C is called Then the destructor of class B Then the destructor of class A Notice that destructors are called in the reverse order of constructor calls Notice that destructors are called in the reverse order of constructor calls

Slide Section 15.2 Conclusion Can you List some special functions that are not inherited by a derived class? List some special functions that are not inherited by a derived class? Write code to invoke the base class copy constructor in defining the derived class's copy constructor? Write code to invoke the base class copy constructor in defining the derived class's copy constructor?

Copyright © 2008 Pearson Addison-Wesley. All rights reserved Polymorphism

Slide Polymorphism Polymorphism refers to the ability to associate multiple meanings with one function name using a mechanism called late binding Polymorphism is a component of the philosophy of object oriented programming and we will come back to it if there is time.

Slide Chapter End