Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1.

Slides:



Advertisements
Similar presentations
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Inheritance and Polymorphism- I Math 130 Introduction to Computer Programming Lecture #27 Monday, November 5, 2007.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 8.1 – 8.5.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 11 More.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Inheritance, Polymorphism, and Virtual Functions.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 11: More About.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Inheritance, Polymorphism, and Virtual Functions
Inheritance Chapter 11.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 13: Introduction to Classes.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Using work from: Starting Out with C++ Early Objects Eighth Edition.
Specialization and Inheritance Chapter 8. 8 Specialization Specialized classes inherit the properties and methods of the parent or base class. A dog is.
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Chapter 4 Applications of the Derivative.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
Chapter 11 More about classes and OOP. Relationships Between Classes Possible relationships – Access ("uses-a") – Ownership/Composition ("has-a") – Inheritance.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
11-1 Chapter.9 Classes & Objects: Inheritance –What Is Inheritance? –Calling the Superclass Constructor –Overriding Superclass Methods –Protected Members.
AN INTRODUCTION TO INHERITANCE. In your group Define 5 attributes that you would use to describe persons. Define 3 different attributes that can describe.
Chapter Topics Chapter 10 discusses the following main topics:
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Inheritance Starting Out with Java: From Control Structures.
Inheriatance. 9-2 What is Inheritance? Generalization vs. Specialization Real-life objects are typically specialized versions of other more general objects.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
More on Inheritance Chapter 11 Continued. Reminders Overloading – different signatures Overriding – same signatures Preventing overriding – use final.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
11-2  What Is Inheritance?  Calling the Superclass Constructor  Overriding Superclass Methods  Protected Members  Chains of Inheritance  The Object.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Learning Objectives  Inheritance  Virtual Function.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 15 Inheritance.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 7: Introduction to Classes and Objects Starting Out with C++ Early.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
© 2010 Pearson Addison-Wesley. All rights reserved. AN INTRODUCTION TO INHERITANCE.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
© 2004 Pearson Addison-Wesley. All rights reserved April 10, 2006 Inheritance (part 2) ComS 207: Programming I (in Java) Iowa State University, SPRING.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Outline Creating Subclasses Overriding Methods Class Hierarchies Inheritance.
Spring 2008 Mark Fontenot CSE Honors Principles of Computer Science I Note Set 8 1.
1 CSE 2341 Object Oriented Programming with C++ Note Set #12.
Part -1 © by Pearson Education, Inc. All Rights Reserved.
Chapter 11 Inheritance. Contents I.What Is Inheritance? II. Calling the Superclass Constructor III. Overriding Superclass Methods IV. Protected Members.
1 CSE 2341 Object Oriented Programming with C++ Note Set #13.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Inheritance, Polymorphism, and Virtual Functions
Inheritance & Polymorphism
by Tony Gaddis and Godfrey Muganda
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
An introduction to inheritance
Starting Out with Java: From Control Structures through Objects
Inheritance Using work from:
Inheritance, Polymorphism, and Virtual Functions
C++ Inheritance.
Chapter 11: Inheritance Starting Out with Java: From Control Structures through Objects Third Edition by Tony Gaddis.
Inheritance in C++ Inheritance Protected Section
Programming in C# CHAPTER 5 & 6
Presentation transcript:

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley What Is Inheritance? 15.1

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-2 What Is Inheritance? Provides a way to create a new class from an existing class The new class is a specialized version of the existing class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-3 Example: Insects

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-4 The "is a" Relationship Inheritance establishes an "is a" relationship between classes. –A poodle is a dog –A car is a vehicle –A flower is a plant –A football player is an athlete

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-5 Inheritance – Terminology and Notation Base class (or parent) – inherited from Derived class (or child) – inherits from the base class Notation: class Student // base class {... }; class UnderGrad : public student {// derived class... };

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-6 Back to the ‘is a’ Relationship An object of a derived class 'is a(n)' object of the base class Example: –an UnderGrad is a Student –a Mammal is an Animal A derived object has all of the characteristics of the base class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-7 What Does a Child Have? An object of the derived class has: all members defined in child class all members declared in parent class An object of the derived class can use: all public members defined in child class all public members defined in parent class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Protected Members and Class Access 15.2

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-9 Protected Members and Class Access protected member access specification: like private, but accessible by objects of derived class Class access specification: determines how private, protected, and public members of base class are inherited by the derived class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Access Specifiers 1)public – object of derived class can be treated as object of base class (not vice- versa) 2)protected – more restrictive than public, but allows derived classes to know details of parents 3)private – prevents objects of derived class from being treated as objects of base class.

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Inheritance vs. Access private: x protected: y public: z private: x protected: y public: z private: x protected: y public: z Base class members x is inaccessible private: y private: z x is inaccessible protected: y protected: z x is inaccessible protected: y public: z How inherited base class members appear in derived class private base class protected base class public base class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Inheritance vs. Access private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); class Test : public Grade When Test class inherits from Grade class using public class access, it looks like this: private members: int numQuestions: float pointsEach; int numMissed; public members: Test(int, int); void setScore(float); float getScore(); float getLetter();

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Inheritance vs. Access (2) private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); When Test class inherits from Grade class using protected class access, it looks like this: private members: int numQuestions: float pointsEach; int numMissed; public members: Test(int, int); protected members: void setScore(float); float getScore(); float getLetter(); class Test : protected Grade

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley More Inheritance vs. Access (3) private members: int numQuestions: float pointsEach; int numMissed; void setScore(float); float getScore(); float getLetter(); public members: Test(int, int); private members: char letter; float score; void calcGrade(); public members: void setScore(float); float getScore(); char getLetter(); class Grade private members: int numQuestions; float pointsEach; int numMissed; public members: Test(int, int); When Test class inherits from Grade class using private class access, it looks like this: class Test : private Grade

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Constructors and Destructors in Base and Derived Classes 15.3

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Constructors and Destructors in Base and Derived Classes Derived classes can have their own constructors and destructors When an object of a derived class is created, the base class’s constructor is executed first, followed by the derived class’s constructor When an object of a derived class is destroyed, its destructor is called first, then that of the base class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Constructors and Destructors in Base and Derived Classes

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 15-18

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Program 5-14 (Continued)

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Base Class Constructor Allows selection between multiple base class constructors Specify arguments to base constructor on derived constructor heading: Square::Square(int side) : Rectangle(side, side) Can also be done with inline constructors Must be done if base class has no default constructor

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Passing Arguments to Base Class Constructor Square::Square(int side):Rectangle(side,side) derived class constructorbase class constructor derived constructor parameter base constructor parameters

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Redefining Base Class Functions 15.4

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Redefining Base Class Functions Redefining function: function in a derived class that has the same name and parameter list as a function in the base class Typically used to replace a function in base class with different actions in derived class

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Redefining Base Class Functions Not the same as overloading – with overloading, parameter lists must be different Objects of base class use base class version of function; objects of derived class use derived class version of function

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Base Class Note setScore function

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Derived Class Redefined setScore function

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley From Program 15-6

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Problem with Redefining Consider this situation: –Class BaseClass defines functions x() and y(). x() calls y(). –Class DerivedClass inherits from BaseClass and redefines function y(). –An object D of class DerivedClass is created and function x() is called. –When x() is called, which y() is used, the one defined in BaseClass or the the redefined one in DerivedClass ?

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Problem with Redefining BaseClass DerivedClass void X(); void Y(); DerivedClass D; D.X(); Object D invokes function X() In BaseClass. Function X() invokes function Y() in BaseClass, not function Y() in DerivedClass, because function calls are bound at compile time. This is static binding.

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Hierarchies 15.5

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Hierarchies A base class can be derived from another base class.

Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Class Hierarchies Consider the GradedActivity, FinalExam, PassFailActivity, PassFailExam hierarchy in Chapter 15.