Required Variables for Overloading Functions

Simply put, do you need to include variables of each data type that your overloaded function processes?

Specifically, this is the program I'm working on. If you can answer the question flat out you may not have to read it. This isn't for homework, but for independent study. Thank you for your help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/* The purpose of this program is to find two numbers and add them together, then divide by two
to get their average value. However, the program must accept and process both float and long variables */
#include <iostream>
float average(long, long); //These three prototypes describe the overloaded function "average".
float average(float, float);
float average(float, long);
int main()
{
    long a; //The following four variables attempt to ensure that the input can be either float or long.
    float b;
    long c;
    float d;
    short end; /*I use the 'end' variable to ensure that the program does not close prematurely.
                  it is not part of the actual program.*/
    std::cout<<"This program averages two integers.\n Please input the first value.";
    std::cin<<a || b; /*I attempted to use the operator "or" to ensure that if the input is a long variable
    then the program will receive it, as well as limit it if the input is only a long variable. 
    However, an error occurs here "no match for 'operator<<' in 'std::cin<< a'" and the same for c.*/
    std::cout<<"\n Please input the second value.\n";
    std::cin<<c || d;
    a || b == x, c || d == y; /*The point here was to set the received variables as x and y so that
    they could be applied into the 'z' variable without an ambiguity error.*/
    z = average(x,y); /*The largest problem here is that I cannot define x, y or z without giving them
    a data type. To do so would be to compromise the purpose of the program, whereas the return would
    be restricted to a single data type.*/
    std::cout<<"The average of the two integers is" << z << "\n.";
    std::cout<<"Input any value to end.\n";
    std::cin>>end;
    return 0;
}
float average //describes average function
{
      z=(x+y)/2;
      return z;
      }


Last edited on
Topic archived. No new replies allowed.