Link to home
Start Free TrialLog in
Avatar of Rugoingwme
Rugoingwme

asked on

trying to learn to use an overloaded function

Hi,

I'm trying to learn how to program in C++.  I have minimal experience programming, having had only one FORTRAN programming course 20 years ago.  I bought a book that teaches C++ programming, but this book didn't give an example of a program written to highlight overloaded functions.  I'm having to create a program using an overloaded function called average to find the average of 2 numbers using

int average(int, int)
long average(long, long)
float average(float, float)

I was trying to create a program to do this using numbers that a person could input, then the program would find the average. I seem to consistently find errors, and the program doesn't compile, or compiles, but gives the answer only in a couple of cases.  Any guidance is appreciated.  Thanks.

Here's the code:

#include <iostream>

int main()
{

      // calculating the average of two integers, two long integers
      // or 2 floating-point values using an overloaded function called average

      // define variables      

      int x, y;
      long a, b;
      float m, n;


      // try using if statements to put the inputs into the appropriate variable buckets, then do the
      // overloaded function thing.  
      
      // first function
      std::cout << "This program calculates the average of 2 numbers. \n";
      std::cout << "what is the first number you want to average? \n";
      std::cin >> x;

            if (x % == 0)
                  {
                  int average(int x, y);
                  std::cout << "what is the second number you want to average? \n";
                  std::cin >> y;
                  std::cout << (x + y) / 2;
                  return 0;      
                  }
            if (x > 4294967295)
                  {
                  a = x;
                  long average(long a, b);
                  std::cout << "what is the second number you want to average? \n";
                  std::cin >> b;
                  std::cout << (a + b) / 2;
                  return 0;      
                  }
            if (x > 1.2e-38)
                  {
                  m = x;
                  float average(float m, n);
                  std::cout << "what is the second number you want to average? \n";
                  std::cin >> n;
                  std::cout << (m + n) / 2;
                  return 0;      
                  }      

} averagef.cpp
ASKER CERTIFIED SOLUTION
Avatar of Tobias
Tobias
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rugoingwme
Rugoingwme

ASKER

Hi everyone,
I divided up the points the way i did because:
Madshiva: I understand the code changes you made.  I still don't really understand overloaded functions better, but you fixed my code.

sarabande: your comment about me declaring the functions, but not implementing them or calling them was helpful.  Unfortunately, most of your other explanations went over my head.  I do recall saying clearly that I was a beginner, but I should have said that i'm only about 5 chapters into a 24 chapter book to teach C++, so that's my bad.

robert_schutt: I think I understand conceptually what you did, but you used statements like "inputstring", "sscanf", and "switch", which I don't yet understand.  I tried compiling the code you wrote, and I couldn't get it to compile.
my bad. i try in simpler words:

the average of two numbers isn't a good sample for overloading as you don't have a difference in the method and in result when using different argument types. that means the average of 1 and 2 will be computed exactly with the same statements than that of 123.45 and 98.765 and it makes not so much sense to provide two functions cause it is always the sum of the arguments divided by two. if you return an int or a long type, it is also a mathematical flaw cause the average of 1 and 2 is 1.5 and not 1 (what would be the result of your function).

therefore in c++ the average would be good for templating where the same statements can be used and only the argument type is a variable. i didn't show the template code as a solution but only for demonstration that in c++ there are further polymorphism concepts beside overloading.

the code MadShiva posted doesn't 'repair' anything, beside that it may compile:

- there are still no implementations for the functions
- there is still no call but only a repetition of the declaration already done above main

so it really couldn't help you.

the code of Robert Schutt adds both the calls and the implemented functions and if you would build it (i think you only would need to add the statement 'using namespace std;' and it would build )  and run, you will see that it correctly runs the correct overload when you enter a float.

for integer input i would assume it always runs the int version of the overloaded functions cause for most compilers int and long are equivalent 32-bit integer types. so, the sscanf would return 1 (TRUE) for both integer formats %ld and %d if you enter an integer less than 2^31  (cause int and long are signed integers and the highest bit (of the 32) was used for sign. and both calls would return 0 (FALSE) if you enter a bigger number.

but if you look at the calls of average (those were made in the printf statements, which do output to console window), you will see that there were exactly 3 calls for average, each using the right argument types. that is what you required and not bad, but actually it is not very surprising that when calling average with two arguments of type float that it will use the right function. that is not so much different as if you used a name 'average_of_floats' as function name. it shows that overloading isn't something that helps from deducing an input type.

that means your design of the program to test overloading is not so good and computing the avarage is not so good for overloading either.

i would suggest you to test overloading on a to_string conversion function, so that the prototypes would be

string to_string(int i);
string to_string(double d);
string to_string(bool b);
string to_string(char c);
string to_string(unsigned char uc);

for that you would include

#include <string>
#include <iostream>
using namespace std;  // to avoid std:: prefix

then ask the user what they want to input (1 for int, 2 for double, ...) and then use the appropriate cin statement:

if (choice == 1)
{
      int i;
      cin >> i;
      cout << to_string(i) << endl;  // here the right overload was called
}
else if (choice == 2)
{
     double d;
     ...

Open in new window



if you want to go that way, you may ask whenever you need more help.

Sara