0% found this document useful (0 votes)
15 views

Compile Time Polymorphism - Function Overloading - Function Overloading

Function overloading allows defining multiple functionalities for a function by varying the number or type of parameters. It allows defining different versions of a function with the same name but different parameters. Examples include defining different Search functions that take an int, string, or Date as a parameter, or defining different Complex constructors that take int, float, or another Complex object as parameters. The compiler will resolve which version to call based on the parameters passed. Ambiguities will cause compiler errors.

Uploaded by

sudhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Compile Time Polymorphism - Function Overloading - Function Overloading

Function overloading allows defining multiple functionalities for a function by varying the number or type of parameters. It allows defining different versions of a function with the same name but different parameters. Examples include defining different Search functions that take an int, string, or Date as a parameter, or defining different Complex constructors that take int, float, or another Complex object as parameters. The compiler will resolve which version to call based on the parameters passed. Ambiguities will cause compiler errors.

Uploaded by

sudhan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Compile Time Polymorphism

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

You might also like