Overload function help

This is a discussion on Overload function help within the C++ Programming forums, part of the General Programming Boards category; This is a homework assignment but I am stuck and could use some help. I have written the following code ...

  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,208
    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%.

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    You forgot to define the first one, which takes only a single int.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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 );"
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,208
    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%.

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, 01: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. Function Pointer Assigned To Map Overload Resolution Problem!
    By DeepBlackMagic in forum C++ Programming
    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, 08:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21