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

本文通过一个银行账户类的例子展示了C++中多态和继承的概念。`Brass`类作为基本账户,`BrassPlus`类是它的增强版,允许透支。` Withdraw`方法在基类和派生类中有不同的实现,体现了多态性。程序展示了如何创建和操作这两个类的对象,包括存款、取款和查看账户信息。在`BrassPlus`类中,`Withdraw`方法根据账户余额和透支限额动态调整行为,这进一步说明了虚函数在实现多态中的作用。

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

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

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

2.使用虚方法。

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

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

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

brass.h

//bank account classes 
#pragma once
#include <string>
//Brass Account Class 
class Brass
{
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() {};

private:
	std::string fullName;
	long acctNum;
	double balance;

};
//Brass Plus Account class
class BrassPlus :public Brass
{
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; };
	//BrassPlus:public Brass();
	//~BrassPlus:public Brass();

private:
	double maxLoan;
	double rate;
	double owesBank;
};

#pragma once

brass.cpp

//brass.cpp --bank account class methods 
#include <iostream>
#include "brass.h"
using std::cout;
using std::endl;
using std::string;
//formatting staff
typedef std::ios_base::fmtflags format;
typedef std::streamsize precis;
format setFormat();
void restore(format f, precis p);
//Brass 方法
Brass::Brass(const string& s, long an, double bal)
{
	fullName = s;
	acctNum = an;
	balance = bal;
}
void Brass::Deposit(double amt)//存款方法
{
	cout << "---Begin of Brass::Deposit---\n";
	if (amt < 0)
		cout << "Negative deposit not allowed; " << "deposit is cancelled.\n";
	else
	{
		balance += amt;
	}

}
void Brass::Withdraw(double amt)
{
	cout << "---Begin of Brass::Withdraw---\n";
	//set up ###.## format 
	format initialState = setFormat();
	precis prec = cout.precision(2);
	if (amt < 0)

		cout << "Withdrawal amount must be positive; " << "Withdrawal cancelled.\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
{
	cout << "---Begin of Brass::Balance---\n";
	return balance;
}
void Brass::ViewAcct()const
{
	cout << "---Begin of Brass::ViewAcct---\n";
	//set up ###.## format 
	format initialState = setFormat();
	precis prec = cout.precision(2);
	cout << "Client: " << fullName << endl;
	cout << "Account Number: " << acctNum << endl;
	cout << "Balance: $" << balance << endl;
}
//BrassPlus Methods
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)//uses implicit copy constructor
{
	maxLoan = ml;
	owesBank = 0.0;
	rate = r;
}
//redefine how ViewAcct works 
void BrassPlus::ViewAcct()const
{
	cout << "---Begin of BrassPlus::ViewAcct---\n";
	//set up ###.## format 
	format initialState = setFormat();
	precis prec = cout.precision(2);
	Brass::ViewAcct();//display base portion 
	cout << "Maximum loan: $" << maxLoan << endl;
	cout << "Own to bank:  $" << owesBank << endl;
	cout.precision(3);
	cout << "Loan Rate: " << 100 * rate << "%\n" << endl;
	restore(initialState, prec);
}
//redefine Withdraw works
void BrassPlus::Withdraw(double amt) //取钱
{
	cout << "---Begin of BrassPlus::Withdraw---\n";
	//set up ###.## format 
	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 exceed.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

// Chapter13.2usebrass1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "brass.h"

int main()
{
   // std::cout << "Hello World!\n";
	using std::cout;
	using std::endl;
	Brass Piggy("Porceelot 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;
}


运行结果如下:

---Begin of Brass::ViewAcct---
Client: Porceelot Pigg
Account Number: 381299
Balance: $4000.00

---Begin of BrassPlus::ViewAcct---
---Begin of Brass::ViewAcct---
Client: Horatio Hogg
Account Number: 382288
Balance: $3000.00
Maximum loan: $500.00
Own to bank:  $0.00
Loan Rate: 11.125%


Depositing $1000 into the Hogg Account:
---Begin of Brass::Deposit---
---Begin of Brass::Balance---
New balance : $4000.00
Withdrawing $4200 from the Pigg Account:
---Begin of Brass::Withdraw---
Withdrawal amount of $ 4200.00exceeds your balance .
Withdrawal canceled.
---Begin of Brass::Balance---
Pigg account balance : $4000.00
********Withdrawing $4200 from the Hoggy Account********:
---Begin of BrassPlus::Withdraw---
---Begin of Brass::Balance---
Bank advance: $200.00
Finance charge: $22.25
---Begin of Brass::Deposit---
---Begin of Brass::Withdraw---
---Begin of BrassPlus::ViewAcct---
---Begin of Brass::ViewAcct---
Client: Horatio Hogg
Account Number: 382288
Balance: $0.00
Maximum loan: $500.00
Own to bank:  $222.25
Loan Rate: 11.125%


D:\eCode\CPPPrimerPlusCode\Chapter13ClassInheritance\13.3Polymorphic_Public_Inheritence\Chapter13.2usebrass1\Debug\Chapter13.3usebrass1.exe (进程 5188)已退出,代码为 0。
按任意键关闭此窗口. . .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值