Polimorfismul functiilor - function overloading

Polimorfismul functiilor are in vedere functii cu nume identic dar cu lista de parametrii diferiti (in literatura de limba engleza in loc de polimorfismul functiiolr se mai foloseste termenul function overloading). Exemplul urmator (polymfun.cpp) construieste 4 patru functii Double pentru a opera si reintoarce valori int, long, float si double.

// Polimorfismul functiilor
#include <iostream.h>
int Double(int);
long Double(long);
float Double(float);
double Double(double);

int main()
{
int myInt = 6500;
long myLong = 65000;
float myFloat = 6.5F;
double myDouble = 6.5e20;

int doubledInt;
long doubledLong;
float doubledFloat;
double doubledDouble;

cout << "myInt: " << myInt << "\n";
cout << "myLong: " << myLong << "\n";
cout << "myFloat: " << myFloat << "\n";
cout << "myDouble: " << myDouble << "\n";

doubledInt = Double(myInt);
doubledLong = Double(myLong);
doubledFloat = Double(myFloat);
doubledDouble = Double(myDouble);

cout << "doubledInt: " << doubledInt << "\n";
cout << "doubledLong: " << doubledLong << "\n";
cout << "doubledFloat: " << doubledFloat << "\n";
cout << "doubledDouble: " << doubledDouble << "\n";

return 0;
}

int Double(int original)
{
cout << "In Double(int)\n";
return 2 * original;
}

long Double(long original)
{
cout << "In Double(long)\n";
return 2 * original;
}

float Double(float original)
{
cout << "In Double(float)\n";
return 2 * original;
}

double Double(double original)
{
cout << "In Double(double)\n";
return 2 * original;
}

In urma rularii acestui program la consola text se obtine:

myInt: 6500
myLong: 65000
myFloat: 6.5
myDouble: 6.5e+20
In Double(int)
In Double(long)
In Double(float)
In Double(double)
doubledInt: 13000
doubledLong: 130000
doubledFloat: 13
doubledDouble: 1.3e+21

Cornel Mironel Niculae, 2003-2004

11-Mar-2004