Polymorphism Lec 21-22.

Slides:



Advertisements
Similar presentations
Derived Classes. C++ 2 Outline  Definition  Virtual functions  Virtual base classes  Abstract classes. Pure virtual functions.
Advertisements

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.
Inheritance in C++ Multiple Base Classes Inheritance By Nouf Aljaffan Reference: Learn C++ from the master, Schildt, second Ed.
Inheritance in C++ Multiple Base Classes Inheritance By: Nouf Aljaffan Edited by : Nouf Almunyif.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
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.
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 4 Inheritance Bernard Chen Spring Objective IS-A relationships and the allowable changes for derived classes The concept of polymorphism.
Inheritance in C++ CS-1030 Dr. Mark L. Hornick.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
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.
INHERITANCE IN C++ 3 CSC1201: PROGRAMMING LANGUAGE 2 ASEEL ALHADLAQ_KSU 1.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Polymorphism &Virtual Functions
Polymorphism &Virtual Functions 1. Polymorphism in C++ 2 types ▫Compile time polymorphism  Uses static or early binding  Example: Function and operator.
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
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.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
OOP and Dynamic Method Binding Chapter 9. Object Oriented Programming Skipping most of this chapter Focus on 9.4, Dynamic method binding – Polymorphism.
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.
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.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
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
Inheritance in C++ 2 CSC1201: Programming Language 2 1Aseel AlHadlaq_KSU.
Overview of C++ Polymorphism
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.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
Polymorphism Lecture - 9.
CSC241 Object-Oriented Programming (OOP) Lecture No. 17.
Computer Science Department Inheritance & 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,
CS1201: Programming Language 2 Classes and objects Inheritance Nouf Aljaffan Edited by : Nouf Almunyif.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS 3370 – C++ Object-oriented Programming
7. Inheritance and Polymorphism
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Object-Oriented Programming
CS212: Object Oriented Analysis and Design
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
CS1201: Programming Language 2
HYBRID INHERITANCE : AMBIGUITY REMOVAL
Abstract Classes AKEEL AHMED.
Virtual Functions Department of CSE, BUET Chapter 10.
Polymorphism Polymorphism
Programming II Polymorphism A.AlOsaimi.
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Pointers Dr. Bhargavi Goswami Department of Computer Science
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Multiple Base Classes Inheritance
Overview of C++ Polymorphism
VIRTUAL FUNCTIONS RITIKA SHARMA.
Inheriting Multiple Base Classes
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:

Polymorphism Lec 21-22

Types of Polymorphism Compile time Uses static or early binding. Example: Function overloading and Operator overloading. Run time Uses dynamic or late binding. Virtual functions.

Run-time Polymorphism

Pointers to Derived Classes Base class pointers can point to derived class objects. Example: Let there be two classes, class base { … }; class derived : public base { … }; Then, base *p1; derived d_obj; p1 = &d_obj;

Contd… A base class pointer pointing to a derived class object Has knowledge only of the base class. Knows nothing about the members added by the derived class. Thus, Members of the derived object that were inherited from the base class can only be accessed.

Example #include<iostream> using namespace std; class base { public: void show() { cout << "base\n"; } }; class derived : public base { { cout << "derived\n"; } int main() { base b1; b1.show(); // base derived d1; d1.show(); // derived base *pb = &b1; pb->show(); // base pb = &d1; }

Virtual Functions A member function declared within a base class are redefined by a derived class. (Overriding) Implement the "one interface, multiple methods" philosophy that underlies polymorphism. The keyword virtual is used to designate a member function as virtual. Support run-time polymorphism with the help of base class pointers.

Contd… While redefining a virtual function in a derived class, The function signature must match the original function present in the base class. The keyword virtual is not needed (but can be specified). The "virtual"-ity of the member function continues along the inheritance chain. A class that contains a virtual function is referred to as a polymorphic class.

Example – 1 #include<iostream> using namespace std; class base { public: virtual void show() { cout << "base\n"; } }; class derived : public base { void show() { cout << "derived\n"; } int main() { base b1; b1.show(); // base derived d1; d1.show(); // derived base *pb = &b1; pb->show(); // base pb = &d1; pb->show(); // derived }

Example – 2 int main() { base *pb; d1 od1; d2 od2; int n; #include<iostream> using namespace std; class base { public: virtual void show() { cout << "base\n"; } }; class d1 : public base { void show() { cout << "derived – 1\n"; } }; class d2 : public base { { cout << "derived – 2\n"; } }; int main() { base *pb; d1 od1; d2 od2; int n; cin >> n; if (n % 2) pb = &od1; else pb = &od2; pb->show(); return 0; }

Virtual Destructors Constructors cannot be virtual, but destructors can be virtual. It ensures that the derived class destructor is called when a base class pointer is used while deleting a dynamically created derived class object.

Example (Non-virtual Destructor) #include<iostream> using namespace std; class base { public: ~base() { cout << "destructing base\n"; } }; class derived : public base { public: ~derived() { cout << "destructing derived\n"; } }; int main() { base *p = new derived; delete p; return 0; } Output destructing base

Example (Virtual Destructor) #include<iostream> using namespace std; class base { public: virtual ~base() { cout << "destructing base\n"; } }; class derived : public base { public: ~derived() { cout << "destructing derived\n"; } }; int main() { base *p = new derived; delete p; return 0; } Output destructing derived destructing base

More About Virtual Functions To omit the body of a virtual function in a base class, use pure virtual functions. virtual returnType functionName(paramList) = 0; This makes a class an abstract class, i.e. cannot create any objects of such classes. Derived classes should override such functions , otherwise they become abstract too. Pointer to an abstract class can be still be created.

Abstract Class &Pure Virtual Function class A \\ Abstract Class { virtual void disp() = 0; \\Pure virtual function }; Not possible- A obj;

Derived class should contain definition of the pure virtual function, else it would also become an abstract class class A { virtual void disp()= 0; }; class B: public class A { virtual void disp() = 0;

#include <iostream> using namespace std; class A { public: virtual void disp()= 0; }; class B: public A virtual void disp() = 0; class C: public B { public: void disp() {cout<<"Welcome";} }; int main() { C obj; obj.disp(); return 0; }

Pointer to Abstract Class #include <iostream> using namespace std; class A { public: virtual void disp()= 0; }; class C: public A void disp() {cout<<"Welcome";} int main() { A *p; C obj; p= &obj; p->disp(); return 0; } OUTPUT Welcome

Applying Polymorphism Early binding Normal functions, overloaded functions. Nonvirtual member and friend functions. Resolved at compile time. Very efficient. But lacks flexibility. Late binding Virtual functions accessed via a base class pointer. Resolved at run-time. Quite flexible during run-time. But has run-time overhead; slows down program execution.