GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

  GeekInterview.com  >  Interview Questions  >  Programming  >  C++

 Print  |  
Question:  Default Parameter

Answer: What is default parameter? Can a overloaded function have a default parameter?


July 07, 2009 11:01:43 #4
 yzesong   Member Since: July 2009    Total Comments: 20 

RE: Default Parameter
 
This is my answer, please correct me if I am wrong.
Overloading functions with default parameters will cause compile error for ambiguous calling on the overloaded function.  But it really depends on the signatures of the overloaded function. If you don't call the overloaded function, as long as the signatures are different, it compiles fine. When you call them, you can not omit all the parameters because of ambiguity issue. 

Here is an example I played with, 
void PrintValues(int nValue1=10, int nValue2=20, int nValue3=30)
{
using namespace std;
cout << "IValues: " << nValue1 << " " << nValue2 << " " << nValue3 << endl;
}

double PrintValues(double fValue1= 1.1, double fValue2=2.2, double fValue3=3.3)
{
using namespace std;
cout << "FValues: " << fValue1 << " " << fValue2 << " " << fValue3 << endl;

return 99.9;
}

int main(int argc, char* argv[])
{
PrintValues(1, 2, 3);
PrintValues(1, 2);
PrintValues(1);
//PrintValues();  // this will causing ambiguity

PrintValues(88.88); // this is fine

return 0;
}

So generally, you don't want to do this. 

     

 

Back To Question