Polymorphism & Pointers

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Object Oriented Programming with Java
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
Polymorphism, Virtual Methods and Abstract Classes.
DERIVED CLASSES AND INHERITANCE Moshe Fresko Bar-Ilan University Object Oriented Programing
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
 Simple payroll application that polymorphically calculates the weekly pay of several different types of employees using each employee’s Earnings method.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Taken from slides of Starting Out with C++ Early Objects Seventh Edition.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
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.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
Object-oriented programming: C++ class A { private: …… // can be accessd by A protected: …… // can be accessed by A and // its derived classes public:
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.
TCP1201 OOPDS Lecture 4 1. Learning Objectives  To understand upcasting & downcasting  To understand static polymorphism and dynamic polymorphism 
CIS 3301 C# Lesson 7 Introduction to Classes. CIS 3302 Objectives Implement Constructors. Know the difference between instance and static members. Understand.
Polymorphism and Virtual Functions. Topics Polymorphism Virtual Functions Pure Virtual Functions Abstract Base Classes Virtual Destructors V-Tables Run.
Topic 1 11/18/2015. Submitted By: Arslan Ali : Roll No: 6703 Ali Ahmad : Roll No: 6710 Ameer Moavia : Roll No: 6734 Abdul Manam : Roll No: 6738 Agha Waqas.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
ISBN Object-Oriented Programming Chapter Chapter
Chapter -6 Polymorphism
Inheritance and Composition Reusing the code and functionality Unit - 04.
Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see.
Glenn Stevenson CSIS 113A MSJC CSIS 123A Lecture 3 Vectors.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Abstract Classes & Object Slicing. Object Slicing.
Programming in C++ Michal Brabec Petr Malý. Inheritance class derived-class: access-specifier base-class {} Base & Derived class Implementation inheritance.
CPSC 252Inheritance II Page 1 Inheritance & Pointers Consider the following client code: const int MAXCLOCKS = 2; Clock* clockPtr[ MAXCLOCKS ]; clockPtr[0]
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Abstract Class Abstract Class is a class which contains atleast one Pure Virtual function in it. Abstract classes are used to provide an Interface for.
Classes and Inheritance
Polymorphism, Virtual Methods and Abstract Classes
7. Inheritance and Polymorphism
CS3340 – OOP and C++ L. Grewe.
Anatomy of a class Part I
Polymorphism, Virtual Methods and Abstract Classes
Dynamic Memory CSCE 121 J. Michael Moore.
Operator Overloading CSCE 121 J. Michael Moore
Iteration CSCE 121 J. Michael Moore.
Sorting CSCE 121 J. Michael Moore
CSCE 121- Spring 2016 J. Michael Moore
Polymorphism CSCE 121 J. Michael Moore.
Stream States CSCE 121 J. Michael Moore
Abstract Classes AKEEL AHMED.
Virtual Functions Department of CSE, BUET Chapter 10.
Anatomy of Polymorphism
Dynamic Memory Copy Challenge
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Exceptions CSCE 121 J. Michael Moore
Input Validation CSCE 121 J. Michael Moore
Standard Template Library Model
Static in Classes CSCE 121 J. Michael Moore.
Function Overloading CSCE 121 J. Michael Moore
STL: Traversing a Vector
Anatomy of Inheritance
Inheritance in Graphics
CISC/CMPE320 - Prof. McLeod
Destructor CSCE 121 J. Michael Moore.
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Today’s Objectives 10-Jul-2006 Announcements Quiz #3
Anatomy of Polymorphism
Polymorphism & Pointers
Dynamic Memory Copy Challenge
C++ Polymorphism Reference and pointer implicit type casting
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.
Presentation transcript:

Polymorphism & Pointers CSCE 121 J. Michael Moore Including slides created by Carlos Soto.

Polymorphism Challenge We cannot create an instance of an abstract class. However, we can have a pointer to an abstract class. If we need a collection of a base class, have a vector of pointers to objects of derived classes in the heap. vector<BaseClass*> v;

Another Polymorphism Challenge If we have a vector of pointers, we can iterate through them and call any virtual functions and the derived version will be executed. What if we want to access attributes/methods specific to the child class? Cast to derived class (use dynamic_cast) How do we know if we are casting to the correct class? No built in way. You can create a virtual function or protected data member that holds the information.

Pointer conversion (implicit) int main () { // AbstractParentClass myObj; Not Allowed! AbstractParentClass* myObj; // totally ok ChildClass anObj; myObj = &anObj; // ChildClass* converted // to AbstractParentClass* }

Polymorphism example int main () { AbstractParentClass* myObj; ChildClass anObj; myObj = &anObj; myObj->virtualFunction(); } ChildClass’s virtualFunction() is called, even though myObj is an AbstractParentClass*

Polymorphism in a Collection int main () { Vector<AbstractParentClass*> things; // fill things with pointers to // various derived classes for (int i = 0; i < things.size(); ++i) { cout << things.at(i)->virtualFunction(); } Regardless of which derived class is at a particular index, the correct version of virtualFunction() is called.