|
-
June 9th, 2010, 08:22 PM
#1
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;
}
-
June 9th, 2010, 09:27 PM
#2
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()
-
June 9th, 2010, 10:44 PM
#3
Re: Function Overloading
 Originally Posted by georgea
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
-
June 9th, 2010, 10:50 PM
#4
Re: Function Overloading
 Originally Posted by Paul McKenzie
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.
-
June 10th, 2010, 12:10 AM
#5
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...
-
June 10th, 2010, 05:22 AM
#6
Re: Function Overloading
 Originally Posted by Eri523
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|