C++/Bank Accounts Program
Expert: Zlatko - 4/23/2010
QuestionI am so overwhelmed, I have no sense of direction. I'm not clear about virtual functions etc. I'm in desperate need, could you please help me out.
Here are the instructions:
Design a class to hold the following information about a bank account:
Balance
Number of deposits this month
Number of withdrawals
Annual interest rate
Monthly service charges
The class should have the following member functions:
constructor Accepts arguments for the balance and annual interest rate
deposit A virtual function that accepts an argument for the amount of the deposit. The function should add the argument to the account balance. It should increment the variable holding the number of deposits.
Handle any exception that may occur here.
withdraw A virtual function that accepts an argument for the amount of the withdrawal. The function should subtract the argument from the account balance. It should increment the variable holding the number of withdrawals.
Handle any exception that may occur here.
calcInt A virtual function that updates the balance by calculating the monthly interest earned by the account, and adding this interest to the balance. This is performed by the following formulas:
Monthly Interest Rate = Annual Interest Rate / 12
Monthly Interest = Balance * Monthly Interest Rate
Balance = Balance + Monthly Interest
monthlyProc A virtual function that subtracts the monthly service charges from the balance, calls the calcInt function, and then sets the variables that hold the number of withdrawals, number of deposits, and monthly service charges to zero.
Next design a savings account class, derived from the account class. The savings account class should have the following additional number:
status ( to represent an active or inactive account )
If the balance of a savings account falls below $25, it becomes inactive. No more withdrawals may be made until the balance is raised above $25, at which time the account becomes active again. The savings account class should have the following member functions:
deposit A function that checks to see if the account is inactive before the deposits is made. If the account is inactive and the deposit brings the balance above $25, the account becomes active again. The deposit is then made by calling the base class function.
withdraw A function that checks to see if the account is inactive before the withdrawal is made. A withdrawal is then made by calling the base class function.
monthlyProc Before the base class function is called, this function checks the number of withdrawals. If the number of withdrawals for the month is more than 4, a service charge of $1 for each withdrawal above 4 is added to the base class variables that holds the monthly service charges.
Next design a checking account class, also derived from the account class. It should have the following member functions:
withdraw Before the base class function is called, this function will determine if a withdrawal will cause the balance to go below $0. If the balance goes below $0, a service charge of $15 will be taken from the account. If there isnt enough in the account to pay the service charge, the balance will become negative and the customer will owe the negative amount to the bank.
monthlyProc Before the base class function is called, this function adds the monthly fee of $5 plus $0.10 per withdrawal to the base class variables that holds the monthly service charges.
Write a complete program that demonstrates these classes by asking the user to enter the amounts of deposits and withdrawals for a savings account and checking account. The program should display statistics for the month, including beginning balance, total amount of deposits, total amount of withdrawals, service charges and ending balance.
This is what I have:
#include <iostream.h> //predefined header file
#include <stdlib.h>
#include <conio.h>
#include <iomanip.h>
5. #include <stdio.h>
6. #include <dos.h>
7. #include <ctype.h>
8.
9. class Accounts { //name of class
10. protected:
11. double balance;
12.
13. public: //public member function
14. Accounts();
15. ~Accounts();
16.
17. double getbal() const;
18.
19. void withdraw (double);
20. double deposit(double);
21. };
22.
23.
24. class savingsAccount: public Accounts {
25. public:
26. savingsAccount(double = 0.0);
27. ~savingsAccount();
28.
29. void payinterest();
30. void setrate(double);
31.
32. protected: //protected member function
33. double rate;
34. };
35.
36. class checkingAccount : public Accounts {
37. public:
38. checkingAccount(double = 0.0);
39. ~checkingAccount();
40.
41. void withdraw (double);
42.
43. protected:
44. double fees;
45. };
46.
47.
48. Accounts::Accounts() : balance(0.0) {
49. }
50.
51. Accounts::~Accounts(){
52. }
53.
54. double Accounts::getbal() const {
55. return balance;
56. }
57.
58. void Accounts::withdraw (double amount) {
59. if (amount > 0 && balance >= amount) {
60. balance -= amount;
61. }
62. }
63.
64. double Accounts::deposit (double amount) {
65. if (amount > 0) {
66. balance += amount;
67. }
68.
69. return balance;
70. }
71.
72.
73. savingsAccount::savingsAccount(double initbalance) : rate(0.0) {
74. balance = initbalance;
75. }
76.
77. savingsAccount::~savingsAccount () {
78. }
79.
80. void savingsAccount::payinterest() {
81. balance = balance*rate/365/100;
82. }
83.
84. void savingsAccount::setrate(double r) {
85. if (r >= 0.0) {
86. rate = r;
87. }
88. }
89.
90.
91. checkingAccount::checkingAccount(double initbalance) : fees(0.0) {
92. balance = initbalance;
93. }
94.
95. checkingAccount::~checkingAccount() {
96. }
97.
98. void checkingAccount::withdraw(double amount) {
99. if (amount > 0 && balance >= amount) {
100. balance -= (amount+fees);
101. }
102. }
103.
104. void main() {
105. Accounts a;
106. savingsAccount sa;
107. checkingAccount ca;
108.
109. sa.setrate(16.30);
110.
111. cout<<""<<endl;
112. cout<<""<<endl;
113. cout<<""<<endl;
114. cout<<" WELCOME TO MY ACOOUNTING SYSTEM "<<endl;
115. cout<<""<<endl;
116. cout<<""<<endl;
117. cout<<"Please wait for system load"<<endl;
118. cout<<""<<endl;
119. cout<<" loading..."<<endl;
120.
121. sleep(3);
122. clrscr();
123.
124. char choice;
125. cout<<" MAIN MENU"<<endl;
126. cout<<""<<endl;
127. cout<<""<<endl;
128. cout<<" 1: Deposit"<<endl;
129. cout<<" 2: Withdraw"<<endl;
130. cout<<" 3: Account Balance"<<endl;
131. cout<<" 4: Exit"<<endl;
132. cout<<"Make Selection using the numbers ('X' to exit)"<<endl;
133. cout<<"---> "<<endl;
134.
135. choice=toupper(getchar());
136. fflush (stdin);
137. switch(choice)
138.
139. {
140. int dep;
141. case '1':
142. cout<<"Enter amount of money to deposit:"<< endl;
143. cin>>dep;
144. sa.payinterest();
145. break;
146.
147. case '2':
148. a.getbal();
149. break;
150.
151. case '3':
152. a.getbal();
153. break;
154.
155. case '4':
156.
157. break;
158. default:
159. cout<<"Invalid menu item...Please re-enter your choice"<<endl;
160. sleep(2);
161. break;
162. }//end of case
163.
164.
165.
166.
167.
168.
169. cout<<""<<endl;
170. cout<<"Savings Account Balance Before: $"<<sa.getbal()<<endl;
171. cout<<"Checking Account Balance Before: $"<<ca.getbal()<<endl<<endl;
172.
173. //we do some operations here
174. ca.withdraw(6700.43);
175. sa.payinterest();
176. sa.deposit(1500.00);
177.
178. cout<<"Savings Account Balance After: $"<<sa.getbal()<<endl;
179. cout<<"Checking Account Balance After: $"<<ca.getbal()<<endl;
cout<<endl;endl;
system("pause");
AnswerHello Alex.
I understand your frustration. Virtual functions are one of the more confusing parts of C++. Your question has inspired me to write a short tutorial. You should read it first, before going on with your assignment. You can see it at
http://docs.google.com/Doc?docid=0AUydEjitXTI4ZGY2NWhybXdfMjBmY2M5em1kZA&hl=en
The next piece of advice I have for you is to not panic when you get a lot of information on your assignment (your spec). The key is to break the spec down into smaller pieces and focus on one piece at a time. Look at all the things the spec says about the Account class. Have you implemented all the data fields ?
Balance
Number of deposits this month
Number of withdrawals
Annual interest rate
Monthly service charges
The spec is there to help you, but you must read it carefully.
No worries. I will show you by example, and you can continue on. Below is code that declares the Account, the SavingsAccount, and the CheckingAccount, according to the spec. The spec clearly outlines how each method is to be implemented. I'll leave the implementation up to you. If you need more help, I'll be checking my e-mail this weekend (just in case your assignment is due Monday morning)
About the code below, my comments start with /* ZM or //ZM . My comments try to point you in the right direction. They explain things I added. I also use comments to disable code that should not be there.
REMEMBER, the code below is not the whole program. Don't blindly paste it over your file or you may lose your old work.
#include <iostream.h> //predefined header file
#include <iomanip.h>
#include <stdlib.h>
#include <conio.h>
#include <stdio.h>
#include <dos.h>
#include <ctype.h>
/* ZM The instructions are quite clear. You just need to organize them
and not get overwhelmed by the size of the problem. Divide the problem
up and work on each part separately.
*/
class Accounts { //name of class
protected:
double balance;
//ZM adding the rest of the data from the spec
int monthlyDepCount;
int monthlyWdCount;
int annualIntRate;
int monthlySvcCh;
public: //public member function
Accounts();
~Accounts();
double getbal() const;
//ZM the specs say withdraw must be virtual
//ZM it is good style to name your parameters even in the function declaration
virtual void withdraw (double amount);
//ZM the specs say nothing about returning a value from deposit.
//ZM if you want to return the balance, then do the same for withdraw.
//ZM the specs say it must be virtual
virtual void deposit(double amount);
/* ZM just follow the instructions
the spec states that calcInt is to be virtual, although it is not clear to me why.
Anyway, we will follow the spec.
*/
virtual void calcInt(void);
virtual void monthlyProc(void);
};
class savingsAccount: public Accounts {
public:
/* ZM you will need more parameters, like annual interest rate, and monthly service charge */
savingsAccount(double initbalance = 0.0);
~savingsAccount();
/* ZM these are not the function names in the spec
void payinterest();
void setrate(double);
*/
/* ZM
When virtual functions are in the base class, you can expect the
same function names in the derived classes.
Below are the functions in the spec. I specify them as virtual only
because they are virtual in the base class. It is not strictly necessary
to do so.
*/
virtual void withdraw (double amount);
virtual void deposit(double amount);
virtual void monthlyProc(void);
protected: //protected member function
//ZM the interest rate is in the base class, not here
//double rate;
//ZM this function needs a status as specified in the spec
//ZM lets be fancy and use an enum
enum Status
{
Active = 1,
Inactive
};
Status status;
};
class checkingAccount : public Accounts {
public:
/* ZM you will need more parameters, like annual interest rate, and monthly service charge */
checkingAccount(double initBalance = 0.0);
~checkingAccount();
virtual void withdraw (double amount);
virtual void monthlyProc(void);
protected:
double fees;
};
/* ZM
Now take your spec, and copy it into the code, in front of each function for the Account class.
The spec clearly states what each function is to do. Use the spec to implement the functions.
Do the same for the Savings and Checking classes. This is basically your code, that you sent me.
Some of it is good, but unfinished.
*/
//withdraw: A virtual function that accepts an argument for the amount of the withdrawal.
//The function should subtract the argument from the account balance. It
//should increment the variable holding the number of withdrawals.
//Handle any exception that may occur here.
void Accounts::withdraw (double amount) {
if (amount > 0 && balance >= amount) {
balance -= amount;
}
}
//deposit: A virtual function that accepts an argument for the amount of
//the deposit. The function should add the argument to the account
//balance. It should increment the variable holding the number of
//deposits.
//Handle any exception that may occur here.
void Accounts::deposit (double amount) {
if (amount > 0) {
balance += amount;
}
}
//calcInt: A virtual function that updates the balance by calculating the monthly interest earned by
//the account, and adding this interest to the balance. This is
//performed by the following formulas:
//Monthly Interest Rate = Annual Interest Rate / 12
//Monthly Interest = Balance * Monthly Interest Rate
//Balance = Balance + Monthly Interest
void Accounts::calcInt (void) {
}
//monthlyProc: A virtual function that subtracts the monthly service charges from the balance, calls the
//calcInt function, and then sets the variables that hold the number of
//withdrawals, number of deposits, and monthly service charges to zero.
void Accounts::monthlyProc (void) {
}
double Accounts::getbal() const {
return balance;
}