
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Function overloading is a programming technique that allows a program to contain multiple functions with the same name but different parameter types or numbers. The rules for function overloading, its usage, and an example of how to implement it in c++. Overloading functions is useful when the algorithm remains the same, but the expected input varies.
Typology: Study notes
1 / 1
This page cannot be seen from the preview
Don't miss anything!
Function Overloading (pages 908-911) A program can contain multiple functions with the same name! EX: void Print ( int data ); void Print ( string data ); You’ll do this when the algorithm is the same, but the type(s) of the parameters are different. Rules: The computer can only distinguish functions with different types or number of arguments. The number and/or type of parameters expected by the function must be unique for each overloaded function. The return type is not considered when selecting the appropriate overloaded function. EX: //this is NOT okay int Calculate( int data ); float Calculate ( int data ); //this is okay int Calculate (int data); int Calculate (int data1, int data2); int Calculate (float data); Using in a program Overloading functions is appropriate when all of the functions use the same algorithm, but should be called in different scenarios based on their expected input. int main( ) { void DebugPrint (int data); //function prototypes void DebugPrint (string data); void DebugPrint (float data); int value; //variable declarations float average; string name; … //program statements to assign values to value, average, name, letter and isOk DebugPrint(value); Debug Print(average); DebugPrint(name); … //rest of the program return 0; } void DebugPrint(int data) { cout << “Debugging “ << data << endl; } void DebugPrint(float data) { cout << “Debugging “ << data << endl; } void DebugPrint(string data) { cout << “Debugging “ << data << endl; }