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

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. 使用sqlalchemy用orm方式写pipeline将scrapy item快速存入 MySQL

    传统的使用scrapy爬下来的数据存入mysql,用的是在pipeline里用pymysql存入数据库, 这种方法需要写sql语句,如果item字段数量非常多的 情况下,编写起来会造成很大的麻烦. 我 ...

  2. django创建ORM模型、通过ORM模型操作单个表、ORM模型常用字段

    一.ORM简介 ORM ,全称Object Relational Mapping,中文叫做对象关系映射,通过ORM我们可以通过类的方式去操作数据库,而不用再写原生的SQL语句.通过把表映射成类,把行作 ...

  3. Python3 格式化字符串

    Python3 格式化字符串 在Python 3.6之前,有两种将Python表达式嵌入到字符串文本中进行格式化的主要方法:%-formatting和str.format() 一.%-formatti ...

  4. 比beanutil更加灵活的dto转换工具dozer

    准确的说,是因为pojo无法一招走天下或者说内外部隔离的原因,所以有些时候,不得不在两个bean之间做copy或者转换映射.对于直接性的属性拷贝beanutil以及能够满足大部分要求,但是如果遇到字段 ...

  5. libvirt的security

    1. libvirt支持SASL authentication and encryption MD5 hashes are considered unsafe and should not be us ...

  6. 简单的MVC与SQL Server Express LocalDB

    M模式: 类,表示数据的应用程序和使用验证逻辑以强制实施针对这些数据的业务规则. V视图: 应用程序使用动态生成 HTML 响应的模板文件. C控制器: 处理传入的浏览器请求的类中检索模型数据,然后指 ...

  7. topcoder srm 430 div1

    problem1 link 其实就是找到一个数字$t$,使得$x$的二进制为1 的位上$t$也都为1.然后$t$删掉所有那些$x$为1的二进制位就是$k$. problem2 link 设所有合法的边 ...

  8. noip模拟【tea】

    tea [题目描述]有n个容量为V的瓶子,第i个瓶子中装着a[i]个单位的tea,使所有瓶子内的tea在不 超过其容量的前提下,非空的瓶子最少.在一个单位时间内,可以同时将多个瓶子中的tea倒入另外多 ...

  9. <OFFER15> 15_NumberOf1InBinary

    // 面试题15:二进制中1的个数 // 题目:请实现一个函数,输入一个整数,输出该数二进制表示中1的个数.例如 // 把9表示成二进制是1001,有2位是1.因此如果输入9,该函数输出2. #inc ...

  10. SQLServer2014 安装错误:等待数据库引擎恢复句柄失败

    查了很多资料最后靠百度百科里的一票报道彻底解决困难.在次发表一下以便给后人排忧解难 已下为百度连接 https://jingyan.baidu.com/article/7908e85cb24c19af ...