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  >  Concepts  >  OOPS

 Print  |  
Question:  What is the use/advantage of function overloading

Answer: Please suggest use of FO


May 05, 2007 07:11:08 #9
 Sunny   Member Since: Visitor    Total Comments: N/A 

RE: What is the use/advantage of function overloading
 

For eg: Say a method called addUs(a,b) adds a and b.
So the definition will be

int addUs(int a, int b){

 return a+b;

}

But now if you want your arguments to be Objects of a class say
class Demo{
int height;
int width;
}
You want the same function addUs() to return a new object which would have attributes height and width having values as sum of the height & width of the 2 arguments passed.

So now the definition would be:

Demo addUs(Demo a, Demo b){
 this.height = a.height + b.height;
 this.widht = a.widht + b.width;
 return this;
}

     

 

Back To Question