| | |
Overloaded Function Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 18
Reputation:
Solved Threads: 0
Hey Everyone! I have two - hopefully simple - problems that I need help with. I wrote an atm program in c++ that works except for two things...
(1) the part where I calculate the balance based on the interest rates for the checkings and savings accounts need to be put into an overloaded function and I dont know how to do that. I understand with an overloaded function both functions will have the same name but different types or number of arguments. But don't know exactly how to apply that And
(2) When the user inputs "e" or "E" for Exit I dont what the program to ask "Make another transaction?" I just want it to go to "Thank you for using MY Bank ATM!"
Here is my code:
(1) the part where I calculate the balance based on the interest rates for the checkings and savings accounts need to be put into an overloaded function and I dont know how to do that. I understand with an overloaded function both functions will have the same name but different types or number of arguments. But don't know exactly how to apply that And
(2) When the user inputs "e" or "E" for Exit I dont what the program to ask "Make another transaction?" I just want it to go to "Thank you for using MY Bank ATM!"
Here is my code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <string> using namespace std; //void calculate_balance( //void calculate_balance( void exit(); int main() { double savings = 1000; double checking = 1000; double withdraw_checking = 0; double withdraw_savings = 0; double deposit_checking = 0; double deposit_savings = 0; double check_amount; string choice, yesno, depos_check, depos_savings; cout << "\n *** MY Bank ATM ***"; do { cout << "\n\nChoose C)hecking S)avings B)alance Ca)sh A Check E)xit: "; cin >> choice; // If choice is for the balance ...display totals of accounts. if (choice == "b" || choice == "B") { cout << "Checking Balance: $" << setw(8) << setprecision(2) << fixed << checking << '\n'; cout << "Savings Balance: $" << setw(8) << setprecision(2) << fixed << savings << endl; } // If choice is for Checking account, display option to withdraw/deposit if (choice == "c" || choice == "C") { cout << "\nW)ithdrawal or D)eposit? :"; cin >> depos_check; // If user asked to withdraw cash if (depos_check == "w" || depos_check == "W") { cout << "Enter amount to withdraw from checking account:"; cin >> withdraw_checking; // If withdraw leaves money in account....can do action. if (checking - withdraw_checking > 0) { checking = - withdraw_checking + checking + (checking * .05); } // if withdrawal is makes account under 0, then says sorry cannot do. else { cout << "Cannot withdraw $" << withdraw_checking << ".00 from account( $" << checking << ".00 )"; cout << "\nPlease deposit more funds or try a smaller withdrawal."; } } // If user asked to deposit funds. Asks how much then adds deposited funds to account (checking) if (depos_check == "d" || depos_check == "D") { cout << "Enter amount to deposit:"; cin >> deposit_checking; checking = deposit_checking + checking + (checking * .05); } } if (choice == "s" || choice == "S") { cout << "\nW)ithdrawal or D)eposit? :"; cin >> depos_savings; // If user asked to withdraw cash if (depos_savings == "w" || depos_savings == "W") { cout << "Enter amount to withdraw from savings account:"; cin >> withdraw_savings; // If withdraw leaves money in account....can do action. if (savings - withdraw_savings > 0) { savings = - withdraw_savings + savings + (savings * .025); } // if withdrawal makes account under 0, then says sorry cannot do. else { cout << "Cannot withdraw $" << withdraw_savings << ".00 from account( $" << savings << ".00 )"; cout << "\nPlease deposit more funds or try a smaller withdrawal."; } } // If user asked to deposit funds. Asks how much then adds deposited funds to account (savings) if (depos_savings == "d" || depos_savings == "D") { cout << "Enter amount to deposit:"; cin >> deposit_savings; savings = deposit_savings + savings + (savings * .025); } // If user asked to cash a check...input amount of check and display . if (choice == "ca" || choice == "Ca") { cout << "Enter amount of Check: " << setw(8) << setprecision(2) << fixed << check_amount << '\n'; cout << "Here is your cash: " << setw(8) << setprecision(2) << fixed << savings << endl; } if (choice == "e" || choice == "E") { exit(); } } // Asks user if they wish to perform another task cout << "\n\nMake another transaction? (Y/N):"; cin >> yesno; } while (yesno == "y" || yesno == "Y"); cout << "Thank you for using MY Bank ATM!"<<endl; return 0; } void exit() { cout << "Thank you for using MY Bank ATM!" <<endl; }
Last edited by programmingme; Jan 22nd, 2008 at 1:00 am.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
An overloaded function could be like this. You could have two functions with the same name but which take different parameters. For example, you could have a function called "calculate_balance" that takes no parameters and a function called "calculate_balance" that takes an integer, for example. Something like this:
The program knows which function to call by the parameters it is passed. So something like this:
C++ Syntax (Toggle Plain Text)
void calculate_balance (); // takes noparameters void calculate_balance (int number); // takes an integer
The program knows which function to call by the parameters it is passed. So something like this:
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; void calculate_balance (); // takes no parameters void calculate_balance (int number); // takes an integer int main () { calculate_balance (); // will call the first function calculate_balance (5); // will call the second function return 0; } void calculate_balance () { cout << "I am in calculate_balance. I was passed no parameters" << endl; } void calculate_balance (int number) { cout << "I am in calculate_balance. I was passed a " << number << endl; }
•
•
Join Date: Jan 2008
Posts: 18
Reputation:
Solved Threads: 0
If everything is declared the same - as double - how do I make the parameters different? I calculate the balance four different times and they all have the same format:
checking = - withdraw_checking + checking + (checking * .05);
checking = deposit_checking + checking + (checking * .05);
savings = - withdraw_savings + savings + (savings * .025);
savings = deposit_savings + savings + (savings * .025);
So what would I have to do to declare my overloaded function?
In my code in the first post I had those calculations inside the main but I have to move them into the overloaded function and call the function in the main.
checking = - withdraw_checking + checking + (checking * .05);
checking = deposit_checking + checking + (checking * .05);
savings = - withdraw_savings + savings + (savings * .025);
savings = deposit_savings + savings + (savings * .025);
So what would I have to do to declare my overloaded function?
In my code in the first post I had those calculations inside the main but I have to move them into the overloaded function and call the function in the main.
For a simple mathematical equation (in other words, something that can be done in one or two lines), why do you need a function?
To overload a function you have to have at least one of the following
Hope this helps.
To overload a function you have to have at least one of the following
- a different number of parameters
- at least one corresponding parameter of different types
Hope this helps.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
The only way I can think of overloading a function that takes two doubles as parameters like that and overload them so they are different is something like this:
That's four different functions, all taking two doubles and returning a double. They are slightly different in that some of the parameters are double and some are pointers to doubles. Either that or something kind of silly like this:
There you are adding meaningless parameters to the end that you don't need or use, but are just passing them in order to overload the function.
C++ Syntax (Toggle Plain Text)
double calculate_balance (double, double); double calculate_balance (double*, double); double calculate_balance (double, double*); double calculate_balance (double*, double*);
That's four different functions, all taking two doubles and returning a double. They are slightly different in that some of the parameters are double and some are pointers to doubles. Either that or something kind of silly like this:
C++ Syntax (Toggle Plain Text)
double calculate_balance (double, double); double calculate_balance (double, double, bool); double calculate_balance (double, double, bool, bool); double calculate_balance (double, double, int);
There you are adding meaningless parameters to the end that you don't need or use, but are just passing them in order to overload the function.
That would work, but it is not recommended.
The purpose of overloading a function is to allow the same operation to be done on different data.
For example, if you want to print a string you might have a couple of overloaded functions:
This allows you to account for the different ways in which you may think of a "string".
However, no matter how the string is represented the underlying operation is the same: the string is printed. That is to say that the purpose of overloading a function is not to give functions that do different things the same name. The functions may have to accomplish their goal differently, because the data given them are different, but the end result is the same: something gets printed.
In your case, then, I don't think you ought to overload anything. You are doing four distinct things: 1) depositing to checking, 2) withdrawing from checking, 3) depositing to savings, 4) withdrawing from savings. Since you are doing four things, make four functions.
Given that the only variance between depositing and withdrawing is the sign of the amount of money added, you could combine the checking functions into one, and the saving functions into another:
So to deposit $12.00 into savings, you would say:
And to withdraw $21.99 from checking, you would say:
Hope this helps.
The purpose of overloading a function is to allow the same operation to be done on different data.
For example, if you want to print a string you might have a couple of overloaded functions:
void print( const char *s );void print( const std::string s );This allows you to account for the different ways in which you may think of a "string".
However, no matter how the string is represented the underlying operation is the same: the string is printed. That is to say that the purpose of overloading a function is not to give functions that do different things the same name. The functions may have to accomplish their goal differently, because the data given them are different, but the end result is the same: something gets printed.
In your case, then, I don't think you ought to overload anything. You are doing four distinct things: 1) depositing to checking, 2) withdrawing from checking, 3) depositing to savings, 4) withdrawing from savings. Since you are doing four things, make four functions.
Given that the only variance between depositing and withdrawing is the sign of the amount of money added, you could combine the checking functions into one, and the saving functions into another:
double calculate_checking_balance( double capital, double principal );double calculate_savings_balance( double capital, double principal );So to deposit $12.00 into savings, you would say:
savings = calculate_savings_balance( savings, 12.00 );And to withdraw $21.99 from checking, you would say:
checking = calculate_checking_balance( checking, -21.99 );Hope this helps.
![]() |
Similar Threads
- my max function in my array wont work (C++)
- I have just need help with one function... (C++)
- Term does not evaluate to a function (C++)
- help with creating and calling a function (C++)
- function calls? (C++)
- Error message help? (C++)
- QuickSort (Computer Science)
- Interpretation of an instructors C++ program... (C++)
Other Threads in the C++ Forum
- Previous Thread: kind of aggravating
- Next Thread: Average?
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets







Okay!