Function Overloading (OOP Task)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: Function Overloading (OOP Task)

  1. #1
    Join Date
    Feb 2013
    Posts
    4

    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.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,995

    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.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Function Overloading (OOP Task)

    Why are you doing the OP's homework for him?

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,995

    Re: Function Overloading (OOP Task)

    Just providing an example of using overloaded functions.

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Function Overloading (OOP Task)

    Quote Originally Posted by 2kaud View Post
    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.

  6. #6
    Join Date
    Feb 2013
    Posts
    4

    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.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Function Overloading (OOP Task)

    Quote Originally Posted by mehmehspazumweh View Post
    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?

  8. #8
    Join Date
    Feb 2013
    Posts
    4

    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.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    1,995

    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);
    }

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Function Overloading (OOP Task)

    Quote Originally Posted by 2kaud View Post
    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.

  11. #11
    Join Date
    Feb 2013
    Posts
    4

    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

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    11,980

    Re: Function Overloading (OOP Task)

    Quote Originally Posted by mehmehspazumweh View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center