Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Introduction to Programming Lecture 39. Copy Constructor.
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
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.
CSI 3125, Preliminaries, page 1 Polymorphism, Virtual function.
Inheritance, Polymorphism, and Virtual Functions
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.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
VIRTUAL FUNCTIONS AND DYNAMIC POLYMORPHISM. *Polymorphism refers to the property by which objects belonging to different classes are able to respond to.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
Pointer Data Type and Pointer Variables
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
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.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
Learners Support Publications Classes and Objects.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
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.
CSCI-383 Object-Oriented Programming & Design Lecture 18.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
Virtual FunctionstMyn1 Virtual Functions A virtual function is declared in a base class by using the keyword virtual. A function that you declare as virtual.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter -6 Polymorphism
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
CS212: Object Oriented Analysis and Design Lecture 16: Runtime Polymorphism.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Object Oriented Programming Elhanan Borenstein Lecture #7.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
Learners Support Publications Constructors and Destructors.
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.
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.
Constructors and Destructors
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Andy Wang Object Oriented Programming in C++ COP 3330
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
CS212: Object Oriented Analysis and Design
ATS Application Programming: Java Programming
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Abstract Classes AKEEL AHMED.
Inheritance, Polymorphism, and Virtual Functions
Polymorphism Polymorphism
Constructors and Destructors
Classes and Objects.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
VIRTUAL FUNCTIONS RITIKA SHARMA.
Submitted By : Veenu Saini Lecturer (IT)
Chapter 6 Polymorphism.
COP 3330 Object-oriented Programming in C++
Chapter 11 Class Inheritance
Computer Science II for Majors
Presentation transcript:

Polymorphism

Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking. Also known as compile time polymorphism, simply means that an object is bound to its function call at compile time

How do we use member function show() to print the values of objects of both classes A and B? – Prototype of show() is same in both cases. So static binding does not apply. – Can use class resolution operator(::) Runtime polymorphism – Selecting appropriate member function while the program is running – Virtual function for this purpose – a.k.a late binding or dynamic binding class A { int x; public: void show(){…} }; class B: public A { int y; public: void show(){…} };

Pointers to Objects Consider the statement: item x; What does it mean ? We can define a pointer of type item as item *itemptr; Object pointers are useful in creating objects at runtime We can also use an object pointer to access public members of an object

To declare item variable x and pointer to x: item x; item *ptr ptr= &x; //ptr initialized with address of x Member function of item can be referred to as: x.getdata(10, 20.5) x.show(); OR ptr->getdata()10,20.5); ptr->show(); OR (*ptr).show(); We can also create the objects using pointers and new operator: item *ptr=new item; This allocates enough memory for the data members in the object structure and assigns the address of the memory space to ptr. ptr can be used to refer the members as: ptr->show(); We can create array of objects using pointers: item *ptr = new item[10]; class item { int code; float prices; public: void getdata(int a, float b) { code=a; price=b; } void show(int a, int b) { cout<<“Code: “<<code<<“\n”; cout<<“Price: “<<price<<“\n\n”; } };

Code Src: pointer_to_obj.cpp

this Pointer Every object has a special pointer “this “ which points to the object itself This pointer is accessible to all the members of the class but not to any static members of the class Can be used to find the address of the object in which the function is a member

this Pointer We create many instances of a class in a program How do functions get to know which object has called them The member functions are passes the pointers of the calling object. qMyInt.Get() &qMyInt

this Pointer The pointer is passed with the name “this” “this” points to qMyInt Actually additional pointer parameter of the class type that the function belongs to memfun(Myclass* const this, arg1,…) which we write as memfun(arg1,..)

Pointers to derived classes Pointers can be used not only to base objects but also to objects of derived classes Pointers to objects of a base are type-compatible with pointers to objects of a derived class Therefore, a single pointer variable can be made to point to objects belonging to different classes But there is a problem in using pointer to access the public members of the derived class Only those members that are inherited from base class can be accessed Those members which originally belong to derived class cannot be accessed.

Pointers to derived classes In case a member of derived class has the same name as one of the members of base class, then any reference to that member by pointer will always access the base class member.

Virtual Functions Polymorphism refers to the property by which objects belonging to different classes are able to respond to the same message, but in different forms An essential requirement of polymorphism is therefore the ability to refer to objects without any regard to their classes. This necessitates the use of single pointer variable to refer to the objects of different classes When we use same function name in both base and derived classes, the function in base class is declared as virtual. When a function is made virtual, c++ determines which function to use at run time based on the type of object pointed by the base pointer rather than the type of the pointer

Virtual functions We must access virtual function through the use of pointer declared as a pointer to the base class. Why can’t we use the object name with dot operator the same way as any other member function to call the virtual function? Yes, we can but run time polymorphism is achieved only when a virtual function is accessed through a pointer to the base class.

Rules for Virtual Functions The virtual function must be member of some class, cannot be static member They are accessed by using object pointer A virtual function can be a friend of another class The prototypes of base class version of virtual function and all derived class version must be identical We cannot have virtual constructors but can have virtual destructors While a base class pointer can point to any type of derived object, the reverse is not true When a base pointer points to a derived class, incrementing or decrementing it will not make it point to the next object of derived class

Pure Virtual Function A virtual function is usually declared in base class and redefined in the derived classes. The function in base class only serves as place holders, also are “empty”. If we define such functions as: – virtual void display()=0; Such functions are called pure virtual functions. The derived classes should either define the function or re-declare it as pure virtual. The class containing pure virtual function cannot be used to declare any object of its own. Such classes are called abstract base classes

Early Binding and Late Binding early-binding-and-late-binding/