Compile Time polymorphism.

Slides:



Advertisements
Similar presentations
Object Oriented Programming
Advertisements

Function Overloading. 2 Function Overloading (Function Polymorphism) Function overloading is a feature of C++ that allows to create multiple functions.
 In inheritance the child (subclass) chooses its parent (superclass)  Remember - only public or “protected” methods and variables are inherited  Should.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
Overloading methods review When is the return statement required? What do the following method headers tell us? public static int max (int a, int b)
Math class methods & User defined methods Introduction to Computers and Programming in JAVA: V
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.
Function Overloading Overloading  Using same name for more than one function  Example: ovldmean.cpp.
OBJECT ORIENTED PROGRAMMING
Lecture From Chapter 6 & /8/10 1 Method of Classes.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
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.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
CHAPTER 3 Function Overloading. 2 Introduction The polymorphism refers to ‘one name having many forms’ ‘different behaviour of an instance depending upon.
Unit IV Unit IV: Virtual functions concepts, Abstracts classes & pure virtual functions. Virtual base classes, Friend functions, Static functions, Assignment.
CS212: Object Oriented Analysis and Design Lecture 9: Function Overloading in C++
ECE122 Feb. 22, Any question on Vehicle sample code?
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.
Supervisor Ebtsam AbdelHakam Department of Computer Science Najran University 24/2/2014 Ebtsam Abdelhakam 1.
1 Lecture 3 More about C++. 2 Topic for today More about classMore about class –Init list –Inline functions –Const –Default function parameters –Static.
10 Polymorphism. 2 Contents Defining Polymorphism Method Overloading Method Overriding Early Binding and Late Binding Implementing Polymorphism.
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.
 Templates enable us to define generic classes and functions and thus provides support for generic programming. Generic types are used as parameters.
Method OverloadingtMyn1 Method overloading Methods of the same name can be declared in the same class, as long as they have different sets of parameters.
Chapter 5 Methods 1. Motivations Method : groups statements that perform a function.  Level of abstraction (black box)  Code Reuse – no need to reinvent.
 Static  Example for Static Field  Example for Static Method  Math class methods  Casting  Scope of Declaration  Method Overloading  Constructor.
Methods.
Polymorphism and Virtual Functions One name many shapes behaviour Unit - 07.
CMSC 341 Lecture 2. Announcements Tutors wanted Office hrs Project 1.
Welcome to FUNCTION OVERLOADING Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS) KV jhagrakhand.
Announcements. Practice questions, with and without solutions will be uploaded by Friday 5 th November, make sure to check them before the weekend \\netstorage\Subjects\ITCA-b\Exam.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Basic Data Types & Memory & Representation
Operator Overloading Ritika Sharma.
Friend functions.
Function Overloading Can enables several function Of same name
Templates.
Default Constructors A default constructor is a constructor that takes no arguments. If you write a class with no constructor at all, C++ will write a.
Suppose we want to print out the word MISSISSIPPI in big letters.
FUNCTIONS In C++.
Function Overloading.
Class A { public : Int x; A()
Objectives Define polymorphism Overload functions
Templates.
Polymorphism Lec
Methods Additional Topics
WEL COME PRAVEEN M JIGAJINNI PGT (Computer Science)
Name: Rubaisha Rajpoot
CMSC 202 Templates.
Variables and Their scope
Passing objects As arguments
More ‘concepts’ on Function
Polymorphism Polymorphism
Method Overloading in JAVA
Polymorphism CT1513.
CS2011 Introduction to Programming I Methods (II)
Object-Oriented Programming (OOP) Lecture No. 37
Function Overloading CSCE 121 J. Michael Moore
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Method of Classes Chapter 7, page 155 Lecture /4/6.
Eighth step for Learning C++ Programming
Templates CMSC 202, Version 4/02.
More ‘concepts’ on Function
Computer Science II for Majors
Presentation transcript:

Compile Time polymorphism

polymorphism Polymorphism means “many forms”. Polymorphism means ability to take more than one form. C++ implements polymorphism through Overloaded functions Overloaded operators Virtual functions.

Function overloading A function name having several different definitions that are differentiable by the number or type of their arguments,is known as an OVERLOADED FUNCTION and this process is called FUNCTION OVERLOADING.

RULES OF FUNCTION OVERLOADING Functions to be overloaded must have same name. their parameter list must vary in: 1.in terms of number of arguments. 2.in type of arguments.

Example : Different parameter types void Function(int a) {} void Function(char a) {} void Function(double a) {}

Example : Different number of parameters int Function(int a) {} int Function(int a,char b) {} int Function(int a,float b, long c) {}

Important points : 1.While creating functions with same name if the signatures of subsequent functions match the previous function’s,then the second is treated as REDECLARATION of the first. For example: void square(int a,float b); void square(int x,float y);

2.While creating functions with same name if the signatures of the two functions match exactly but the return types differ,the second declaration is treated as an erroneous re-declaration of the first and is flagged at COMPILE time as an error. For example: float happy(float f); double happy(float x); error

FUNCTION OVERLOADING IS REFERRED TO AS COMPILE TIME POLYMORPHISM

WHY ……..??? SINCE,ERRORS COME OUT DURING COMPILE TIME ….

Class abc { int x;float y; public: int max(int a,int b) { if(a>b) return a; else return b; } float max(float a) { if(a>10) return 10.00;}}; void main() { clrscr(); float d;int c; abc o1,o2; c=o1.max(5,6); printf("%d",c); d=o2.max(10.0001); printf("%f",d); getch(); }

class graphics{ float a,b; public: float area(float r){ float x; x=3.14*r*r; return x;} float area(float l,float b) { float y; y=l*b; return(y); } }; void main(){ float k; clrscr(); graphics o1,o2; k=o1.area(10.0); printf("%f\t",k); k=o2.area(10.01,20.01); printf("%f",k); getch();}

Advantages : Can perform variety of functions with same function name. No need to remember name of many functions.

disAdvantages: Functions should have different argument lists. Member functions cannot be overloaded solely on the basis of one being static and the other being non static.