P493 brass
实现多态共有继承的两种方法
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的更多相关文章
- ruby 基础知识(二)
ruby 中的动态方法 http://singleant.iteye.com/blog/1680382 Rails 大量使用了符号(symbol).符号看上去很像变量名,不过以冒号作为前缀.符号的例 ...
- 漫谈java重载与重写
重载(Overloading):为了让方法名相同而形参不同的构造方法同时存在,让类以统一的方式处理不同类型数据的一种手段 重写(Overriding):导出类对继承自基类的方法做出一定的修改,又称方法 ...
- 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 ...
- Java-继承,多态练习09-22-01
1.实现如下类之间的继承关系,并编写Music类来测试这些类. 父类: package com.lianxi; public class Instrument { //属性 private Strin ...
- 18.实现如下类之间的继承关系,并编写Music类来测试这些类。
package zhongqiuzuoye; public class Instrument { public void play() { System.out.println("弹奏乐器& ...
- 实现如下类之间的继承关系,并编写Music类来测试这些类。
实现如下类之间的继承关系,并编写Music类来测试这些类. package com.hanqi.test; public class Instrument { //输出弹奏乐器 public void ...
- JAVA语言学习笔记(一)
1 一切都是对象 JAVA中所有代码都必须写在类里面. 方法名和参数列表(它们合起来被称为"方法签名")唯一地标识出某个方法.联想多态. 基本数据类型的"局部变量&quo ...
- MapReduce的模式、算法和用例
英文原文:<MapReduce Patterns, Algorithms, and Use Cases> https://highlyscalable.wordpress.com/2012 ...
- GreenPlum简单性能测试与分析--续
版权声明:本文由黄辉原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/259 来源:腾云阁 https://www.qclou ...
随机推荐
- js 实现几分钟前、几小时前、几天前,以及几分钟后、几小时后、几天前后
js 实现几分钟前.几小时前.几天前,以及几分钟后.几小时后.几天前后 /* * * 把传入的时间戳与当前时间比较,计算几分钟前.几小时前.几天前,以及几分钟后.几小时后.几天前后 * unixtim ...
- Zookeeper注册中心的搭建
一.Zookeeper的介绍 Zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件.它是一个为分布式应用 ...
- win7安装mysql-8.0.13-winx64
这里展示一下,由于需要安装一个版本测试一下数据,其实就是超简单的啦. 下包 注:https://dev.mysql.com/downloads/mysql/ 解压与配置 [mysqld] basedi ...
- oracle RAC如何正确地删除ASM磁盘组
1.登录到命令行 切换到grid用户 [grid@swnode1 ~]$ sqlplus / as sysasm SQL*Plus: Release Production on Wed May :: ...
- opencv学习之路(17)、边缘检测
一.概述 二.canny边缘检测 #include "opencv2/opencv.hpp" using namespace cv; void main() { //Canny边缘 ...
- linux 中部署 rsync 实现文件远程备份及 同步
客户端:数据源:服务端:数据接收方 rsync官方文档:https://www.samba.org/ftp/rsync/rsync.html 手动测试用“通过远程外壳访问"里的语法: 参考1 ...
- Install jdk on Ubuntu16
wikiHow to Install Oracle Java JDK on Ubuntu Linux This tutorial will cover the installation of 32-b ...
- Oracle错误——SP2-0734: 未知的命令开头 "imp C##sin..." - 忽略了剩余的行。
错误 在windows的DOS窗口下使用命令导入Oracle数据. 原因 进入sqlplus里是不能执行imp的(sqlplus不认识imp),imp 是个工具,应该在cmd的dos命令提示符下执行.
- 打造性感好用的 VS Code 编辑器
官网: https://code.visualstudio.com/ Blog链接:打造性感好用的VS Code编辑器 主命令框 F1或Ctrl+Shift+P: 打开命令面板.在打开的输入框内,可以 ...
- 为什么不能用drop function add 去删除函数? 因为不能使用 mysql中的保留字!
mysql中有很多的 保留字, 也叫关键字, 你在使用 数据库中的任何东西, 都最好是 避开这些关键字/保留字, 包括 数据库名, 表名, 字段名, 函数名, 存储过程名. 这些关键字包括: mysq ...