Interview Questions Answers.ORG
Interviewer And Interviewee Guide
Interviews
Quizzes
Home
Quizzes
Interviews C Plus Plus Language Interviews:C++ Access ControlC++ COM ActiveXC++ ConstructorsC++ ContainersC++ Exception HandlingC++ FriendC++ InheritanceC++ Inline FunctionC++ New And DeleteC++ Operator OverloadingC++ Pointers & FunctionsC++ ProgrammerC++ ReferencesC++ Static DataC++ SyntaxC++ TemplateC++ Type CheckingC++ Virtual Functions
Copyright © 2018. All Rights Reserved
C++ Operator Overloading Interview Question:
Explain function overloading?
Submitted by: AdministratorFunction overloading is the process of using the same name for two or more functions. The secret to overloading is that each redefinition of the function must use either different types of parameters or a different number of parameters. It is only through these differences that the compiler knows which function to call in any situation.
Consider following program which overloads MyFunction() by using different types of parameters:
#include <iostream>
Using namespace std;
int MyFunction(int i);
double MyFunction(double d);
int main()
{
cout <<MyFunction(10)<<”n”; //calls MyFunction(int i);
cout <<MyFunction(5.4)<<”n”; //calls MyFunction(double d);
return 0;
}
int MyFunction(int i)
{
return i*i;
}
double MyFunction(double d)
{
return d*d;
}
Now the following program overloads MyFunction using different no of parameters
#include <iostream>
Using namespace std;
int MyFunction(int i);
int MyFunction(int i, int j);
int main()
{
cout <<MyFunction(10)<<”n”; //calls MyFunction(int i);
cout <<MyFunction(1, 2)<<”n”; //calls MyFunction(int i, int j);
return 0;
}
int MyFunction(int i)
{
return i;
}
double MyFunction(int i, int j)
{
return i*j;
}
Please note, the key point about function overloading is that the functions must differ in regard to the types and/or number of parameters. Two functions differing only in their return types can not be overloaded.
Submitted by: Administrator
Consider following program which overloads MyFunction() by using different types of parameters:
#include <iostream>
Using namespace std;
int MyFunction(int i);
double MyFunction(double d);
int main()
{
cout <<MyFunction(10)<<”n”; //calls MyFunction(int i);
cout <<MyFunction(5.4)<<”n”; //calls MyFunction(double d);
return 0;
}
int MyFunction(int i)
{
return i*i;
}
double MyFunction(double d)
{
return d*d;
}
Now the following program overloads MyFunction using different no of parameters
#include <iostream>
Using namespace std;
int MyFunction(int i);
int MyFunction(int i, int j);
int main()
{
cout <<MyFunction(10)<<”n”; //calls MyFunction(int i);
cout <<MyFunction(1, 2)<<”n”; //calls MyFunction(int i, int j);
return 0;
}
int MyFunction(int i)
{
return i;
}
double MyFunction(int i, int j)
{
return i*j;
}
Please note, the key point about function overloading is that the functions must differ in regard to the types and/or number of parameters. Two functions differing only in their return types can not be overloaded.
Submitted by: Administrator
Copyright 2007-2025 by Interview Questions Answers .ORG All Rights Reserved.
https://InterviewQuestionsAnswers.ORG.
https://InterviewQuestionsAnswers.ORG.