Polymorph means 'having many shapes'
A function in C++ (and other programming languages) can be polymorph by taking different sets of parameters.
2006-12-04 02:58:50
·
answer #1
·
answered by Arpit 2
·
0⤊
0⤋
Polymorph means 'having many shapes'
A function in C++ (and other programming languages) can be polymorph by taking different sets of parameters.
For instance, you could have a method that doubles a numeric value and outputs it that takes an int (integer) as a parameter:
void Multi( int n )
{
cout << n * 2;
}
Now, if you want a method that multiplies a float you do not have to come up with a new method name, but reuse the name 'Multi' and make the function polymorph by writing a new 'shape' of the function taking a float:
void Multi(float f)
{
cout << f * 2;
}
or pass a string for that matter
void Multi(string s)
{
cout << s << s;
}
Just to make sure we are on the same boat, these three methods would reside in the same scope without bothering each other ;)
2006-12-03 13:34:35
·
answer #2
·
answered by Martin I 3
·
0⤊
0⤋
Polymorphism=One Function Many Forms.
The same Function name is used to perform different operations.
Asssume we have a ADD Function to add Two Integer numbers,
The same function can be expanded to peform the same addition operation on Float and even characters also.
In this case the Parameters that u r passing to Function will change.
The Compiler will call the Function now by checking the Parameters that u have passed.
void add(int a,int b);
void add(float a,int b);
void add(int a,float b);
void add(float a,float b);
void add(char a,char b);
The above is an Example of the Add Function oveloaded for five times.
Even u can have more, that is based on Parameters and their types.
2006-12-04 00:41:59
·
answer #3
·
answered by Ravi Nanjunda Rao 3
·
0⤊
0⤋
A Function with same name with different numer or different types of parameters. e.g. Foo(S as String) and Foo(i as integer) is Polymorphic fucntion. Also Foo(s as string, i as interger ) is Ploymorphic.
In gereneral Signature of the function needs to different. Only thing Return type needs to same of all definations of function.
2006-12-03 21:58:05
·
answer #4
·
answered by raju 5
·
0⤊
0⤋