实现多态共有继承的两种方法

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

2 使用虚方法

P493程序清单13.7使用的方法为在派生类中重新定义基类的方法

brass.h

#ifndef BRASS_H
#define BRASS_H #include <string> class Brass{
private:
std::string fullName; //客户姓名
long acctNum; //账号
double balance; //当前结余
public:
Brass(const std::string & s="Nullbody",long an=-1,
double bal=0.0);
void Deposit(double amt); //存款
virtual void withdraw(double amt); //取款
double Balance() const; //显示余额
virtual void ViewAcct() const;
virtual ~Brass() {}
}; //Brass Plus Account Class
class BrassPlus:public Brass
{
private:
double maxLoan; //透支的上线
double rate; //透支贷款利率
double owesBank; //当前的透支额度
public:
BrassPlus(const std::string &s="Nullbody",long an=-1,
double bal=0.0,double ml=500,double r=0.11125);
BrassPlus(const Brass &ba,double ml=500,
double r=0.11125);
virtual void ViewAcct() const;
virtual void withdraw(double amt);
void ResetMax(double m){maxLoan=m;};
void ResetRate(double r){rate=r;};
void ResetOwes() {owesBank=0;};
};
#endif // BRASS_H

main.cpp

#include <iostream>
#include "brass.h" using namespace std; //formatting stuff
typedef std::ios_base::fmtflags format;
typedef std::streamsize precis; format setFormat();
void restore(format f,precis p); //Brass methods
Brass::Brass(const string &s, long an, double bal)
{
fullName=s;
acctNum=an;
balance=bal;
}
//储蓄函数
void Brass::Deposit(double amt)
{
if(amt<0)
{
cout<<"Negative deposit not allowed;"
<<"deposit is cancelled\n";
}
else
balance=balance+amt;
}
void Brass::withdraw(double amt)
{
//set up ###.##format
format initialState=setFormat();
precis prec=cout.precision(2);
if(amt<0)
{
cout<<"withdrawal amouut must be positive;"
<<"withdrawal canceled\n";
}
else if(amt<=balance)
{
balance=balance-amt;
}
else
{
cout<<"withrdrawl amout of $"<<amt
<<" exceeds your balance\n"
<<"withdrawal canceled\n";
}
restore(initialState,prec); }
double Brass::Balance() const
{
return balance;
}
void Brass::ViewAcct() const
{
//set up ###.## format
format initialState=setFormat();
precis prec=cout.precision(2);
cout<<"Client:"<<fullName<<endl;
cout<<"Account Number:"<<acctNum<<endl;
cout<<"Balance:$"<<balance<<endl;
restore(initialState,prec); //存储原始的格式
}
//BrassPlus的方法
BrassPlus::BrassPlus(const string &s,
long an, double bal,
double ml, double r):Brass(s,an,bal)
{
//构造函数
maxLoan=ml;
owesBank=0.0;
rate=r;
}
BrassPlus::BrassPlus(const Brass &ba, double ml, double r):Brass(ba)
{
maxLoan=ml;
owesBank=0.0;
rate=r;
}
//重定义
void BrassPlus::ViewAcct() const
{
//set up ###.##format
format initialState=setFormat();
precis prec=cout.precision(2);
Brass::ViewAcct(); //显示基类的部分
cout<<"Maximum loan:$"<<maxLoan<<endl;
cout<<"Owed to back:$"<<owesBank<<endl;
cout.precision(3);
cout<<"Loan Rate"<<100*rate<<"%\n";
restore(initialState,prec);
}
//重定义
//如果用户提取的金额超过了结余,该方法将安排贷款
void BrassPlus::withdraw(double amt)
{
format initialState=setFormat();
precis prec=cout.precision(2);
//使用Balance()函数来确定结余
double bal=Balance();
if(amt<=bal)
{
//提款少于存款,提款成功
Brass::withdraw(amt); //继承类中调用基类的方法
}
else if(amt<=bal+maxLoan-owesBank)
{
double advance=amt-bal;
owesBank+=advance*(1.0+rate); //owesBank当前的透支额度
cout<<"Bank advance:$"<<advance<<endl;
cout<<"Finance charge:$"<<advance*rate<<endl;
Deposit(advance); //放贷
//放贷成功后可以今天提款
Brass::withdraw(amt);
}
else
{
cout<<"Credit limit exceeded.Transaction cancelled.\n";
}
restore(initialState,prec);
}
format setFormat()
{
//set up ###.## format
return cout.setf(std::ios_base::fixed,
std::ios_base::floatfield);
}
void restore(format f, precis p)
{
cout.setf(f,std::ios_base::floatfield);
cout.precision(p);
}
int main(int argc, char *argv[])
{
cout << "Hello World!" << endl;
using std::cout;
using std::endl;
Brass Piggy("Procelot Pigg",381299,4000.00);
BrassPlus Hoggy("Horatio Hogg",382288,3000.00);
Piggy.ViewAcct();
cout<<endl;
Hoggy.ViewAcct();
cout<<"Depositing $1000 into the Hogg Account:\n";
Hoggy.Deposit(1000.00); //储蓄1000元到Hoggy的账户
cout<<"New Balance:$"<<Hoggy.Balance()<<endl;
cout<<"Withdrawing $4200 from the Pigg Account:\n";
//取款失败,且不能借贷,因为Piggy是基类的对象,没有实现借贷的方法
Piggy.withdraw(4200.00);
cout<<"Pigg account balance:$"<<Piggy.Balance()<<endl;
cout<<"Withdrwaing $42000 from the Hogg Account:\n";
Hoggy.withdraw(4200.00);
Hoggy.ViewAcct();
return 0;
}

运行结果如下



IDE为Qt Creator 4.0.3 (Community)

P493 brass的更多相关文章

  1. ruby 基础知识(二)

    ruby  中的动态方法 http://singleant.iteye.com/blog/1680382 Rails 大量使用了符号(symbol).符号看上去很像变量名,不过以冒号作为前缀.符号的例 ...

  2. 漫谈java重载与重写

    重载(Overloading):为了让方法名相同而形参不同的构造方法同时存在,让类以统一的方式处理不同类型数据的一种手段 重写(Overriding):导出类对继承自基类的方法做出一定的修改,又称方法 ...

  3. The Nine Indispensable Rules for HW/SW Debugging 软硬件调试之9条军规

    I read this book in the weekend, and decided to put the book on my nightstand. It's a short and funn ...

  4. Java-继承,多态练习09-22-01

    1.实现如下类之间的继承关系,并编写Music类来测试这些类. 父类: package com.lianxi; public class Instrument { //属性 private Strin ...

  5. 18.实现如下类之间的继承关系,并编写Music类来测试这些类。

    package zhongqiuzuoye; public class Instrument { public void play() { System.out.println("弹奏乐器& ...

  6. 实现如下类之间的继承关系,并编写Music类来测试这些类。

    实现如下类之间的继承关系,并编写Music类来测试这些类. package com.hanqi.test; public class Instrument { //输出弹奏乐器 public void ...

  7. JAVA语言学习笔记(一)

    1 一切都是对象 JAVA中所有代码都必须写在类里面. 方法名和参数列表(它们合起来被称为"方法签名")唯一地标识出某个方法.联想多态. 基本数据类型的"局部变量&quo ...

  8. MapReduce的模式、算法和用例

    英文原文:<MapReduce Patterns, Algorithms, and Use Cases> https://highlyscalable.wordpress.com/2012 ...

  9. GreenPlum简单性能测试与分析--续

    版权声明:本文由黄辉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/259 来源:腾云阁 https://www.qclou ...

随机推荐

  1. php 使用table方式导出excel文件

    这些天在使用PHPExcel导出数据时,5000条数据竟然挂了.后来跟同事聊聊,有些明悟,PHPExcel做了很多处理,我在这里理解为渲染,就会暂用过多的空间,‘膨胀’的空间导致内存暂用过大,就挂了. ...

  2. mysql库、表、索引

    创建和删除数据库,同一个数据库下的不同表可以采用不同的引擎 mysql> create database oldboy default character set utf8 collate ut ...

  3. Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源

    Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源 在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等.今天就给大家介绍一个 Spri ...

  4. NATS—发布/订阅机制

    概念 发布/订阅(Publish/subscribe 或pub/sub)是一种消息范式,消息的发送者(发布者)不是计划发送其消息给特定的接收者(订阅者).而是发布的消息分为不同的类别,而不需要知道什么 ...

  5. mysql服务器iowait高优化一例完整深入解析

    我们有一服务器,上面运行着两个mysql实例,这几天iowait一直很高,在20-30%,下午特地专门排查和解决了下,相关过程整理如下. 该服务器有两个挂载盘,服务器在阿里云上,一个系统盘,一个数据盘 ...

  6. How To Answer The Question "tell me about yourself" In An Interview

    Two or three minutes. ponit list: education experience highlight accomplishments show passion/ drive ...

  7. openvas漏洞扫描

    openvas配置步骤 1.-因为老师给的kali中自带的openvas,所以我们可以直接执行命令:openvas-check-setup来查看下他的安装状态: 如下图所示:在步骤7中出现错误,其中图 ...

  8. 使用JBarcode生成一维码

    需要的jar包,只有jbarcode.jar 链接: https://pan.baidu.com/s/1o9oDPB8 密码: x367 public class Main { //设置条形码高度 p ...

  9. topcoder srm 694 div1 -3

    1.给出$n$个数字,将其分成三个非空的组,每组的权值为该组所有数字的抑或.选择一种分法使得三组的权值和最大? 思路:记录前两组的权值且三组有没有数字时第三组的值.(当前两组的值知道时第三组的权值是确 ...

  10. SVM学习笔记4-核函数和离群点的处理

    核函数在svm里,核函数是这样定义的.核函数是一个n*n(样本个数)的矩阵,其中:$K_{ij}=exp(-\frac{||x^{(i)}-x^{(j)}||^{2}}{2\sigma ^{2}})$ ...