-
February 27th, 2013, 06:54 AM
#1
Function Overloading (OOP Task)
Hello. I am working on a small task as part of some independent study for a course i am taking. I have been asked to create a program to overload a function which should accept and print first one integer and then two integers. Here is the code i have produced so far:
http://pastebin.com/BUT9mfaf
Code:
#include <iostream>
using namespace std;
//Overloading f1 three ways
int f1(int a);
double f1(double a);
long f1(long a);
int main()
{
cout << f1();
system ("pause");
return 0;
}
//f1 for integer
int f1(int a)
{
cout << "Please enter a number: ";
cin >> a;
return a;
}
// f1 for double
double f1(double a)
{
cout << "Please enter a number: ";
cin >> a;
return a;
}
//f1 for long
long f1(long a)
{
cout << "Please enter a number: ";
cin >> a;
return a;
}
now my C++ isnt great, i'll be honest, but as far as i am aware the code should work. my only concern is the error i receive on line 12. I am certain there should be a value in the bracket but i dont know what. can you any of you C++ gurus point me in the right direction please? i just dont know what to do.
-
February 27th, 2013, 07:57 AM
#2
Re: Function Overloading (OOP Task)
Yes, you need to supply a value to f1 as f1 is defined to require one argument of either type int, double or long. The number should be obtained before the function is called so the program knows which version of the function to use.
Code:
//f1 requires a value
cout << f1();
In relation to your program to overload a function to print first one and then two integers, you could do something like this
Code:
#include <iostream>
using namespace std;
//Overloading function f1
int f1(int a);
int f1(int a, int b);
int main()
{
int n1,
n2;
cout << "Please enter an integer: ";
cin >> n1;
cout << "Pleae enter another integer: ";
cin >> n2;
f1(n1);
f1(n1, n2);
system ("pause");
return 0;
}
//f1 for one integer
int f1(int a)
{
cout << "f1 with one int argument: " << a << endl;
return a;
}
// f1 for two integers
int f1(int a, int b)
{
cout << "f1 with two int arguments: " << a << " " << b << endl;
return a;
}
f1 is defined to either require one int argument or two int arguments. Which version of the function is used depends upon how the f1 function is called.
Hope this helps.
-
February 27th, 2013, 08:17 AM
#3
Re: Function Overloading (OOP Task)
Why are you doing the OP's homework for him?
-
February 27th, 2013, 09:00 AM
#4
Re: Function Overloading (OOP Task)
Just providing an example of using overloaded functions.
-
February 27th, 2013, 09:06 AM
#5
Re: Function Overloading (OOP Task)
Originally Posted by 2kaud
Just providing an example of using overloaded functions.
No, you did his homework for him, exactly as it was asked. That's a little too much help.
-
February 27th, 2013, 09:46 AM
#6
Re: Function Overloading (OOP Task)
Thank you 2kaud for your help. To answer the previous post. 2kaud has certainly not "done my homework for me", in fact i fail to see how this is even a good example of overloading when they are both integers? It will certainly help me though. Thanks again.
-
February 27th, 2013, 09:52 AM
#7
Re: Function Overloading (OOP Task)
Originally Posted by mehmehspazumweh
Thank you 2kaud for your help. To answer the previous post. 2kaud has certainly not "done my homework for me", in fact i fail to see how this is even a good example of overloading when they are both integers? It will certainly help me though. Thanks again.
"I have been asked to create a program to overload a function which should accept and print first one integer and then two integers."
Isn't that what you asked for?
-
February 27th, 2013, 11:28 AM
#8
Re: Function Overloading (OOP Task)
yeah fair enough. i didnt quite explain the full program expected of me so it that sense you are right... he did do it for me... but thats not all i have to do. this example will help but as i said he hasnt done all the work. thanks again.
-
February 27th, 2013, 11:34 AM
#9
Re: Function Overloading (OOP Task)
This is an example of overloading on different type for argument
Code:
#include <iostream>
using namespace std;
void f1(int i)
{
cout << "in f1 with integer " << i << endl;
}
void f1(double d)
{
cout << "in f1 with double " << d << endl;
}
int main()
{
f1(3);
f1(4.5);
return (0);
}
-
February 27th, 2013, 01:22 PM
#10
Re: Function Overloading (OOP Task)
Originally Posted by 2kaud
This is an example of overloading on different type for argument
Helping is one thing. Doing the work as you have here is not what this forum is about.
http://forums.codeguru.com/showthrea...ork-assignment
Generally, members of this forum will be reluctant to do your homework for you. Here are some reasons:
In a very general sense, it is counter-productive to do homework for others. The very purpose of your homework is to verify that you have understood a subject and are able to apply the learned knowledge to solving specific problems. By having someone else do the assignment for you, you are not only cheating at your teachers, but also at yourself, since you pretend (and perhaps even believe) to have acquainted knowledge and skills that you actually don't have.
...
Don't expect to lean back and have others do the work for you.
-
February 27th, 2013, 01:40 PM
#11
Re: Function Overloading (OOP Task)
Right... OK. This will be my last post on this thread because quite frankly you're being very unfair and I don't particularly care for your opinion any longer. You seem to have not realized that i did actually produce my own code? I asked to be "pointed in the right direction". Honestly... how could you possibly think i am expecting fully working code as a response to my perfectly reasonable question? The fact that the response was indeed working code gives you no right to assume that I don't understand it or will maybe attempt to pass it off as my own. I understand the purpose of homework for crying out loud, don't patronize me. I was only stuck on one line of code which i am now able to resolve and continue working on thanks to the help of 2kaud. Jeez
-
February 27th, 2013, 02:04 PM
#12
Re: Function Overloading (OOP Task)
Originally Posted by mehmehspazumweh
Right... OK. This will be my last post on this thread because quite frankly you're being very unfair and I don't particularly care for your opinion any longer. You seem to have not realized that i did actually produce my own code? I asked to be "pointed in the right direction". Honestly... how could you possibly think i am expecting fully working code as a response to my perfectly reasonable question? The fact that the response was indeed working code gives you no right to assume that I don't understand it or will maybe attempt to pass it off as my own. I understand the purpose of homework for crying out loud, don't patronize me. I was only stuck on one line of code which i am now able to resolve and continue working on thanks to the help of 2kaud. Jeez
My posts weren't directed to you. 2kaud didn't actually address your question, but gave you a complete solution to your homework, which isn't the way the board operates. Your problem really wasn't about overloading but about how to call functions correctly.
Tags for this Thread
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
|