1. #ifndef _ACCTABC_H_
  2. #define _ACCTABC_H_
  3. //(*
  4. #include <iostream>
  5. #include <string>
  6. //*)
  7. //Abstract Base Class
  8. class AcctABC
  9. {
  10. private:
  11. std::string fullName;
  12. long acctNum;
  13. double balance;
  14.  
  15. protected:
  16. struct Formatting
  17. {
  18. std::ios_base::fmtflags flag;
  19. std::streamsize pr;
  20. };
  21. const std::string & FullName(void) const {return fullName;}
  22. long AcctNum(void) const {return acctNum;}
  23. Formatting SetFormat(void) const;
  24. void Restore(Formatting & f) const;
  25.  
  26. public:
  27. AcctABC(const std::string & s = "Nullbody", long an = -, double bal = 0.0);
  28. void Deposit(double amt);
  29. virtual void Withdraw(double amt) = ; //pure virtual function
  30. double Balance(void) const {return balance;};
  31. virtual void ViewAcct(void) const = ; //pure virtual function
  32. virtual ~AcctABC() {}
  33. };
  34.  
  35. //Brass Account Class
  36. class Brass : public AcctABC
  37. {
  38. public:
  39. Brass(const std::string & s = "Nullbody", long an = -, double bal = 0.0) : AcctABC(s, an, bal) {}
  40. virtual void Withdraw(double amt);
  41. virtual void ViewAcct(void) const;
  42. virtual ~Brass() {}
  43. };
  44.  
  45. //Brass Plus Account Class
  46. class BrassPlus : public AcctABC
  47. {
  48. private:
  49. double maxLoan;
  50. double rate;
  51. double owesBank;
  52.  
  53. public:
  54. BrassPlus(const std::string & s = "Nullbodt", long an = -, double bal = 0.0, double ml = , double r = 0.10);
  55. BrassPlus(const Brass & ba, double ml = , double r = 0.1);
  56. virtual void ViewAcct(void) const;
  57. virtual void Withdraw(double amt);
  58. void ResetMax(double m) {maxLoan = m;}
  59. void ResetRate(double r) {rate = r;}
  60. void ResetOwes(void) {owesBank = ;}
  61. };
  62.  
  63. #endif
  1. #include <iostream>
  2. #include "acctabc.h"
  3. using std::cout;
  4. using std::ios_base;
  5. using std::endl;
  6. using std::string;
  7.  
  8. //Abstract Base class
  9. AcctABC::AcctABC(const string & s, long an, double bal)
  10. {
  11. fullName = s;
  12. acctNum = an;
  13. balance = bal;
  14. }
  15.  
  16. void AcctABC::Deposit(double amt)
  17. {
  18. if(amt < )
  19. {
  20. cout << "Negative deposit not allowed; deposit is cancelled." << endl;
  21. }
  22. else
  23. {
  24. balance += amt;
  25. }
  26. }
  27.  
  28. void AcctABC::Withdraw(double amt)
  29. {
  30. balance -= amt;
  31. }
  32.  
  33. //protected methods for formatting
  34. AcctABC::Formatting AcctABC::SetFormat(void) const
  35. {
  36. //set up ###.## format
  37. Formatting f;
  38. f.flag = cout.setf(ios_base::fixed, ios_base::floatfield);
  39. f.pr = cout.precision();
  40. return f;
  41. }
  42.  
  43. void AcctABC::Restore(Formatting & f) const
  44. {
  45. cout.setf(f.flag, ios_base::floatfield);
  46. cout.precision(f.pr);
  47. }
  48.  
  49. //Brass methods
  50. void Brass::Withdraw(double amt)
  51. {
  52. if(amt < )
  53. {
  54. cout << "Withdrawal amount must be positive; withdrawal canceled." << endl;
  55. }
  56. else if(amt < Balance())
  57. {
  58. AcctABC::Withdraw(amt);
  59. }
  60. else
  61. {
  62. cout << "Withdrawal amount of $" << amt << " exceeds your balance." << endl << "Withdrawal canceled." << endl;
  63. }
  64. }
  65.  
  66. void Brass::ViewAcct(void) const
  67. {
  68. Formatting f = SetFormat();
  69. cout << "Brass Client: " << FullName() << endl;
  70. cout << "Account Number: " << AcctNum() << endl;
  71. cout << "Balance: $" << Balance() << endl;
  72. Restore(f);
  73. }
  74.  
  75. //BrassPlus Methods
  76. BrassPlus::BrassPlus(const string & s, long an, double bal, double ml, double r) : AcctABC(s, an, bal)
  77. {
  78. maxLoan = ml;
  79. owesBank = 0.0;
  80. rate = r;
  81. }
  82.  
  83. BrassPlus::BrassPlus(const Brass & ba, double ml, double r) : AcctABC(ba)
  84. {
  85. maxLoan = ml;
  86. owesBank = 0.0;
  87. rate = r;
  88. }
  89.  
  90. void BrassPlus::ViewAcct(void) const
  91. {
  92. Formatting f = SetFormat();
  93.  
  94. cout << "BrassPlus Client: " << FullName() << endl;
  95. cout << "Account Number: " << AcctNum() << endl;
  96. cout << "Balance: $" << Balance() << endl;
  97. cout << "Maxinum loan: $" << maxLoan <<endl;
  98. cout << "Owed to bank: $" << owesBank << endl;
  99. cout.precision();
  100. cout << "Loan Rate: " << * rate << "%\n";
  101. Restore(f);
  102. }
  103.  
  104. void BrassPlus::Withdraw(double amt)
  105. {
  106. Formatting f = SetFormat();
  107.  
  108. double bal = Balance();
  109. if(amt <= bal)
  110. {
  111. AcctABC::Withdraw(amt);
  112. }
  113. else if(amt <= bal + maxLoan - owesBank)
  114. {
  115. double advance = amt - bal;
  116. owesBank += advance * (1.0 + rate);
  117. cout << "Bank advance: $" << advance * rate << endl;
  118. Deposit(advance);
  119. AcctABC::Withdraw(amt);
  120. }
  121. else
  122. {
  123. cout << "Credit limit exceeded. Transaction cancelled." << endl;
  124. }
  125. Restore(f);
  126. }
  1. #include <iostream>
  2. #include <string>
  3. #include "acctabc.h"
  4. const int CLIENTS = ;
  5.  
  6. int main(void)
  7. {
  8. using std::cin;
  9. using std::cout;
  10. using std::endl;
  11.  
  12. AcctABC * p_clients[CLIENTS];
  13. std::string temp;
  14. long tempnum;
  15. double tempbal;
  16. char kind;
  17.  
  18. for(int i = ; i < CLIENTS; i++)
  19. {
  20. cout << "Enter client's name: ";
  21. getline(cin, temp);
  22. cout << "Enter client's account number: ";
  23. cin >> tempnum;
  24. cout << "Enter opening balance: $";
  25. cin >> tempbal;
  26. cout << "Enter 1 for Brass Account or 2 for BrassPlus Account: ";
  27.  
  28. while(cin >> kind && (kind != '' && kind != ''))
  29. {
  30. cout << "Enter either 1 or 2: ";
  31. }
  32.  
  33. if(kind == '')
  34. {
  35. p_clients[i] = new Brass(temp, tempnum, tempbal);
  36. }
  37. else
  38. {
  39. double tmax, trate;
  40. cout << "Enter the overdraft limit: $";
  41. cin >> tmax;
  42. cout << "Enter the interest rate " << "as a decimal fraction: ";
  43. cin >> trate;
  44. p_clients[i] = new BrassPlus(temp, tempnum, tempbal, tmax, trate);
  45. }
  46.  
  47. while(cin.get() != '\n')
  48. {
  49. continue;
  50. }
  51. }
  52. cout << endl;
  53. for(int i = ; i < CLIENTS; i++)
  54. {
  55. p_clients[i]->ViewAcct();
  56. cout << endl;
  57. }
  58.  
  59. for(int i = ; i < CLIENTS; i++)
  60. {
  61. delete p_clients[i];
  62. }
  63. cout << "DONE.\n";
  64.  
  65. return ;
  66. }

抽象基类(ABC),纯虚函数的更多相关文章

  1. C++:抽象基类和纯虚函数的理解

    转载地址:http://blog.csdn.net/acs713/article/details/7352440 抽象类是一种特殊的类,它是为了抽象和设计的目的为建立的,它处于继承层次结构的较上层. ...

  2. (转)(C++)关于抽象基类和纯虚函数

    ★抽象类:一个类可以抽象出不同的对象来表达一个抽象的概念和通用的接口,这个类不能实例化(创造)对象. ★纯虚函数(pure virtual):在本类里不能有实现(描述功能),实现需要在子类中实现.例: ...

  3. 4.6 C++抽象基类和纯虚成员函数

    参考:http://www.weixueyuan.net/view/6376.html 总结: 在C++中,可以通过抽象基类来实现公共接口 纯虚成员函数没有函数体,只有函数声明,在纯虚函数声明结尾加上 ...

  4. C++入门经典-例8.9-抽象类,纯虚函数,创建纯虚函数

    1:包含有纯虚函数的类称为抽象类,一个抽象类至少具有一个纯虚函数.抽象类只能作为基类派生出的新的子类,而不能在程序中被实例化(即不能说明抽象类的对象),但是可以使用指向抽象类的指针.在程序开发过程中并 ...

  5. C++基础知识 基类指针、虚函数、多态性、纯虚函数、虚析构

    一.基类指针.派生类指针 父类指针可以new一个子类对象 二.虚函数 有没有一个解决方法,使我们只定义一个对象指针,就可以调用父类,以及各个子类的同名函数? 有解决方案,这个对象指针必须是一个父类类型 ...

  6. OOP2(虚函数/抽象基类/访问控制与继承)

    通常情况下,如果我们不适用某个函数,则无需为该函数提供定义.但我们必须为每个虚函数都提供定义而不管它是否被用到了,这因为连编译器也无法确定到底会适用哪个虚函数 对虚函数的调用可能在运行时才被解析: 当 ...

  7. C++纯虚函数

    本文较为深入的分析了C++中虚函数与纯虚函数的用法,对于学习和掌握面向对象程序设计来说是至关重要的.具体内容如下: 首先,面向对象程序设计(object-oriented programming)的核 ...

  8. c/c++ 基金会(七) 功能覆盖,虚函数,纯虚函数控制

    1.功能覆盖 ClassA , ClassB ,其中ClassB继承ClassA 类的定义如下面的: #ifndef _CLASSA_H #define _CLASSA_H #include < ...

  9. C++ 虚函数 、纯虚函数、接口的实用方法和意义

    也许之前我很少写代码,更很少写面向对象的代码,即使有写多半也很容易写回到面向过程的老路上去.在写面向过程的代码的时候,根本不管什么函数重载和覆盖,想到要什么功能就变得法子的换个函数名字,心里想想:反正 ...

随机推荐

  1. java的通信机制

    通信机制无非就那几种:http访问.socket访问: http又分为:jsp.servlet.html,用的就是get和post方法 socket则可分为:tcp或者udp方式 从以上内容又衍生出其 ...

  2. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

  3. 对require.js 的使用进行总结

    一.为什么要使用require.js 首先一个页面如果在加载多个js文件的时候,浏览器会停止网页渲染,加载文件越多,网页失去响应的时间就会越长:其次,由于js文件之间存在依赖关系,因此必须严格保证加载 ...

  4. 表达式:使用API创建表达式树(1)

    表达式树可使用Expressions类的静态工厂方法来创建.这种用API的方式创建给予我们在编程极大的灵活性,MSDN上关于表达式的例子也不少,但在使用过程中还是会遇到许多麻烦,对有的表达式类,介绍得 ...

  5. (转)PHP函数之error_reporting(E_ALL ^ E_NOTICE)详细说明

    在看帝国cms的connect.php是发现第一句是error_reporting(E_ALL ^ E_NOTICE);以前也没注意过这个语句,知道是设置错误提示的,但不清楚具体怎样设置使用.下面从网 ...

  6. [转帖]AVS音视频编解码技术了解

    AVS高清立体视频编码器 电视技术在经历了从黑白到彩色.从模拟到数字的技术变革之后正在酝酿另一场技术革命,从单纯观看二维场景的平面电视跨越到展现三维场景的立体电视3DTV.3DTV系统的核心问题之一是 ...

  7. MySQL 管理

    MySQL 管理 启动及关闭 MySQL 服务器 首先,我们需要通过以下命令来检查MySQL服务器是否启动: ps -ef | grep mysqld 如果MySql已经启动,以上命令将输出mysql ...

  8. javascript 常用array类型方法

    concat:基于当前数组中的所有项创建一个新数据,会创建当前数组一个副本,然后将接受到的参数放到数组末尾,最后返回新数组.如果没有参数,则复制当前数组并返回副本. slice:基于当前数组中一个或多 ...

  9. window.clearInterval与window.setInterval的用法 定时器的设置

    window.setInterval() 功能:按照指定的周期(以毫秒计)来调用函数或计算表达式. 语法:setInterval(code,millisec) code:在指定时间到时要执行的Java ...

  10. mongoDB之用户及权限设置

    之前用MongoDB没有设置用户和权限,一直都是本机应用程序连接MongoDB.在服务器上部署后对外没有开数据库连接端口,本机应用程序连接再开放应用程序端口供外部访问. 我部署的环境是ubuntu 1 ...