Presentation is loading. Please wait.

Presentation is loading. Please wait.

Compile Time polymorphism.

Similar presentations


Presentation on theme: "Compile Time polymorphism."— Presentation transcript:

1 Compile Time polymorphism

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

3 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.

4 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.

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

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

7 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);

8 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

9 FUNCTION OVERLOADING IS REFERRED TO AS COMPILE TIME POLYMORPHISM

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

11 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( ); printf("%f",d); getch(); }

12 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();}

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

14 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.


Download ppt "Compile Time polymorphism."

Similar presentations


Ads by Google