CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]

Slides:



Advertisements
Similar presentations
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Chapter 1 OO using C++. Abstract Data Types Before we begin we should know how to accomplish the goal of the program We should know all the input and.
Polymorphism, Virtual Methods and Abstract Classes.
© Copyright Eliyahu Brutman Programming Techniques Course Version 1.0.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
1 Lecture 4 Further OO Concepts I - Polymorphism Overview  What is polymorphism?  Why polymorphism is wonderful?  Why is Upcasting useful?  What is.
ECE122 L22: Polymorphism Using Inheritance April 26, 2007 ECE 122 Engineering Problem Solving with Java Lecture 22 Polymorphism using Inheritance.
Inheritance, Polymorphism, and Virtual Functions
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
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 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12: Adding Functionality to Your Classes.
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.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Run-Time Type Identification Jim Fawcett CSE687 – Object Oriented Design Spring 2007.
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 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
1 Chapter 14 Object-Oriented Software Development Dale/Weems.
1 Inheritance. 2 Why use inheritance?  The most important aspect of inheritance is that it expresses a relationship between the new class and the base.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Chapter 10 Inheritance and Polymorphism
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Chapter -6 Polymorphism
CSCI-383 Object-Oriented Programming & Design Lecture 24.
Peyman Dodangeh Sharif University of Technology Fall 2014.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 3 is due Sunday, the 8 th at 7pm. Problems with assn 3? Discuss at your team meeting tonight.
CMSC 202 Polymorphism. 10/20102 Topics Binding (early and late) Upcasting and downcasting Extensibility The final modifier with  methods  classes.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 20 - Virtual Functions Outline 20.1Introduction 20.2Type Fields and switch Statements 20.3Virtual.
Inheritance - 3. Virtual Functions Functions defined as virtual are ones that the base expects its derived classes may redefine. Functions defined as.
 2006 Pearson Education, Inc. All rights reserved Object-Oriented Programming: Polymorphism.
1 Inheritance and Polymorphism Chapter Getting Started Continue the Cat Management example from previous presentation.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
C++ How to Program, 7/e. © by Pearson Education, Inc. All Rights Reserved.2.
CSCI-383 Object-Oriented Programming & Design Lecture 17.
The Slicing Problem CS240 Computer Science II. Some relationship between base and derived classes: data members Derived class inherits data members of.
A First Book of C++ Chapter 12 Extending Your Classes.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CMSC 202 Polymorphism.
7. Inheritance and Polymorphism
Class A { public : Int x; A()
Polymorphism.
Polymorphism & Virtual Functions
Designing for Inheritance
Virtual Functions Department of CSE, BUET Chapter 10.
Inheritance, Polymorphism, and Virtual Functions
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Programming II Polymorphism A.AlOsaimi.
Polymorphism CMSC 202, Version 4/02.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Lecture 6: Polymorphism
Static Binding Static binding chooses the function in the class of the base class pointer, ignoring any versions in the class of the object actually.
Computer Science II for Majors
Presentation transcript:

CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0] = new Clock( 14, 12, 0 ); clockPtr[1] = new AlarmClock( 8, 54, 12 ); clockPtr[0]->display(); cout << endl << endl; clockPtr[1]->display(); cout << endl; Output: 14:12:00 08:54:12 Alarm is not set Alarm time 00:00:00

CPSC 252Inheritance II Page 2 Interestingly, the output is exactly what we wanted. Even though both pointers are declared to be pointers to Clock objects, the statement: clockPtr[1]->display(); invokes AlarmClock::display(). In order to understand why this occurs, we need to introduce some new terminology. Static binding or compile-time binding causes the compiler to determine which version of an overridden function to call when the program is compiled. When the determination of which version of an overridden function to call occurs while the program is running we call this dynamic binding or run-time binding.

CPSC 252Inheritance II Page 3 Suppose that objectPtr is a pointer to a base class and that objectRef is a reference to a base class. Further suppose that func() is overridden in the derived class. When an expression of the type objectPtr->func() or objectRef.func() is encountered: - if func() is declared to be virtual then the type of object pointed to by objectPtr (or objectRef ) determines which version of func() to call. This determination must occur at run-time - dynamic binding. - if func() is not declared to be virtual then the type of pointer (or reference variable) determines which version of func() to call. This determination occurs at compile- time – static binding.

CPSC 252Inheritance II Page 4 Note: dynamic binding occurs only when a pointer or reference variable is used to invoke a virtual member function. If we assign an object of the derived class to a variable of the base class, then we get the object-slicing that we talked about previously.

CPSC 252Inheritance II Page 5 Returning to our Clock example, notice that the display() function is declared to be virtual in the base class. Hence, dynamic binding occurs when the statement: clockPtr[1]->display(); is encountered. Even though clockPtr was declared to be a pointer to a Clock object, it is AlarmClock::display() that is invoked! This occurs because clockPtr is actually pointing to an object of type AlarmClock. The fact that a pointer to the base class can in fact point to any object in its inheritance hierarchy is called polymorphism which literally means, many forms. All object-oriented programming languages must support polymorphism.

CPSC 252Inheritance II Page 6 Inheritance, Pointers and Type Casting Consider the statement: clockPtr[1] = new AlarmClock( 17, 34, 0 ); We are assigning the address of an AlarmClock object to a pointer to a Clock – this is OK, an AlarmClock is-a Clock. It is possible, but not safe, to cast a pointer to a Clock object into a pointer to an AlarmClock object as follows: AlarmClock* myAlarmClockPtr; myAlarmClockPtr = ( AlarmClock* ) new Clock( 7, 0, 0 ); Now we have to be careful not to do something like: myAlarmClockPtr->setAlarm( true ); because myAlarmClockPtr is actually pointing to a Clock object.

CPSC 252Inheritance II Page 7 Inheritance, Pointers and Type Casting This kind of assignment is only safe if the object really is an AlarmClock, which it could be: AlarmClock* myAlarmClockPtr; Clock *myClockPtr; myClockPtr = new AlarmClock(7, 0, 0); // This “forgets” that we have an AlarmClock // and allows us to treat it as a Clock myAlarmClockPtr = ( AlarmClock* ) myClockPtr; // This “gets back” the AlarmClock