C++ Prime Plus 学习笔记030

书籍:C++ Primer Plus (第六版)(中文版)

工具:Dev-C++ 5.11

电脑信息:Intel® Xeon® CPU E5-2603 v3 @ 1.60GHz

系统类型:64位操作系统,基于X64的处理器 windows10 专业版

第13章 类继承

13.2 继承:is-a关系

13.3 多态公有继承

实例13.3

brass.h

cpp 复制代码
#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(){}
			
};

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.cpp

cpp 复制代码
#include <iostream>
#include "brass.h"
using std::cout;
using std::endl;
using std::string;

typedef std::ios_base::fmtflags format;
typedef std::streamsize precis;
format setFormat();
void restore(format f,precis p);

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 += amt;
}

void Brass::withdraw(double amt)
{
	format initialState=setFormat();
	precis prec=cout.precision(2);
	if(amt<0)
		cout<<"withdrawal amount must be positive; "
		<<"withdrawal canceled.\n";
	else if(amt<=balance)
		balance -= amt;
	else
		cout<<"withdrawal amount of $"<<amt
		<<" exceeds your balance.\n"
		<<"withdrawal canceled.\n";
	restore(initialState,prec);
}

double Brass::Balance() const
{
	return balance;
}

void Brass::ViewAcct() const
{
	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(const string &s,long an,double bal,
		double m1,double r):Brass(s,an,bal)
		{
			maxLoan=m1;
			owesBank=0.0;
			rate=r;
		}
		
BrassPlus::BrassPlus(const Brass &ba,double m1,double r):Brass(ba)
{
			maxLoan=m1;
			owesBank=0.0;
			rate=r;
}
		
void BrassPlus::ViewAcct()	const
{
	format initialState=setFormat();
	precis prec=cout.precision(2);
	Brass::ViewAcct();
	cout<<"maximum loan:$"<<maxLoan<<endl;
	cout<<"Owed to bank:$"<<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);
	
	double bal=Balance();
	if(amt<bal)
		Brass::withdraw(amt);
	else if(amt<=bal+maxLoan-owesBank)
	{
		double advance=amt-bal;
		owesBank +=advance*(1.0+rate);
		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()
{
	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);
}

usebrass1.cpp

cpp 复制代码
#include <iostream>
#include "brass.h"

int main()
{
	using std::cout;
	using std::endl;
	
	Brass piggy("Porcelot pigg",381299,4000.00);
	BrassPlus Hoggy("Horatio Hogg",382288,3000.00);
	piggy.ViewAcct();
	cout<<endl;
	Hoggy.ViewAcct();
	cout<<endl;
	cout<<"Depositing $1000 into the Hogg Account:\n";
	Hoggy.Deposit(1000.00);
	cout<<"New balance: $"<<Hoggy.Balance()<<endl;
	cout<<"withdrawing $4200 from the pigg Account:\n";
	piggy.withdraw(4200.00);
	cout<<"pigg account balance: $"<<piggy.Balance()<<endl;
	cout<<"withdrawing $4200 from the Hoggy Account:\n";
	Hoggy.withdraw(4200.00);
	Hoggy.ViewAcct();
	
	return 0;	
}

编译运行结果:

cpp 复制代码
Client: Porcelot pigg
Account Number: 381299
Balance:$4000.00

Client: Horatio Hogg
Account Number: 382288
Balance:$3000.00
maximum loan:$500.00
Owed to bank:$0.00
Loan rate: 11.125%

Depositing $1000 into the Hogg Account:
New balance: $4000
withdrawing $4200 from the pigg Account:
withdrawal amount of $4200.00 exceeds your balance.
withdrawal canceled.
pigg account balance: $4000
withdrawing $4200 from the Hoggy Account:
Bank advance:$200.00
Finance charge:$22.25
Client: Horatio Hogg
Account Number: 382288
Balance:$0.00
maximum loan:$500.00
Owed to bank:$222.25
Loan rate: 11.125%

--------------------------------
Process exited after 0.2029 seconds with return value 0
请按任意键继续. . .

使用虚方法特性

usebrass2.cpp

cpp 复制代码
#include <iostream>
#include <string>
#include "brass.h"
const int CLIENTS = 4;

int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	
	Brass *p_clients[CLIENTS];
	std::string temp;
	long tempnum;
	double tempbal;
	char kind;
	
	for(int i=0;i<CLIENTS;i++)
	{
		cout<<"Enter client's name: ";
		getline(cin,temp);
		cout<<"Enter client's account number: ";
		cin>>tempnum;
		cout<<"Enter opening balance: $";
		cin>>tempbal;
		cout<<"Enter 1 for Brass Accouont or "
			<<"2 for Brassplus Account: ";
		while(cin>>kind && (kind != '1' && kind !='2')) 
			cout<<"Enter either 1 or 2: ";
		if(kind =='1')
			p_clients[i] = new Brass(temp,tempnum,tempbal);
		else
		{
			double tmax,trate;
			cout<<" Enter the overdraft limit: $";
			cin>>tmax;
			cout<<"Enter the interest rate "
			<<"as a decimal fraction: ";
			cin>>trate;
			p_clients[i]=new BrassPlus(temp,tempnum,tempbal,tmax,trate);			 
		}
		while(cin.get()!='\n')
			continue;
	}
	cout<<endl;
	for(int i=0;i<CLIENTS;i++)
	{
		p_clients[i]->ViewAcct();
		cout<<endl;
	}
	
	for(int i=0;i<CLIENTS;i++)
	{
		delete p_clients[i];
	}
	cout<<"Done.\n";
	
	return 0;	
}

编译运行结果:

cpp 复制代码
Enter client's name: Harry Fishsong
Enter client's account number: 112233
Enter opening balance: $1500
Enter 1 for Brass Accouont or 2 for Brassplus Account: 1
Enter client's name: Dinah Otternoe
Enter client's account number: 121213
Enter opening balance: $1800
Enter 1 for Brass Accouont or 2 for Brassplus Account: 2
 Enter the overdraft limit: $350
Enter the interest rate as a decimal fraction: 0.12
Enter client's name: Brenda Birdherd
Enter client's account number: 212118
Enter opening balance: $5200
Enter 1 for Brass Accouont or 2 for Brassplus Account: 2
 Enter the overdraft limit: $800
Enter the interest rate as a decimal fraction: 0.10
Enter client's name: Tim Turtletop
Enter client's account number: 233255
Enter opening balance: $688
Enter 1 for Brass Accouont or 2 for Brassplus Account: 1

Client: Harry Fishsong
Account Number: 112233
Balance:$1500.00

Client: Dinah Otternoe
Account Number: 121213
Balance:$1800.00
maximum loan:$350.00
Owed to bank:$0.00
Loan rate: 12.000%

Client: Brenda Birdherd
Account Number: 212118
Balance:$5200.00
maximum loan:$800.00
Owed to bank:$0.00
Loan rate: 10.000%

Client: Tim Turtletop
Account Number: 233255
Balance:$688.00

Done.

--------------------------------
Process exited after 112.9 seconds with return value 0
请按任意键继续. . .
相关推荐
liu****5 小时前
20.预处理详解
c语言·开发语言·数据结构·c++·算法
代码游侠5 小时前
数据结构——哈希表
数据结构·笔记·学习·算法·哈希算法·散列表
ULTRA??5 小时前
moonbit关于模式匹配中的变量绑定
开发语言·c++·人工智能
闲聊MoonL6 小时前
Microsoft Azure Cobalt 200 Launched with 132 Arm Neoverse V3 Cores
笔记
van久6 小时前
.Net Core 学习:DbContextOptions<T> vs DbContextOptions 详细解析
java·学习·.netcore
小龙报6 小时前
【算法通关指南:数据结构与算法篇】树形结构遍历指南:DFS 递归深搜与 BFS 队列广搜实战解析
c语言·数据结构·c++·算法·链表·深度优先·visual studio
HalvmånEver6 小时前
Linux:进程替换(进程控制四)
linux·运维·服务器·学习·进程
zmzb01036 小时前
C++课后习题训练记录Day44
开发语言·c++
qq_433554546 小时前
C++ 二维线性DP
c++·算法·图论