Polymorphism.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

F UNCTION O VERLOADING Chapter 5 Department of CSE, BUET 1.
V IRTUAL F UNCTIONS Chapter 10 Department of CSE, BUET 1.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Function Part II: Some ‘advanced’ concepts on functions.
Templates. Objectives At the conclusion of this lesson, students should be able to Explain how function templates are used Correctly create a function.
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.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
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.
Polymorphism Lecture-10. Print A Cheque A Report A Photograph PrintCheque() PrintReport() PrintPhoto() Printing.
Object Oriented Programming with C++/ Session 6 / 1 of 44 Multiple Inheritance and Polymorphism Session 6.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
ECE122 Feb. 22, Any question on Vehicle sample code?
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Chapter 3 Inheritance and Polymorphism Goals: 1.Superclasses and subclasses 2.Inheritance Hierarchy 3.Polymorphism 4.Type Compatibility 5.Abstract Classes.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
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.
Lecture 19 CIS 208 Wednesday, April 06, Welcome to C++ Basic program style and I/O Class Creation Templates.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Chapter -6 Polymorphism
3. The Nuts and Bolts of C++ Computer Programming 3. The Nuts and Bolts of C++ 1 Learning the C++ language 3. The Nuts and Bolts of C++ 16 September 2008.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني Destructors The destructor fulfills the opposite functionality. It is automatically called when an object.
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Learners Support Publications Constructors and Destructors.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.
Constructors and Destructors
Templates.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 9 Type Conversions
Pointers, Polymorphism, and Memory Allocation
Command Line Arguments
Function Overloading.
Polymorphism &Virtual Functions
Class A { public : Int x; A()
Inheritance and Run time Polymorphism
Chapter 5 Function Basics
Polymorphism & Virtual Functions
C Basics.
ATS Application Programming: Java Programming
group work #hifiTeam
Polymorphism Lec
LEC Default Function Arguments, Ambiguity in Function Overloading and Operator Overloading.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Extra.
CS1201: Programming Language 2
Contents Introduction to Constructor Characteristics of Constructor
Name: Rubaisha Rajpoot
Virtual Functions Department of CSE, BUET Chapter 10.
More ‘concepts’ on Function
Polymorphism Polymorphism
Constructors and Destructors
9: POLYMORPHISM Programming Technique II (SCSJ1023) Jumail Bin Taliba
Polymorphism CMSC 202, Version 4/02.
Virtual Functions Polymorphism is supported by C++ both at compile time and at run time. Compile-time polymorphism is achieved by overloading functions.
Constructors and Destructors
Method of Classes Chapter 7, page 155 Lecture /4/6.
VIRTUAL FUNCTIONS RITIKA SHARMA.
Inheriting Multiple Base Classes
Chapter 6 Polymorphism.
Functions Imran Rashid CTO at ManiWeber Technologies.
COP 3330 Object-oriented Programming in C++
CMSC 202 Lesson 6 Functions II.
More ‘concepts’ on Function
Presentation transcript:

Polymorphism

Polymorphism The word polymorphism means having many forms. In simple words, we can define polymorphism as the ability of a message to be displayed in more than one form. Real life example of polymorphism, a person at a same time can have different characteristic. Like a man at a same time is a father, a husband, a employee. So a same person posses have different behavior in different situations. This is called polymorphism.

Types of Poymorphism Compile Time Polymorphism Run Time Polymorphism

Compile Time Polymorphism Early binding refers to events that occur at compile time. In essence, early binding occurs when all information needed to call a function is known at compile time. (Put differently, early binding means that an object and a function call are bound during compilation). Examples of early binding include normal function calls (including standard library functions), overloaded function calls, and overloaded operators. The main advantage to early binding is efficiency. Because all information necessary to call a function is determined at compile time, these types of function calls are very fast.

Run Time Polymorphism The opposite of early binding is late binding. As it relates to C++, late binding refers to function calls that are not resolved until run time. As function calls are not determined at compile time, the object and the function are not linked until run time.

Run Time Polymorphism The main advantage to late binding is flexibility. Unlike early binding, late binding allows you to create programs that can respond to events occurring while the program executes without having to create a large amount of "contingency code." Keep in mind that because a function call is not resolved until run time, late binding can make for somewhat slower execution times. Virtual functions are used to achieve late binding.

Function Overloading Function overloading is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either different types of parameters or a different number of parameters. It is only through these differences that the compiler knows which function to call in any given situation.

Example #include <iostream> using namespace std; int myfunc(int i); // these differ in types of parameters double myfunc(double i); int main() { cout << myfunc(10) << " "; // calls myfunc(int i) cout << myfunc(5.4); // calls myfunc(double i) return 0; }

double myfunc(double i) { return i; } int myfunc(int i) Output: 10 5.4

Example 2 #include <iostream> using namespace std; int myfunc(int i); // these differ in number of parameters int myfunc(int i, int j); int main() { cout << myfunc(10) << " "; // calls myfunc(int i) cout << myfunc(4, 5); // calls myfunc(int i, int j) return 0; }

int myfunc(int i) { return i; } int myfunc(int i, int j) return i*j; Output: 10 20

Key Points The key point about function overloading is that the functions must differ in regard to the types and/or number of parameters. Two functions differing only in their return types cannot be overloaded. For example, this is an invalid attempt to overload myfunc( ): int myfunc(int i); float myfunc(int i); // Error: differing return types are insufficient when overloading. Sometimes, two function declarations will appear to differ, when in fact they do not. For example, consider the following declarations. void f(int *p); void f(int p[]); // error, *p is same as p[] Remember, to the compiler *p is the same as p[ ]. Therefore, although the two prototypes appear to differ in the types of their parameter, in actuality they do not.

Overloading Constructors Many times you will create a class for which there are two or more possible ways to construct an object. In these cases, you will want to provide an overloaded constructor for each way. This is a self-enforcing rule because if you attempt to create an object for which there is no matching constructor, a compile-time error results.

Example #include <iostream> #include <cstdio> using namespace std; class date { int day, month, year; public: date(char *d); date(int m, int d, int y); void show_date(); }; // Initialize using string. date::date(char *d) { sscanf(d, "%d%*c%d%*c%d", &month, &day, &year); } // Initialize using integers. date::date(int m, int d, int y) day = d; month = m; year = y;

void date::show_date(){ cout << month << "/" << day; cout << "/" << year << "\n"; } int main() { date ob1(12, 4, 2003), ob2("10/22/2003"); ob1.show_date(); ob2.show_date(); return 0; Output: 12/4/2003 10/22/2003

Allowing Both Initialized and Uninitialized Objects Another common reason constructors are overloaded is to allow both initialized and uninitialized objects (or, more precisely, default initialized objects) to be created. This is especially important if you want to be able to create dynamic arrays of objects of some class, since it is not possible to initialize a dynamically allocated array. To allow uninitialized arrays of objects along with initialized objects, you must include a constructor that supports initialization and one that does not.

Example #include <iostream> #include <new> using namespace std; class powers { int x; public: // overload constructor two ways powers() { x = 0; } // no initializer powers(int n) { x = n; } // initializer int getx() { return x; } void setx(int i) { x = i; } }; int main() { powers ofTwo[] = {1, 2, 4, 8, 16}; // initialized powers ofThree[5]; // uninitialized powers *p; int i; // show powers of two cout << "Powers of two: "; for(i=0; i<5; i++) { cout << ofTwo[i].getx() << " "; } cout << "\n\n"; // set powers of three ofThree[0].setx(1); ofThree[1].setx(3); ofThree[2].setx(9); ofThree[3].setx(27); ofThree[4].setx(81);

// show powers of three cout << "Powers of three: "; for(i=0; i<5; i++) { cout << ofThree[i].getx() << " "; } cout << "\n\n"; // dynamically allocate an array p = new powers[5]; // no initialization // initialize dynamic array with powers of two p[i].setx(ofTwo[i].getx()); // show powers of two cout << "Powers of two: "; cout << p[i].getx() << " "; delete [] p; return 0; Output: Powers of two: 1 2 4 8 16 Powers of three: 1 3 9 27 81