Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Slides:



Advertisements
Similar presentations
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Inheritance. Many objects have a hierarchical relationship –Examples: zoo, car/vehicle, card game, airline reservation system Inheritance allows software.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Sixth Edition Chapter 15: Polymorphism,
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation - a language mechanism for restricting access to some.
© 2006 Pearson Addison-Wesley. All rights reserved9 A-1 Chapter 9 Advanced Java Topics CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck Spring 2007.
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
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.
Lecture 6: Polymorphism - The fourth pillar of OOP - 1.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Polymorphism &Virtual Functions
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Chapter 11 Inheritance and Composition. Chapter Objectives Learn about inheritance Learn about subclasses and superclasses Explore how to override the.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Inheritance and Polymorphism.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
C/C++ 3 Yeting Ge. Static variables Static variables is stored in the static storage. Static variable will be initialized once. 29.cpp 21.cpp.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Object Oriented Programming
Class Relationships And Reuse Interlude 4 Data Structures and Problem Solving with C++: Walls and Mirrors, Frank Carrano, © 2012.
ISBN Object-Oriented Programming Chapter Chapter
Chapter -6 Polymorphism
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Presented by Ted Higgins, SQL Server DBA An Introduction to Object – Oriented Programming.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
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,
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance and Polymorphism
Class A { public : Int x; A()
Object-Oriented Programming
Inheritance, Polymorphism, and Virtual Functions
Polymorphism.
CS212: Object Oriented Analysis and Design
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Virtual Functions Department of CSE, BUET Chapter 10.
Advanced Java Topics Chapter 9
Inheritance, Polymorphism, and Virtual Functions
Polymorphism Polymorphism
Pointers Dr. Bhargavi Goswami Department of Computer Science
Chapter 8: Class Relationships
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Computer Science II for Majors
Presentation transcript:

Chapter 15 – Inheritance, Virtual Functions, and Polymorphism

Inheritance Name of another way in which classes and objects relate Base class Derived class When object of derived class declared Object of base class is automatically created and contained within derived class object Lesson 15.1

Image of base class object creation when derived class object instantiated Lesson 15.1

Defining a Base Class class Base { private: type base_priv_dat; protected: type base_prot_dat; public: type base_function_name ( ); }; Lesson 15.1

Defining a Derived Class class Derived: public Base { private: int derived_priv_dat; public: void derived_function_name ( ); }; Lesson 15.1

Interaction Between Base and Derived Class Objects Private base class members are private member of base class only Protected base class members are protected members of both derived and base classes Public base class members are public members of derived and base classes Derived class members convey no special access privileges to base class functions Lesson 15.1

Relationship Between Base and Derived Objects Base object private data private data protected data public functions public functions derived_object.derived_function( ) derived_object.base_function( )

No Base Class Object Name Use only derived object name to access both derived and base class functions Derived class takes precedence over-riding base class function Lesson 15.1

Comparison of Types of Class and Object Interactions Client class object Server class Friend class object Granting class On all examples, upper boxes represent private data members and lower boxes represent public member functions. Derived class object Base class object Component class object Composite Class Object Lesson 15.1

Private and Protected Inheritance class Derived : private Base public and protected members of Base become private members of Derived private members of Base remain private to Base class Derived : protected Base public and protected members of Base become public and protected member of Derived Lesson 15.1

Order of Constructor and Destructor Function Calls Both base and derived constructors called when derived class object instantiated Bass class constructor called first, derived constructor second Derived class destructor called first, base class destructor second Order in which objects declared determines order of constructor calls Lesson 15.2

Other Uses of Inheritance Well suited to extending or customizing existing code Can make full use of base class features and add specific features Can modify base classes without requiring derived classes to be modified Allows base classes to remain up to date Lesson 15.1

Explicit Call to Base Class Constructor Derived object (value1, value2); Derived :: Derived (type derived_var, type base_var) :derived_member (derived_var), Base (base_var) Constructor Lesson 15.2

Effect of Inheritance Levels Object for lowest level class contains sub-objects of all other classes Order of calling constructors is from top down in inheritance graph Pass data one step or level at a time Functions can be overridden in any class down inheritance graph Can assign derived class objects to base class objects, but not reverse Lesson 15.3

Effect of Inheritance Levels Object for lowest level class contains sub-objects of all other classes Planet object (no name) Celestial_body object (no name) Earth object Lesson 15.3

Multiple Inheritance Class can inherit from more than one class directly When one class has "is a" relationship with more than one class Lesson 15.4

Virtual Classes Class which we are not allowed to create independent objects Used as base class for other classes for which we do want independent objects C++ automatically makes class virtual when member of class is pure virtual function virtual type function (type, type) = 0; Lesson 15.5

Pointers and Inheritance Can declare base class type pointer variable and assign address of derived class object Any address of object down inheritance graph Lesson 15.5

Binding Association of function call with a function Early binding Occurs during compilation Also called static or compile-time binding Late binding Occurs during execution Also called dynamic or run-time binding Lesson 15.5

Polymorphism Achieved by creating and using virtual functions Code making function call does not explicitly state which function it is calling C++ decides during execution which is correct function

Summary Learned how to: Create an inheritance hierarchy Use inheritance as a form of class interaction Make a virtual class Use polymorphism in an engineering program