POLYMORPHISM ( in C++) POLYMORPHISM ( in C++). Presentation Outline Polymorphism Definition Types – Compile time and Run time polymorphism Function overloading.

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.
Module 8 “Polymorphism and Inheritance”. Outline Understanding Inheritance Inheritance Diagrams Constructors in Derived Classes Type Compatibility Polymorphism.
Chapter 14: Overloading and Templates C++ Programming: Program Design Including Data Structures, Fifth Edition.
:PRESENTED BY: SHUBHAM KANDHARKAR SHUBHAM BHAGEL NIKET KOTHARI MANJEET SINGH ALHAWAT PRANAV TOSHNIWAL.
Inheritance, Polymorphism, and Virtual Functions
Chapter 15: Operator Overloading
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.
Operator overloading Object Oriented Programming.
VIRTUAL FUNCTIONS AND DYNAMIC POLYMORPHISM. *Polymorphism refers to the property by which objects belonging to different classes are able to respond to.
Chapter 12: Adding Functionality to Your Classes.
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.
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
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.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Operator Overloading. Introduction It is one of the important features of C++ language  Compile time polymorphism. Using overloading feature, we can.
Unit: 7 Operator Overloading and Type Conversions Course: MBATech Trimester: II.
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.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and 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.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
CONSTRUCTOR AND DESTRUCTORS
Operator overloading: Overloading a unary operator is similar to overloading a binary operator except that there is one Operand to deal with. When you.
Function Overloading and References
Chapter -6 Polymorphism
Unit VI polymorphism. Md.Jaffar Sadiqsumalatha Polymorphism refers to : one name, many forms. Polymorphism is of two types:  Compile time polymorphism.
CS212: Object Oriented Analysis and Design Lecture 11: Operator Overloading-I.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
 Virtual Function Concepts: Abstract Classes & Pure Virtual Functions, Virtual Base classes, Friend functions, Static Functions, Assignment & copy initialization,
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 07: Object Oriented Programming:2014 Object-Oriented Programming in C++ Operator.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Operator Overloading Ritika Sharma.
Friend functions.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance and Polymorphism
FUNCTIONS In C++.
Introduction Rules Types Programs
Operator Overloading.
Class A { public : Int x; A()
Inheritance, Polymorphism, and Virtual Functions
ATS Application Programming: Java Programming
Polymorphism Lec
Modern Programming Tools And Techniques-I Inheritance
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Inheritance Basics Programming with Inheritance
Operator Overloading
Inheritance, Polymorphism, and Virtual Functions
Polymorphism Polymorphism
Operator overloading Dr. Bhargavi Goswami
Computer Programming with JAVA
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.
Inheritance C++ strongly support the reusability, that is one class has been written and tested already it can be adapted or used to create a new class.
Inheritance.
VIRTUAL FUNCTIONS RITIKA SHARMA.
Inheriting Multiple Base Classes
Chapter 6 Polymorphism.
C++ Polymorphism Reference and pointer implicit type casting
C++ Object Oriented 1.
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 ( in C++) POLYMORPHISM ( in C++)

Presentation Outline Polymorphism Definition Types – Compile time and Run time polymorphism Function overloading Operator overloading Operator overriding Virtual function

Polymorphism – Definition Poly  many, Morph  form Polymorphism  many forms Ability to perform different tasks or operations at different instances of time. Example: Person  student in school/college  child to his parents  brother to his siblings  etc., Single person exhibiting different forms at different instances

Types

Function Overloading Same function performing different operations or tasks Function name is same Function definitions are different The correct function is invoked by matching the corresponding function prototypes

Example Function calls cout<<“area of square”<<area(5); cout<<“area of rect”<<area(10,20); cout<<“area of circle”<<area(3.5); Function definitions float area (float r) {return (3.14*r*);} int area (int s) {return (s*s);} int area (int l, int b) {return (l*b);}

Function Overloading Contd., The compiler is aware of exactly which function to be called So function overloading achieves compile time polymorphism or Early binding

Operator Overloading Defining an additional task to (some of) the built-in operators Keyword: operator For unary operators no arguments For binary operators one argument

Operator Overloading Contd., Syntax for unary operator: returntype operator symbol() {statements;} Syntax for binary operator: returntype operator symbol(datatype arg) {statements;} Overloaded operators are invoked by: symbol x or x symbol (say –x or x – ) for unary x symbol y (x – y) for binary

Example unary operator (minus) class unaryopr { public: int x,y; void getdata (int a, int b) {x=a; y=b;} void display () {cout<<x<< “and”<<y;} unaryopr operator – () {x=-x; y=-y;} }; void main() {unaryopr U; U.getdata(10,-20); cout<<“nos before overloading”; U.display(); -U; cout<<“nos after overloading”; U.display(); getch(); }

Output: nos before overloading nos after overloading Example unary operator (minus) contd.,

class complex //Addition of complex nos {int a,b; public: void getdata(int x, int y) {a=x; b=y;} complex operator +(complex ob) { complex t; t.a = a + ob.a; t.b = b + ob.b; return t; } void display() {cout<<a<<“+”<<b<<“i”<<“\n”;} }; Example binary operator (plus)

Example binary operator (plus) contd., void main() {complex obj1,obj2,res; obj1.getdata(4,5); obj2.getdata(2,2); cout<<“Input values:”<<“\n”; obj1.display(); obj2.display(); res=obj1+obj2; cout<<“Result”<<“\n”; res.display(); getch(); } Output: Input values: 4+5i 2+2i Result: 6+7i

Limitations of Operator Overloading Following operators cannot be overloaded sizeof operator Scope resolution operator Conditional operator Membership (dot) operator Pointer to member operator(.*)

Function Overriding Redefining a base class function in the derived class with same name and arguments Overridden functions are accessed using pointer referencing the objects instead of direct access through objects If not done so, the overridden function in the derived class will hide the corresponding function in the base class

Example class BC {public: void show() {cout<<“show base”;} }; class DC : public BC {public: void show() {cout<<“show derived”;} }; void main() {BC *bptr; BC base; bptr=&base; bptr  show(); //calls BC function DC derived; bptr=&derived; bptr  show(); //calls BC function } Output: show base show base

Virtual Function Though base class pointer points derived class object, it always executes base class function Because the compiler ignores the contents of pointer and chooses the function that matches the type of the pointer If so, then how polymorphism is achieved? It is achieved using virtual functions Keyword virtual is used before the function in the base class

Virtual Function Contd., By doing so, the function is called based on the object referenced by the pointer instead of pointer type at run time The keyword virtual indicates the compiler to perform late binding (run time) on that function Hence run time polymorphism is achieved

Example class BC {public: virtual void show() {cout<<“show base”;} }; class DC : public BC {public: void show() {cout<<“show derived”;} }; void main() {BC *bptr; BC base; bptr=&base; bptr  show(); //calls BC function DC derived; bptr=&derived; bptr  show(); //calls DC function } Output: show base show derived

Thank you