Compile Time Polymorphism - Function Overloading - Function Overloading
Compile Time Polymorphism - Function Overloading - Function Overloading
- Function overloading
P. Vasuki
Function Overloading
• Defining multiple functionalities for a function.
• The signature of a function (number of parameters
or type of parameters) varies from one definition to
another
• Example consider following functions are defined in
student class
• Search(int) // Searching a student record using roll
//number
• Search(string) //searching a student record using his
name
• Search (Date DOB) //searching a student record
//using his Date of Birth
Ambiguities
• Compiler rises error on ambiguity.
• Sample situations on Ambiguity
– Swap(int, int)
– Swap(int &,int &)
– Swap(int,int,int n=2);
– Display(char)
– Display(unsigned char)
Constructor Overloading
• Declaring different constructor with
different signatures are constructor
overloading – a special type of Function
Overloading
• class complex
• complex()
• complex(int , int)
• complex(float, float)
• Complex(complex &)