Jeff West - Quiz Section 12

Slides:



Advertisements
Similar presentations
Recursion 2014 Spring CS32 Discussion Jungseock Joo.
Advertisements

Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Review of Inheritance. 2 Several Levels of Inheritance Base Class B Derived class D Derived class D1.
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
C++ Classes & Data Abstraction
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Object-Oriented Programming in C++ Lecture 7 Polymorphism.
. Virtual Classes & Polymorphism. Example (revisited) u We want to implement a graphics system u We plan to have lists of shape. Each shape should be.
Polymorphism From now on we will use g++!. Example (revisited) Goal: Graphics package Handle drawing of different shapes Maintain list of shapes.
OOP Spring 2007 – Recitation 71 Object Oriented Programming Spring 2006 Recitation 8.
OOP Etgar 2008 – Recitation 71 Object Oriented Programming Etgar 2008 Recitation 7.
12/08/08MET CS Fall Polymorphism 10. Polymorphism Goal: Goal: Create methods that can be invoked with all object types, base as well as.
Abstract Classes An abstract class is a base class that will never have an object instantiated from it. –Abstract classes are used only for inheritance,
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.
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.
Dr. Ahmad R. Hadaegh A.R. Hadaegh California State University San Marcos (CSUSM) Page 1 Virtual Functions Polymorphism Abstract base classes.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Inheritance Joe Meehean. Object Oriented Programming Objects state (data) behavior (methods) identity (allocation of memory) Class objects definition.
1 Inheritance We are modeling the operation of a transportation company that uses trains and trucks to transfer goods. A suitable class hierarchy for the.
CSC 205 Java Programming II Polymorphism. Topics Polymorphism The principle of substitution Dynamic binding Object type casting Abstract class The canonical.
Inheritance. Lecture contents Inheritance Class hierarchy Types of Inheritance Derived and Base classes derived class constructors protected access identifier.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Abstract ClassestMyn1 Abstract Classes Virtual functions in a base class must be defined unless they are declared to be pure virtual (abstract) functions.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
1 Lecture 6: Polymorphism - The fourth pillar of OOP -
Overview of C++ Polymorphism
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Learners Support Publications Constructors and Destructors.
ECE 264 Object-Oriented Software Development Instructor: Dr. Honggang Wang Spring 2013 Lecture 19: Abstract Classes.
258 3/30/98 CSE 143 Object-Oriented Programming [Chapter 11]
CSCI-383 Object-Oriented Programming & Design Lecture 17.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
DEVRY COMP 220 iLab 7 Polymorphism Lab Report and Source Code Check this A+ tutorial guideline at
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Virtual Function and Polymorphism
CMSC 202 Polymorphism.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
CS 3370 – C++ Object-oriented Programming
Advanced Program Design with C++
7. Inheritance and Polymorphism
Inheritance and Big O And your first reading assignment
Class A { public : Int x; A()
Object-Oriented Programming & Design Lecture 18 Martin van Bommel
Polymorphism.
Inheritance: hiding methods
Polymorphism Lec
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Polymorphism CT1513.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Constructors and Destructors
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Polymorphism Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition, by Kernighan.
CISC/CMPE320 - Prof. McLeod
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Overview of C++ Polymorphism
An Example of Inheritance
Jim Fawcett CSE687 – Object Oriented Design Spring 2014
Inheritance & Destructors
C++ Polymorphism Reference and pointer implicit type casting
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:

Jeff West - Quiz Section 12 CSE 143 Section AD Quiz Section 12 7/26/2001 Jeff West - Quiz Section 12

Jeff West - Quiz Section 12 Announcements Quiz at the end of section on Inheritance and Dynamic Dispatch 7/26/2001 Jeff West - Quiz Section 12

Jeff West - Quiz Section 12 Substitution A derived class can ALWAYS be substituted for a base class. However, the behavior of a substituted class depends on the type of the methods you’re calling. 7/26/2001 Jeff West - Quiz Section 12

What does this print to the screen? (No virtual functions) void Shape::draw() { cout << “Hi, I’m a shape!”; } void Rectangle::draw() { cout << “Hi, I’m a rectangle!”; void drawShape(Shape myShape) { myShape.draw(); Shape myShape; Circle myRectangle(GREEN, 20, 10); myShape = myRectangle; drawShape(myShape); drawShape(myRectangle); 7/26/2001 Jeff West - Quiz Section 12

And this??? (No virtual functions) void Shape::draw() { cout << “Hi, I’m a shape!”; } void Rectangle::draw() { cout << “Hi, I’m a rectangle!”; void drawShape(Shape* myShape) { myShape->draw(); Shape* myShape; Circle* myRectangle = new Rectangle(GREEN, 20, 10); myShape = myRectangle; drawShape(myShape); drawShape(myRectangle); 7/26/2001 Jeff West - Quiz Section 12

What if we want the drawShape function to… We can make it so that the drawShape function would call the draw() method of the run-time type that is passed in! To do this, we need only to declare the function virtual in the base class (Shape in our example)… 7/26/2001 Jeff West - Quiz Section 12

What does this print to the screen? (Virtual functions!) void Shape::draw() { cout << “Hi, I’m a shape!”; } void Rectangle::draw() { cout << “Hi, I’m a rectangle!”; void drawShape(Shape myShape) { myShape.draw(); Shape myShape; Circle myRectangle(GREEN, 20, 10); myShape = myRectangle; drawShape(myShape); drawShape(myRectangle); 7/26/2001 Jeff West - Quiz Section 12

Jeff West - Quiz Section 12 To take advantage… To take advantage of virtual functions you need to use pointers to objects. Otherwise, information is lost! The most common use for all of this is to have one large array of one object type (e.g. vehicles) that have many “sub-types” that behave differently (trucks, cars, busses)… 7/26/2001 Jeff West - Quiz Section 12

And this??? (Virtual functions!) void Shape::draw() { cout << “Hi, I’m a shape!”; } void Rectangle::draw() { cout << “Hi, I’m a rectangle!”; void drawShape(Shape* myShape) { myShape->draw(); Shape* myShape; Circle* myRectangle = new Rectangle(GREEN, 20, 10); myShape = myRectangle; drawShape(myShape); drawShape(myRectangle); 7/26/2001 Jeff West - Quiz Section 12

Pure Virtual Functions (1) What if you never want the ability to create a shape, because it doesn’t make sense for a shape to “draw” itself… You’d call Shape an abstract class, intended only to be a skeleton for derived classes. Make functions that need to be in a child class but can’t be defined in the Shape class pure virtual! 7/26/2001 Jeff West - Quiz Section 12

Pure Virtual Functions (2) Class Shape { public: … virtual void draw() = 0; }; 7/26/2001 Jeff West - Quiz Section 12

Jeff West - Quiz Section 12 But Why??? Without having some sort of draw() function in EVERY Shape class instance, you couldn’t: void Rectangle::draw() { cout << “Hi, I’m a rectangle!”; } void drawShape(Shape* myShape) { myShape->draw(); Shape* myShape; Circle* myRectangle = new Rectangle(GREEN, 20, 10); myShape = myRectangle; drawShape(myShape); drawShape(myRectangle); 7/26/2001 Jeff West - Quiz Section 12

A bit of cleaning up to do… Always make your destructors virtual! This ensures that all dynamic memory in inherited classes is taken care of. Remember how constructors of derived classes always call the constructors of their base class before executing? Destructors always call the destructors of their base class after executing! 7/26/2001 Jeff West - Quiz Section 12