Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
hi friends
can you find the following

class Code
{
  int sum(int a);
  char sum(int a);
}
int Code::sum(int a)
{
  cout<<" returns integer";
}
char Code::sum(int a)
{
 cout<<" returns char";
}
int main()
{
 Code c;
 c.sum(65);
 }


I am not writing the whole code can you guess the output for this and tell me which function will invoke first and how it will be with output.
can anyone help me please
Posted
Updated 3-Aug-17 3:10am
v2
Comments
shakil0304003 15-Apr-11 3:47am    
very silly function overloading mistake...

Error
Notice that a function cannot be overloaded only by its return type. At least one of its parameters must have a different type.

Overloaded functions in http://www.cplusplus.com/doc/tutorial/functions2/[^]
 
Share this answer
 
The fact that you can't overload on return type doesn't mean you can't simplify the process a bit under certain circumstances, e.g. if your return type is constructible from your input type.
C++
template <typename T> T get(int i)
{
	return T(i);
}

int main()
{
	int i = get<int>(65);
	char c = get<char>(65);

	return 0;
}

The code above might be mistaken for an overload when in fact it's not. You will have to specify the return type when you call the function as the template argument. Sometimes this can be useful, but in effect, it is not different from implementing one method for each return type, like int getInt(int i) and char getChar(int i).
 
Share this answer
 
Hi,

The code wont compile..
U will get be given C2556 saying "The Overloaded func have different return types but the same parameter list".

Your function should have "Distinct" formal parameter list in compile time polymorphism(Function Overloading).
 
Share this answer
 
v2
Comments
Olivier Levrey 15-Apr-11 7:50am    
This answer doesn't deserve a 1. Besides, it is correct. Have a 5 ganeshkawade.
Apart from the above, your sum() functions are declared private and cannot be called in main - you'll get compiler error there if you don't declare them as public.

Regarding your question, if you go define void sum(int) and void sum(char), then your call sum(65) would call the int variant: integer constants are always assumed to be of type int, unless they are too big to fit into an int.
 
Share this answer
 
v2
You can have, instead of an overload, a single function that returns a data structure with all the types you want. Simply use the structure member for the type you wish to use at any given time.

C++
struct Rvals {
  int  i;
  char c;
} typedef rvals;


and then use the i or c member, as you wish, or both, from a single call.
 
Share this answer
 
v2
Comments
Richard Deeming 3-Aug-17 9:59am    
April 2011!
W Balboos, GHB 3-Aug-17 10:06am    
How did I end up on this page? Weird!

Wait - at this moment, it's flagged as having been updated 55 min. ago. It appeared in the cue as "new"
Richard Deeming 3-Aug-17 13:15pm    
That's a "feature" - the "updated" date is updated whenever a new solution is posted. If you look at the revision history[^], it was last updated in 2011.

I notice solution 5 is missing, so I assume it was a spam answer that got deleted. The question still gets pushed to the top of the list whenever that happens.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900