CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2010
    Posts
    9

    Angry Function Overloading

    So, once again, my vague-as-hell book is asking to do something else.
    Specifically, this is a direct quote from the book:
    "Using default arguments, write a function that asks the user for a number and returns that number. The function should accept a string prompt from the calling code. If the caller doesn't supply a string for the prompt, the function should use a generic prompt. Next, using function overloading, write a function that achieves the same results."
    Is it me, or the book? Maybe I'm not cut for programming.

    This is my attempt:
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    int return_number(void);
    string return_number(string prompt1 = "Enter in a number: ");
    
    int main() 
    {
      int user_num1 = return_number();
      cout << "Thanks for entering: " << user_num1 << ".\n\n";
      
      string user_int1 = return_number("Enter a # man: ");
      cout << "Thanks for entering: " << user_int1 << ".\n\n";
      
      return 0;
    }
    
    int return_number(void)
    {
      int user_num;
      cout << "Enter a number: ";
      cin >> user_num;
      return user_num;
    }
    
    string return_number(string prompt2)
    {
      string user_int;
      cout << prompt2;
      cin >> user_int;
      return user_int;
    }

  2. #2
    Join Date
    Jun 2010
    Posts
    4

    Re: Function Overloading

    Your close,

    Its asking you to write a function where if the a prompt parameter is supplied, use that prompt, if not use a default prompt.

    And secondly, implement the same functionality using overloaded functions.


    Ill give you the prototypes for your functions

    int return_number( string prompt = "Generic Prompt")
    int return_number()

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Function Overloading

    Quote Originally Posted by georgea View Post
    Your close,

    Its asking you to write a function where if the a prompt parameter is supplied, use that prompt, if not use a default prompt.

    And secondly, implement the same functionality using overloaded functions.


    Ill give you the prototypes for your functions

    int return_number( string prompt = "Generic Prompt")
    int return_number()
    There is no need for the second prototype. The first one is all that's necessary.

    Secondly, the string should be passed by const reference, not by value.

    Regards,

    Paul McKenzie

  4. #4
    Join Date
    May 2009
    Posts
    19

    Re: Function Overloading

    Quote Originally Posted by Paul McKenzie View Post
    There is no need for the second prototype. The first one is all that's necessary.

    Secondly, the string should be passed by const reference, not by value.

    Regards,

    Paul McKenzie
    That's true for the first implementation, where he should have only the first prototype. Then it asks him to do it with function overloading, which would have two prototypes:

    int return_number();
    int return_number(const string* prompt);

    In the second case, the first, generic method could simply call the second. (and probably should)

    EDIT: Just realized that if you're using a C compiler then the first declaration needs to have void in the declaration, making it int return_number(void); (why you would be using a C compiler, I don't know, it is the C++ forums, but this may help someone in the future) I'm a java programmer at heart, I don't think about these things as much...
    Last edited by Singing Boyo; June 9th, 2010 at 11:32 PM.

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Function Overloading

    Singing Boyo,

    most of your reply is correct, but this excercise is C++ from bottom up. Classical C has no function overloading and default parameter values at all.

    Just FYI...

  6. #6
    Join Date
    Aug 2007
    Posts
    858

    Re: Function Overloading

    Quote Originally Posted by Eri523 View Post
    Classical C has no function overloading and default parameter values at all.
    Not to mention no string class.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured