Overloaded Function Help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2008
Posts: 18
Reputation: programmingme is an unknown quantity at this point 
Solved Threads: 0
programmingme programmingme is offline Offline
Newbie Poster

Overloaded Function Help

 
0
  #1
Jan 22nd, 2008
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. #include <iostream>
  2. #include <iomanip>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. //void calculate_balance(
  8. //void calculate_balance(
  9. void exit();
  10.  
  11. int main()
  12. {
  13.  
  14. double savings = 1000;
  15. double checking = 1000;
  16. double withdraw_checking = 0;
  17. double withdraw_savings = 0;
  18. double deposit_checking = 0;
  19. double deposit_savings = 0;
  20. double check_amount;
  21. string choice, yesno, depos_check, depos_savings;
  22.  
  23.  
  24. cout << "\n *** MY Bank ATM ***";
  25.  
  26. do
  27. {
  28. cout << "\n\nChoose C)hecking S)avings B)alance Ca)sh A Check E)xit: ";
  29. cin >> choice;
  30.  
  31.  
  32. // If choice is for the balance ...display totals of accounts.
  33. if (choice == "b" || choice == "B")
  34. {
  35. cout << "Checking Balance: $" << setw(8) << setprecision(2) << fixed
  36. << checking << '\n';
  37. cout << "Savings Balance: $" << setw(8) << setprecision(2) << fixed
  38. << savings << endl;
  39. }
  40.  
  41. // If choice is for Checking account, display option to withdraw/deposit
  42. if (choice == "c" || choice == "C")
  43. {
  44. cout << "\nW)ithdrawal or D)eposit? :";
  45. cin >> depos_check;
  46.  
  47. // If user asked to withdraw cash
  48. if (depos_check == "w" || depos_check == "W")
  49. {
  50. cout << "Enter amount to withdraw from checking account:";
  51. cin >> withdraw_checking;
  52.  
  53. // If withdraw leaves money in account....can do action.
  54. if (checking - withdraw_checking > 0)
  55. {
  56. checking = - withdraw_checking + checking + (checking * .05);
  57. }
  58.  
  59. // if withdrawal is makes account under 0, then says sorry cannot do.
  60. else
  61. {
  62. cout << "Cannot withdraw $" << withdraw_checking << ".00 from account( $"
  63. << checking << ".00 )";
  64. cout << "\nPlease deposit more funds or try a smaller withdrawal.";
  65. }
  66. }
  67.  
  68. // If user asked to deposit funds. Asks how much then adds deposited funds to
  69. account (checking)
  70. if (depos_check == "d" || depos_check == "D")
  71. {
  72. cout << "Enter amount to deposit:";
  73. cin >> deposit_checking;
  74. checking = deposit_checking + checking + (checking * .05);
  75. }
  76. }
  77.  
  78. if (choice == "s" || choice == "S")
  79. {
  80. cout << "\nW)ithdrawal or D)eposit? :";
  81. cin >> depos_savings;
  82.  
  83. // If user asked to withdraw cash
  84. if (depos_savings == "w" || depos_savings == "W")
  85. {
  86. cout << "Enter amount to withdraw from savings account:";
  87. cin >> withdraw_savings;
  88.  
  89. // If withdraw leaves money in account....can do action.
  90. if (savings - withdraw_savings > 0)
  91. {
  92. savings = - withdraw_savings + savings + (savings * .025);
  93. }
  94.  
  95. // if withdrawal makes account under 0, then says sorry cannot do.
  96. else
  97. {
  98. cout << "Cannot withdraw $" << withdraw_savings << ".00 from account( $" <<
  99. savings << ".00 )";
  100. cout << "\nPlease deposit more funds or try a smaller withdrawal.";
  101. }
  102. }
  103.  
  104. // If user asked to deposit funds. Asks how much then adds deposited funds to
  105. account (savings)
  106. if (depos_savings == "d" || depos_savings == "D")
  107. {
  108. cout << "Enter amount to deposit:";
  109. cin >> deposit_savings;
  110. savings = deposit_savings + savings + (savings * .025);
  111. }
  112.  
  113. // If user asked to cash a check...input amount of check and display .
  114. if (choice == "ca" || choice == "Ca")
  115. {
  116. cout << "Enter amount of Check: " << setw(8) << setprecision(2) << fixed <<
  117. check_amount << '\n';
  118. cout << "Here is your cash: " << setw(8) << setprecision(2) << fixed <<
  119. savings << endl;
  120. }
  121.  
  122. if (choice == "e" || choice == "E")
  123. {
  124. exit();
  125. }
  126. }
  127.  
  128. // Asks user if they wish to perform another task
  129. cout << "\n\nMake another transaction? (Y/N):";
  130. cin >> yesno;
  131.  
  132. }
  133. while (yesno == "y" || yesno == "Y");
  134.  
  135.  
  136. cout << "Thank you for using MY Bank ATM!"<<endl;
  137.  
  138. return 0;
  139.  
  140. }
  141.  
  142. void exit()
  143. {
  144. cout << "Thank you for using MY Bank ATM!" <<endl;
  145. }
Last edited by programmingme; Jan 22nd, 2008 at 1:00 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Overloaded Function Help

 
0
  #2
Jan 22nd, 2008
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:

  1. void calculate_balance (); // takes noparameters
  2. 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:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void calculate_balance (); // takes no parameters
  5. void calculate_balance (int number); // takes an integer
  6.  
  7. int main ()
  8. {
  9. calculate_balance (); // will call the first function
  10. calculate_balance (5); // will call the second function
  11. return 0;
  12. }
  13.  
  14.  
  15. void calculate_balance ()
  16. {
  17. cout << "I am in calculate_balance. I was passed no parameters" << endl;
  18. }
  19.  
  20.  
  21. void calculate_balance (int number)
  22. {
  23. cout << "I am in calculate_balance. I was passed a " << number << endl;
  24. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: programmingme is an unknown quantity at this point 
Solved Threads: 0
programmingme programmingme is offline Offline
Newbie Poster

Re: Overloaded Function Help

 
0
  #3
Jan 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Overloaded Function Help

 
0
  #4
Jan 22nd, 2008
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
  • a different number of parameters
  • at least one corresponding parameter of different types
(The result type doesn't make a difference.)

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Overloaded Function Help

 
0
  #5
Jan 22nd, 2008
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:


  1. double calculate_balance (double, double);
  2. double calculate_balance (double*, double);
  3. double calculate_balance (double, double*);
  4. 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:

  1. double calculate_balance (double, double);
  2. double calculate_balance (double, double, bool);
  3. double calculate_balance (double, double, bool, bool);
  4. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Overloaded Function Help

 
0
  #6
Jan 22nd, 2008
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:
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: programmingme is an unknown quantity at this point 
Solved Threads: 0
programmingme programmingme is offline Offline
Newbie Poster

Re: Overloaded Function Help

 
0
  #7
Jan 22nd, 2008
Thanks for all your responses. Duoas the reason I have to use overloaded function is because that's what my instructor required. Didn't make any sense to me but that's what they want.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: programmingme is an unknown quantity at this point 
Solved Threads: 0
programmingme programmingme is offline Offline
Newbie Poster

Re: Overloaded Function Help

 
0
  #8
Jan 22nd, 2008
Double Post
Last edited by programmingme; Jan 22nd, 2008 at 3:40 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Overloaded Function Help

 
0
  #9
Jan 22nd, 2008
Show your professor my post, ask him what we have misunderstood about your assignment, and post back with the response.
Last edited by Duoas; Jan 22nd, 2008 at 3:45 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: programmingme is an unknown quantity at this point 
Solved Threads: 0
programmingme programmingme is offline Offline
Newbie Poster

Re: Overloaded Function Help

 
0
  #10
Jan 22nd, 2008
Okay!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC