Polymorphism & Pointers

Slides:



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

Object Oriented Programming with Java
Brown Bag #3 Return of the C++. Topics  Common C++ “Gotchas”  Polymorphism  Best Practices  Useful Titbits.
OOP Abstraction Classes Class Members: Properties & Methods Instance (object) Encapsulation Interfaces Inheritance Composition Polymorphism Using Inheritance.
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Polymorphism, Virtual Methods and Abstract Classes.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Object-Oriented Design. Method for designing computer programs Consider “objects” interacting in the program –Example: a zoo.
1 CSE 303 Lecture 24 Inheritance in C++, continued slides created by Marty Stepp
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.
Static and Dynamic Behavior CMPS Power of OOP Derives from the ability of objects to change their behavior dynamically at run time. Static – refers.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 8: Class Relationships Data Abstraction & Problem Solving.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
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.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
Chapter -6 Polymorphism
Polymorphism, Virtual Methods and Interfaces Version 1.1.
Abstract Classes & Object Slicing. Pure Virtual Functions Virtual Function : Child class may override, if working with pointer/reference, check to see.
Class Inheritance Inheritance as an is-a relationship Public derive one class from another Protected access Initializer lists in constructor Upcasting.
OOP, Inheritance and Polymorphism Lecture 6. Object relations  Inheritance is ‘a kind of’, ‘a type of’ e.g. a revolver is a type of gun  Aggregation.
 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.
C#.Net Software Development Version 1.0. Overview Inheritance Member Access Constructors Polymorphism (Name Hiding) Multilevel Hierarchy Virtual and VTable.
C++ How to Program, 7/e.  There are cases in which it’s useful to define classes from which you never intend to instantiate any objects.  Such classes.
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
Ch 10- Advanced Object-Oriented Programming Features
7. Inheritance and Polymorphism
Anatomy of a class Part I
Polymorphism, Virtual Methods and Abstract Classes
Inheritance Often, software encapsulates multiple concepts for which some attributes/behaviors overlap E.g. A computer (role-playing) game has: Monsters:
Operator Overloading CSCE 121 J. Michael Moore
Searching CSCE 121 J. Michael Moore.
Iteration CSCE 121 J. Michael Moore.
Programming with ANSI C ++
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.
Anatomy of Polymorphism
Inheritance Virtual Functions, Dynamic Binding, and Polymorphism
Standard Template Library Model
Pointers Dr. Bhargavi Goswami Department of Computer Science
Function Overloading CSCE 121 J. Michael Moore
STL: Traversing a Vector
Anatomy of Inheritance
Inheritance in Graphics
Event Driven Programming Anatomy – Handle
Polymorphism & Pointers
Understanding Polymorphism
Anatomy of Polymorphism
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Chapter 11 Class Inheritance
Object Slicing & Dynamic Casts
Operator Overloading CSCE 121 Based on Slides created by Carlos Soto.
Anatomy of a class Part I
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 Including slides created by Carlos Soto and J. Michael Moore

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 collection of pointers to objects of derived classes. e.g.: vector<BaseClass*> v; BaseClass* A = new BaseClass[SIZE];

Another Polymorphism Challenge If we have a collection 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) { things.at(i)->virtualFunction(); } Regardless of which derived class is at a particular index, the correct version of virtualFunction() is called.