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