• C++PrimerPlus(第6版)中文版:Chapter13.3多态公有继承例子:usebrass1.cpp


    共有继承建立起了一种 is -a 的关系,即子类是一个基类,例如:猫是一种动物,狗也是一种动物。对基类对象执行的任何操作,也可以对派生类对象执行。

    经常会遇到这样一种情况:希望同一个方法在派生类和基类中的行为是不同的。换句话说:方法的行为取决于调用该方法的对象。猫叫是发出miaomiao的声音,狗叫是wangwang的声音。这是一种多态,同一个方法的行为随上下文而异。有两种机制可以实现多态共有继承:

    1.在派生类中重新定义基类的方法。

    2.使用虚方法。

    brass 的英文为黄铜,也有钱的意思,在这里是表示基本支票账户。brassplus就是加强版的基本账户,这种账户它有一定的透支金额,(类似于信用卡取现),它是brass类的子类,即brassplus共有继承于brass。

    本例子是使用对象类型来确定使用哪个版本的函数,基类对象调基类的版本,派生类调用派生类的版本,这个很清楚,也没有利用到虚函数的特性。usebrass2.cpp 将会给出虚函数的具体使用,还有使用虚函数的好处等。

    有一些代码是和格式设置有关的,读者可以不必关心这个。

    brass.h

    1. //bank account classes
    2. #pragma once
    3. #include
    4. //Brass Account Class
    5. class Brass
    6. {
    7. public:
    8. Brass(const std::string& s = "Nullbody", long an = -1, double bal = 0.0);
    9. void Deposit(double amt);
    10. virtual void Withdraw(double amt);
    11. double Balance() const;
    12. virtual void ViewAcct() const;
    13. virtual ~Brass() {};
    14. private:
    15. std::string fullName;
    16. long acctNum;
    17. double balance;
    18. };
    19. //Brass Plus Account class
    20. class BrassPlus :public Brass
    21. {
    22. public:
    23. BrassPlus(const std::string& s = "Nullbody", long an = -1, double bal = 0.0, double ml = 500, double r = 0.11125);
    24. BrassPlus(const Brass& ba, double ml = 500, double r = 0.11125);
    25. virtual void ViewAcct()const;
    26. virtual void Withdraw(double amt);
    27. void ResetMax(double m) { maxLoan = m; };
    28. void ResetRate(double r) { rate = r; };
    29. void ResetOwes() { owesBank = 0; };
    30. //BrassPlus:public Brass();
    31. //~BrassPlus:public Brass();
    32. private:
    33. double maxLoan;
    34. double rate;
    35. double owesBank;
    36. };
    37. #pragma once

    brass.cpp

    1. //brass.cpp --bank account class methods
    2. #include
    3. #include "brass.h"
    4. using std::cout;
    5. using std::endl;
    6. using std::string;
    7. //formatting staff
    8. typedef std::ios_base::fmtflags format;
    9. typedef std::streamsize precis;
    10. format setFormat();
    11. void restore(format f, precis p);
    12. //Brass 方法
    13. Brass::Brass(const string& s, long an, double bal)
    14. {
    15. fullName = s;
    16. acctNum = an;
    17. balance = bal;
    18. }
    19. void Brass::Deposit(double amt)//存款方法
    20. {
    21. cout << "---Begin of Brass::Deposit---\n";
    22. if (amt < 0)
    23. cout << "Negative deposit not allowed; " << "deposit is cancelled.\n";
    24. else
    25. {
    26. balance += amt;
    27. }
    28. }
    29. void Brass::Withdraw(double amt)
    30. {
    31. cout << "---Begin of Brass::Withdraw---\n";
    32. //set up ###.## format
    33. format initialState = setFormat();
    34. precis prec = cout.precision(2);
    35. if (amt < 0)
    36. cout << "Withdrawal amount must be positive; " << "Withdrawal cancelled.\n";
    37. else if (amt <= balance)
    38. balance -= amt;
    39. else
    40. {
    41. cout << "Withdrawal amount of $ " << amt << "exceeds your balance .\n" << "Withdrawal canceled.\n";
    42. }
    43. restore(initialState, prec);
    44. }
    45. double Brass::Balance()const
    46. {
    47. cout << "---Begin of Brass::Balance---\n";
    48. return balance;
    49. }
    50. void Brass::ViewAcct()const
    51. {
    52. cout << "---Begin of Brass::ViewAcct---\n";
    53. //set up ###.## format
    54. format initialState = setFormat();
    55. precis prec = cout.precision(2);
    56. cout << "Client: " << fullName << endl;
    57. cout << "Account Number: " << acctNum << endl;
    58. cout << "Balance: $" << balance << endl;
    59. }
    60. //BrassPlus Methods
    61. BrassPlus::BrassPlus(const string& s, long an, double bal, double ml, double r) :Brass(s, an, bal)
    62. {
    63. maxLoan = ml;
    64. owesBank = 0.0;
    65. rate = r;
    66. }
    67. BrassPlus::BrassPlus(const Brass& ba, double ml, double r) :Brass(ba)//uses implicit copy constructor
    68. {
    69. maxLoan = ml;
    70. owesBank = 0.0;
    71. rate = r;
    72. }
    73. //redefine how ViewAcct works
    74. void BrassPlus::ViewAcct()const
    75. {
    76. cout << "---Begin of BrassPlus::ViewAcct---\n";
    77. //set up ###.## format
    78. format initialState = setFormat();
    79. precis prec = cout.precision(2);
    80. Brass::ViewAcct();//display base portion
    81. cout << "Maximum loan: $" << maxLoan << endl;
    82. cout << "Own to bank: $" << owesBank << endl;
    83. cout.precision(3);
    84. cout << "Loan Rate: " << 100 * rate << "%\n" << endl;
    85. restore(initialState, prec);
    86. }
    87. //redefine Withdraw works
    88. void BrassPlus::Withdraw(double amt) //取钱
    89. {
    90. cout << "---Begin of BrassPlus::Withdraw---\n";
    91. //set up ###.## format
    92. format initialState = setFormat();
    93. precis prec = cout.precision(2);
    94. double bal = Balance();//结余,
    95. if (amt <= bal)//如果取的钱 小于账户里面的结余
    96. Brass::Withdraw(amt);
    97. else if (amt <= bal + maxLoan - owesBank)//如果取的钱 小于等于 结余+最大贷款额-已经欠银行的钱
    98. {
    99. double advance = amt - bal;
    100. owesBank += advance * (1.0 + rate);
    101. cout << "Bank advance: $" << advance << endl; //预付款
    102. cout << "Finance charge: $" << advance * rate << endl;//财富费用
    103. Deposit(advance);
    104. Brass::Withdraw(amt);
    105. }
    106. else
    107. cout << "Credit limit exceed.Transaction cancelled.\n";
    108. restore(initialState, prec);
    109. }
    110. format setFormat()
    111. {
    112. return cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
    113. }
    114. void restore(format f, precis p)
    115. {
    116. cout.setf(f, std::ios_base::floatfield);
    117. cout.precision(p);
    118. }

    usebrass1.cpp

    1. // Chapter13.2usebrass1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
    2. //
    3. #include
    4. #include "brass.h"
    5. int main()
    6. {
    7. // std::cout << "Hello World!\n";
    8. using std::cout;
    9. using std::endl;
    10. Brass Piggy("Porceelot Pigg",381299,4000.00);
    11. BrassPlus Hoggy("Horatio Hogg",382288,3000.00);
    12. Piggy.ViewAcct();
    13. cout << endl;
    14. Hoggy.ViewAcct();
    15. cout << endl;
    16. cout << "Depositing $1000 into the Hogg Account:\n";
    17. Hoggy.Deposit(1000.00);
    18. cout << "New balance : $" << Hoggy.Balance() << endl;
    19. cout << "Withdrawing $4200 from the Pigg Account:\n";
    20. Piggy.Withdraw(4200.00);
    21. cout << "Pigg account balance : $" << Piggy.Balance() << endl;
    22. cout << "********Withdrawing $4200 from the Hoggy Account********:\n";
    23. Hoggy.Withdraw(4200.00);
    24. Hoggy.ViewAcct();
    25. return 0;
    26. }

    运行结果如下:

    1. ---Begin of Brass::ViewAcct---
    2. Client: Porceelot Pigg
    3. Account Number: 381299
    4. Balance: $4000.00
    5. ---Begin of BrassPlus::ViewAcct---
    6. ---Begin of Brass::ViewAcct---
    7. Client: Horatio Hogg
    8. Account Number: 382288
    9. Balance: $3000.00
    10. Maximum loan: $500.00
    11. Own to bank: $0.00
    12. Loan Rate: 11.125%
    13. Depositing $1000 into the Hogg Account:
    14. ---Begin of Brass::Deposit---
    15. ---Begin of Brass::Balance---
    16. New balance : $4000.00
    17. Withdrawing $4200 from the Pigg Account:
    18. ---Begin of Brass::Withdraw---
    19. Withdrawal amount of $ 4200.00exceeds your balance .
    20. Withdrawal canceled.
    21. ---Begin of Brass::Balance---
    22. Pigg account balance : $4000.00
    23. ********Withdrawing $4200 from the Hoggy Account********:
    24. ---Begin of BrassPlus::Withdraw---
    25. ---Begin of Brass::Balance---
    26. Bank advance: $200.00
    27. Finance charge: $22.25
    28. ---Begin of Brass::Deposit---
    29. ---Begin of Brass::Withdraw---
    30. ---Begin of BrassPlus::ViewAcct---
    31. ---Begin of Brass::ViewAcct---
    32. Client: Horatio Hogg
    33. Account Number: 382288
    34. Balance: $0.00
    35. Maximum loan: $500.00
    36. Own to bank: $222.25
    37. Loan Rate: 11.125%
    38. D:\eCode\CPPPrimerPlusCode\Chapter13ClassInheritance\13.3Polymorphic_Public_Inheritence\Chapter13.2usebrass1\Debug\Chapter13.3usebrass1.exe (进程 5188)已退出,代码为 0
    39. 按任意键关闭此窗口. . .

  • 相关阅读:
    《安富莱嵌入式周报》第281期:Keil Studio发布VSCode插件,微软嵌入式IDE升级,开源穿戴手表,CAN XL汽车单片机,USB4 V2.0规范
    NVIDIA CUDA Toolkit
    男孩姓卜取什么名字好听
    JavaSE——异常
    利用SpringBoot重写黑马旅游网
    由 mysql 中的一次慢查询,来看 order by 的文件排序和索引排序
    unity中跟随鼠标浮动的面板,并可以自适应文字内容的大小
    关于webpack(v5.74.0)的html-webpack-plugin原理
    CAS机制的的解释和总结
    【Educoder离散数学实训】集合及其基本运算的实现
  • 原文地址:https://blog.csdn.net/superfreak/article/details/126480387