Overloading Function - Not understanding the rules behind it

Hello everyone,

I am working on a problem where I need to design a menu that has 4 options to 'run' and each will use the same function via overloading to display two values entered from a menu.

Here is how the menu choices should work:


1. If the user entered menu choice “1” above, you may assume that both numbers are integers. Your program should read them into appropriate variables, then call the appropriate function (that takes two integers as input arguments) to display them as they were entered.

2. If the user entered menu choice “2” above, you may assume that the first number was an integer and the second number was a float. Your program should read them into appropriate variables, then call the appropriate function (that takes one integer followed by one float as input arguments), which should round up the float to the next highest integer, then display the two integers.

3. If the user entered menu choice “3” above, you may assume that the first number was an float and the second number was a integer. Your program should read them into appropriate variables, then call the appropriate function (that takes one float followed by one integer as input arguments), which should round down the float to the next lowest integer, then display the two integers.

4. If the user entered menu choice “4” above, you may assume that both numbers are floats. Your program should read them into appropriate variables, then call the appropriate function (that takes two floats as input arguments), which should round down the first float to the next lowest integer, and round up the second float to the next highest integer, then display the two integers.

*********************************************************************
With this information, I wrote the following program....

*************************************************************************
#include<cmath>
#include <iostream>
using namespace std;

void menu_choice(int number_one, int number_two);
void menu_choice(int number_one, float number_two);
void menu_choice(float number_one, int number_two);
void menu_choice(float number_one, float number_two);

int main()
{
// Declare all needed variables
int choice;
int n1, n2, n3, n4;
int number_1, number_2;// choice_one, choice_two;


// Display a menu for the user to chose from.
cout << "MENU:" << endl;
cout << "1. When both numbers integers\n";
cout << "2. When the first number is an integer and the second number is a float\n";
cout << "3. When the first number is a float and the second number is an integer\n";
cout << "4. When both numbers are floats" << endl << endl;

cout << "Enter a choice from the menu above: ";
cin >> choice;


switch (choice)//determines which 'function' will be used based of menu choice
{
case 1:
cout << "Enter first number: \n";
cin >> number_1;
cout << "Enter second number: \n";
cin >> number_2;
menu_choice(n1, n2);

break;

case 2:
cout << "Enter first number: \n";
cin >> number_1;
cout << "Enter second number: \n";
cin >> number_2;

menu_choice(n3, n4);

break;

case 3:
cout << "Enter first number: \n";
cin >> number_1;
cout << "Enter second number: \n";
cin >> number_2;
menu_choice(n5,n6);

break;

case 4:
cout << "Enter first number: \n";
cin >> number_1;
cout << "Enter second number: \n";
cin >> number_2;
menu_choice(n7,n8);

break;
}

cout << "You have entered an invalid option." << endl << endl;

return 0;
}
void menu_choice(int number_one, int number_two)
{
int n1, n2;
n1 = number_one;
n2 = number_two;
cout << n1, " ", n2;
return;

}
void menu_choice(int number_one, float number_two)
{
int n3;
float n4 ;
n3 = number_one;
n4 = number_two;
cout << n3, " ", ceil(n4);
return;

}
void menu_choice(float number_one, int number_two)
{
float n5;
int n6;
n5 = number_one;
n6 = number_two;
cout << floor(number_one), " ", number_two;
return;
}
void menu_choice(float number_one, float number_two)
{
float n7, n8;
n7 = number_one;
n8 = number_two;
cout << floor(number_one), " ", ceil(number_two);
return;
}

****************************************************************************

So here is my problem, I'm understanding that to overload a function, something must be different within the statement. Which is seen in each one by using int and float in different places. BUT, when I call the function in the main body within the switch statement. I am not understanding how to correctly label each function so that the right statement is called. So I keep running into build errors. I know it has something to do with the overloading aspect along with using void...but the specific reason why,is lost to me.

Any help understanding this would be greatly appreciated!

Last edited on
The type of the arguments that you pass to the function decides which overload to call. For instance if you pass an int and a float menu_choice(1, 2.0f) you know that the second overload menu_choice(int, float) will be called.
Last edited on
Hello McClapYourHands,

Peter87 has a good point to think about. Your concept of the overloaded functions is correct, but the implementation is incorrect.

I will point out that n1 - n4 are never initialized, so even if it would compile you would have garbage in those variables and no idea what the output would be. Then n5 - n8 are never defined. Yet you are calling your overloaded functions with these variables.

In the case: part of the switch you do cin >> number_1; and cin >> number_2; both defined as int, but one needs to be a float for case 2 and 3 and 2 floats for case 4. You put information into these two variables, but never use them. They should the parameters to call menu_choice(number_1, number_2);.

In your menu_choice() choices all you really need there is what is necessary to deal with the floats and the cout line. The return statement is not needed.

Work on that and let us know if you have any more problems.

Hope that helps,

Andy


Thank you Peter87 and Handy Andy! After thinking on what you both said, I re-looked at my code and realized that I was over-complicating/ over-thinking the process of overloading, and got the program to work :)

Thank you for the explanations, they helped immensely!

McClapYourHands
Topic archived. No new replies allowed.