共有继承建立起了一种 is -a 的关系,即子类是一个基类,例如:猫是一种动物,狗也是一种动物。对基类对象执行的任何操作,也可以对派生类对象执行。
经常会遇到这样一种情况:希望同一个方法在派生类和基类中的行为是不同的。换句话说:方法的行为取决于调用该方法的对象。猫叫是发出miaomiao的声音,狗叫是wangwang的声音。这是一种多态,同一个方法的行为随上下文而异。有两种机制可以实现多态共有继承:
1.在派生类中重新定义基类的方法。
2.使用虚方法。
brass 的英文为黄铜,也有钱的意思,在这里是表示基本支票账户。brassplus就是加强版的基本账户,这种账户它有一定的透支金额,(类似于信用卡取现),它是brass类的子类,即brassplus共有继承于brass。
本例子是使用对象类型来确定使用哪个版本的函数,基类对象调基类的版本,派生类调用派生类的版本,这个很清楚,也没有利用到虚函数的特性。usebrass2.cpp 将会给出虚函数的具体使用,还有使用虚函数的好处等。
有一些代码是和格式设置有关的,读者可以不必关心这个。
brass.h
- //bank account classes
- #pragma once
- #include
- //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
- #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
- #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。
- 按任意键关闭此窗口. . .