Thread: Overload function help

  1. #1
    Registered User
    Join Date
    Feb 2012
    Location
    Hawthorne, California, United States
    Posts
    9

    Overload function help

    This is a homework assignment but I am stuck and could use some help. I have written the following code and the program works as it is supposed to. My problem is the assignment I have is to write an overloaded function and I am not sure how to do that. Any ideas or suggestions would be appreciated.

    Code:
        int pts, rbd, ast;
        double min, yer;
        
        cout<< "Enter points scored: \n";
        cin>> pts;
        
        cout<< "Enter time played in minutes: \n";
        cin>> min;
        
        cout<< "Enter rebounds: \n";
        cin>> rbd;
        
        cout<< "Enter assists: \n";
        cin>> ast;
        
        yer = (((pts / min) + (rbd / min) + (ast / min)) / 3) * 10;
        
        cout<< "Player Efficiency Rating is:" << yer << endl;
    One additional note, I have removed some sections of the code that were not related to my question. So what is missing here is intentional.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Overloading of functions just refers to two functions, with the same name, accepting different types of arguments.

    So break your code up into parts that can be placed into separate functions - cases where it is doing the same basic thing, but with different data, are obvious candidates. Then among those functions look for a case where those functions do essentially the same thing, but on different argument types.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Location
    Hawthorne, California, United States
    Posts
    9
    Quote Originally Posted by grumpy View Post
    Overloading of functions just refers to two functions, with the same name, accepting different types of arguments.

    So break your code up into parts that can be placed into separate functions - cases where it is doing the same basic thing, but with different data, are obvious candidates. Then among those functions look for a case where those functions do essentially the same thing, but on different argument types.

    So I tried implementing your advice, my program is definitely not working now and I do not even understand the error the compiler is giving me. I am required to have 4 overloaded functions. The compiled is telling me Unresolved External 'yer (int)'

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    
    double yer( int p );
    double yer( int p, double m )
    {
    	return p/m;
    }
    double yer( int p, int r, double m )
    {
    	return (p+r) / m;
    }
    double yer( int p, int r, int a, double m )
    {
    	return (p+r+a) / m;
    }
    
    
    int main()
    {
    	int pts, rbd, ast;
    	double min, per, per2, per3, per4, pert;
    	
    	cout<< "Enter points scored: \n";
    	cin>> pts;
    	
    	cout<< "Enter time played in minutes: \n";
    	cin>> min;
    	
    	cout<< "Enter rebounds: \n";
    	cin>> rbd;
    	
    	cout<< "Enter assists: \n";
    	cin>> ast;
    	
    	
    	per = yer (pts);
    	per2 = yer (pts / min);
    	per3 = yer (pts + rbd / min);
    	per4 = yer (pts + rbd + ast / min);
    	
    	pert = ((per+per2+per3+per4) / 4)* 10;
    	
    	cout<< "Player Efficiency Rating is:" << pert << endl;
    	
    return 0;
    }

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    You forgot to define the first one, which takes only a single int.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Location
    Hawthorne, California, United States
    Posts
    9
    So if I were to change the very first yer to an int instead of a double it should work?

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Rob31645 View Post
    So if I were to change the very first yer to an int instead of a double it should work?
    No, the declaration is okay, you need to DEFINE it.
    i.e Write the body of "double yer( int p );"

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It won't make much difference, given that you are only ever calling the function with an argument of type double. The intent of overloading is to allow the function to be called in different manners, but having similar meanings each way.

    Personally, I wouldn't even bother doing your calculation in an overloaded function. The prompting and reading of input values, however, ....
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function template overload/specialisation
    By DL1 in forum C++ Programming
    Replies: 6
    Last Post: 12-18-2008, 02:11 PM
  2. ambiguity error caused by function overload?
    By Elhaz in forum C++ Programming
    Replies: 9
    Last Post: 10-02-2004, 07:38 PM
  3. Function Overload in C
    By Cikotic in forum C Programming
    Replies: 6
    Last Post: 07-05-2004, 03:54 PM
  4. Replies: 1
    Last Post: 05-01-2004, 05:41 AM
  5. overload
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-11-2001, 09:38 AM