Thread: Newbie asking about overloading functions

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    1

    Newbie asking about overloading functions

    The book I am using to teach myself C++ shows the format for overloading functions but doesn't seem to go into great detail on how to use them. In an attempt to get a better handle on the concept I wrote code declaring four functions: Data_Type(int), Data_Type(double), Data_Type(float), and Data_Type(char).

    My understanding is that when the Data_Type function is called, the type of parameter passed in will determine which of the four is actually used. Is that a correct assesment?

    Assuming that I'm not completely misunderstanding the concept, one of the ways I'd like to use it is to write code the will call the appropiate function based on the type of input received (e.g., one function if the input is an int, anther if it's a double, etc).

    However, since simply saying "cin >> Data_Type();" won't work, I'm stumped on how to construct the cin statement when I'm not sure what data type is going to be entered.

    Any help or suggestions you'd be willing to offer a confused newbie would be greatly appreciated.
    -Thanks

  2. #2
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    You are correct about overloaded functions, however depending on what you want the functions to do they may not be necessary. Standard conversions will promote primitives so one function that accepts a double will accept all other primitives -

    Code:
    void foo(double b)
    {
    	cout << b;
    }
    
    
    int main() 
    { 
    
    	char a = 1;
    	int b = 2;
    	float c = 3;
    	double d = 4;
    
    	foo(a);
    	foo(b);
    	foo(c);
    	foo(d);
    
    
    	return 0;
    }
    The only one from the above example that you may want to overload is a function to accept a character (otherwise you'll get it's value outputted rather than the character).

    However, you need to know the input type before you can call the overloaded function. You could get the input as a character string and then check what it consists of (which you could then convert to an integer or floating point type), but you could never be sure what the input what supposed to be.
    zen

  3. #3
    Unregistered
    Guest
    Thanks for the help. But if I declare the fucntion as a double, that will result in a integer or a float being converted to a double won't it?

  4. #4
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    Yes.
    zen

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. operator new overloading and template functions
    By krappa in forum C++ Programming
    Replies: 40
    Last Post: 05-28-2008, 08:29 AM
  2. Overloading Functions. I keep getting junk.
    By Hexadakota in forum C++ Programming
    Replies: 13
    Last Post: 01-31-2008, 04:07 PM
  3. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  4. illegal function overloading????
    By Mr_Jack in forum C++ Programming
    Replies: 3
    Last Post: 12-17-2003, 01:03 PM
  5. Overloading functions with variable arguments
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 11-08-2001, 09:02 AM