1. // brass.h -- bank account classes
  2. #ifndef BRASS_H_
  3. #define BRASS_H_
  4. #include <string>
  5. // Brass Account Class
  6. class Brass
  7. {
  8. private:
  9. std::string fullName;
  10. long acctNum;
  11. double balance;
  12. public:
  13. Brass(const std::string & s = "Nullbody", long an = -1,
  14. double bal = 0.0);
  15. void Deposit(double amt);
  16. virtual void Withdraw(double amt);
  17. double Balance() const;
  18. virtual void ViewAcct() const;
  19. virtual ~Brass() {}
  20. };
  21. //Brass Plus Account Class
  22. class BrassPlus : public Brass
  23. {
  24. private:
  25. double maxLoan;
  26. double rate;
  27. double owesBank;
  28. public:
  29. BrassPlus(const std::string & s = "Nullbody", long an = -1,
  30. double bal = 0.0, double ml = 500,
  31. double r = 0.11125);
  32. BrassPlus(const Brass & ba, double ml = 500,
  33. double r = 0.11125);
  34. virtual void ViewAcct()const;
  35. virtual void Withdraw(double amt);
  36. void ResetMax(double m) { maxLoan = m; }
  37. void ResetRate(double r) { rate = r; };
  38. void ResetOwes() { owesBank = 0; }
  39. };
  40. #endif
  41. // brass.cpp -- bank account class methods
  42. #include <iostream>
  43. #include "brass.h"
  44. using std::cout;
  45. using std::endl;
  46. using std::string;
  47. // formatting stuff
  48. typedef std::ios_base::fmtflags format;
  49. typedef std::streamsize precis;
  50. format setFormat();
  51. void restore(format f, precis p);
  52. // Brass methods
  53. Brass::Brass(const string & s, long an, double bal)
  54. {
  55. fullName = s;
  56. acctNum = an;
  57. balance = bal;
  58. }
  59. void Brass::Deposit(double amt)
  60. {
  61. if (amt < 0)
  62. cout << "Negative deposit not allowed; "
  63. << "deposit is cancelled.\n";
  64. else
  65. balance += amt;
  66. }
  67. void Brass::Withdraw(double amt)
  68. {
  69. // set up ###.## format
  70. format initialState = setFormat();
  71. precis prec = cout.precision(2);
  72. if (amt < 0)
  73. cout << "Withdrawal amount must be positive; "
  74. << "withdrawal canceled.\n";
  75. else if (amt <= balance)
  76. balance -= amt;
  77. else
  78. cout << "Withdrawal amount of $" << amt
  79. << " exceeds your balance.\n"
  80. << "Withdrawal canceled.\n";
  81. restore(initialState, prec);
  82. }
  83. double Brass::Balance() const
  84. {
  85. return balance;
  86. }
  87. void Brass::ViewAcct() const
  88. {
  89. // set up ###.## format
  90. format initialState = setFormat();
  91. precis prec = cout.precision(2);
  92. cout << "Client: " << fullName << endl;
  93. cout << "Account Number: " << acctNum << endl;
  94. cout << "Balance: $" << balance << endl;
  95. restore(initialState, prec); // Restore original format
  96. }
  97. // BrassPlus Methods
  98. BrassPlus::BrassPlus(const string & s, long an, double bal,
  99. double ml, double r) : Brass(s, an, bal)
  100. {
  101. maxLoan = ml;
  102. owesBank = 0.0;
  103. rate = r;
  104. }
  105. BrassPlus::BrassPlus(const Brass & ba, double ml, double r)
  106. : Brass(ba) // uses implicit copy constructor
  107. {
  108. maxLoan = ml;
  109. owesBank = 0.0;
  110. rate = r;
  111. }
  112. // redefine how ViewAcct() works
  113. void BrassPlus::ViewAcct() const
  114. {
  115. // set up ###.## format
  116. format initialState = setFormat();
  117. precis prec = cout.precision(2);
  118. Brass::ViewAcct(); // display base portion
  119. cout << "Maximum loan: $" << maxLoan << endl;
  120. cout << "Owed to bank: $" << owesBank << endl;
  121. cout.precision(3); // ###.### format
  122. cout << "Loan Rate: " << 100 * rate << "%\n";
  123. restore(initialState, prec);
  124. }
  125. // redefine how Withdraw() works
  126. void BrassPlus::Withdraw(double amt)
  127. {
  128. // set up ###.## format
  129. format initialState = setFormat();
  130. precis prec = cout.precision(2);
  131. double bal = Balance();
  132. if (amt <= bal)
  133. Brass::Withdraw(amt);
  134. else if ( amt <= bal + maxLoan - owesBank)
  135. {
  136. double advance = amt - bal;
  137. owesBank += advance * (1.0 + rate);
  138. cout << "Bank advance: $" << advance << endl;
  139. cout << "Finance charge: $" << advance * rate << endl;
  140. Deposit(advance);
  141. Brass::Withdraw(amt);
  142. }
  143. else
  144. cout << "Credit limit exceeded. Transaction cancelled.\n";
  145. restore(initialState, prec);
  146. }
  147. format setFormat()
  148. {
  149. // set up ###.## format
  150. return cout.setf(std::ios_base::fixed,
  151. std::ios_base::floatfield);
  152. }
  153. // usebrass2.cpp -- polymorphic example
  154. // compile with brass.cpp
  155. #include <iostream>
  156. #include <string>
  157. #include "brass.h"
  158. const int CLIENTS = 4;
  159. int main()
  160. {
  161. using std::cin;
  162. using std::cout;
  163. using std::endl;
  164. Brass * p_clients[CLIENTS];
  165. std::string temp;
  166. long tempnum;
  167. double tempbal;
  168. char kind;
  169. for (int i = 0; i < CLIENTS; i++)
  170. {
  171. cout << "Enter client's name: ";
  172. getline(cin,temp);
  173. cout << "Enter client's account number: ";
  174. cin >> tempnum;
  175. cout << "Enter opening balance: $";
  176. cin >> tempbal;
  177. cout << "Enter 1 for Brass Account or "
  178. << "2 for BrassPlus Account: ";
  179. while (cin >> kind && (kind != '1' && kind != '2'))
  180. cout <<"Enter either 1 or 2: ";
  181. if (kind == '1')
  182. p_clients[i] = new Brass(temp, tempnum, tempbal);
  183. else
  184. {
  185. double tmax, trate;
  186. cout << "Enter the overdraft limit: $";
  187. cin >> tmax;
  188. cout << "Enter the interest rate "
  189. << "as a decimal fraction: ";
  190. cin >> trate;
  191. p_clients[i] = new BrassPlus(temp, tempnum, tempbal,
  192. tmax, trate);
  193. }
  194. while (cin.get() != '\n')
  195. continue;
  196. }
  197. cout << endl;
  198. for (int i = 0; i < CLIENTS; i++)
  199. {
  200. p_clients[i]->ViewAcct();
  201. cout << endl;
  202. }
  203. for (int i = 0; i < CLIENTS; i++)
  204. {
  205. delete p_clients[i]; // free memory
  206. }
  207. cout << "Done.\n";
  208. /* code to keep window open
  209. if (!cin)
  210. cin.clear();
  211. while (cin.get() != '\n')
  212. continue;
  213. */
  214. return 0;
  215. }