添加基础和虚基类、虚函数
This commit is contained in:
parent
37742dfa76
commit
e735a97c12
102
base/extend/acctabc.cpp
Normal file
102
base/extend/acctabc.cpp
Normal file
@ -0,0 +1,102 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/23.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "acctabc.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
AcctABC::AcctABC(const std::string &s, long an, double bal) {
|
||||
fullName = s;
|
||||
accNum = an;
|
||||
balance = bal;
|
||||
}
|
||||
|
||||
void AcctABC::Deposit(double amt) {
|
||||
if (amt < 0) {
|
||||
cout << "Negative deposit not allowed; " << " deposit is cancelled.\n";
|
||||
} else {
|
||||
balance -= amt;
|
||||
}
|
||||
}
|
||||
|
||||
void AcctABC::Withdraw(double amt) {
|
||||
balance -= amt;
|
||||
}
|
||||
|
||||
AcctABC::Formatting AcctABC::SetFormat() const {
|
||||
Formatting f;
|
||||
f.flag = cout.setf(ios_base::fixed, ios_base::floatfield);
|
||||
f.pr = cout.precision(2);
|
||||
return f;
|
||||
}
|
||||
|
||||
void AcctABC::Resetore(Formatting &f) const {
|
||||
f.flag = cout.setf(f.flag, ios_base::floatfield);
|
||||
cout.precision(f.pr);
|
||||
}
|
||||
|
||||
double Brass::Balance() const {
|
||||
return balance;
|
||||
}
|
||||
|
||||
void Brass::Withdraw(double amt) {
|
||||
if (amt < 0) {
|
||||
cout << "Withdrawal amount must be positive; " << "Withdrawal canceled." << endl;
|
||||
} else if (amt <= Balance()) {
|
||||
AcctABC::Withdraw(amt);
|
||||
} else {
|
||||
cout << "Withdrawal amount of $ " << amt << " exceeds your balance.\n" << " Withdrawal canceled." << endl;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Brass::ViewAcct() const {
|
||||
Formatting f = SetFormat();
|
||||
cout << "Brass Client: " << FullName() << endl;
|
||||
cout << "Account Number: " << AcctNum() << endl;
|
||||
cout << "Balance: $" << Balance() << endl;
|
||||
Restore(f);
|
||||
}
|
||||
|
||||
BrassPlus::BrassPlus(const std::string &s, long an, double bal, double ml, double r) : AcctABC(s,an,bal){
|
||||
maxLoan = ml;
|
||||
owesBank = 0.0;
|
||||
rate = r;
|
||||
}
|
||||
BrassPlus::BrassPlus(const Brass &ba, double ml, double r) : AcctABC(ba) {
|
||||
maxLoan = ml;
|
||||
owesBank = 0.0;
|
||||
rate = r;
|
||||
}
|
||||
void BrassPlus::ViewAcct() const {
|
||||
Formatting f = SetFormat();
|
||||
cout << "BrassPlus Client: " << FullName() << endl;
|
||||
cout << "Account Number: " << AcctNum() << endl;
|
||||
cout << "Balance: $" << Balance() << endl;
|
||||
cout << "Maximum loan: $" << maxLoan << endl;
|
||||
cout << "Owed to bank: $" << owesBank << endl;
|
||||
cout.precision(3);
|
||||
cout << "Loan Rate: " << 100 * rate << "%\n";
|
||||
Restore(f);
|
||||
}
|
||||
|
||||
void BrassPlus::Withdraw(double amt) {
|
||||
Formatting f = SetFormat();
|
||||
|
||||
double bal = Balance();
|
||||
if (amt <= bal) {
|
||||
AcctABC::Withdraw(amt);
|
||||
} else if (amt <= bal + maxLoan - owesBank) {
|
||||
double advance = amt - bal;
|
||||
owesBank += advance * (1.0 + rate);
|
||||
cout << "Bank advance: $" << advance << endl;
|
||||
cout << "Finance change: $" << advance * rate << endl;
|
||||
Deposit(advance);
|
||||
AcctABC::Withdraw(amt);
|
||||
} else {
|
||||
cout << "Credit limit exceeded. Transaction cancelled.\n";
|
||||
}
|
||||
Restore(f);
|
||||
}
|
96
base/extend/acctabc.h
Normal file
96
base/extend/acctabc.h
Normal file
@ -0,0 +1,96 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/23.
|
||||
//
|
||||
|
||||
#ifndef BASE_ACCTABC_H
|
||||
#define BASE_ACCTABC_H
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class AcctABC {
|
||||
private:
|
||||
std::string fullName;
|
||||
long accNum;
|
||||
double balance;
|
||||
protected:
|
||||
struct Formatting {
|
||||
std::ios_base::fmtflags flag;
|
||||
std::streamsize pr;
|
||||
};
|
||||
|
||||
const std::string &FullName() const {
|
||||
return fullName;
|
||||
}
|
||||
|
||||
long AcctNum() const {
|
||||
return accNum;
|
||||
}
|
||||
|
||||
Formatting SetFormat() const;
|
||||
|
||||
void Restore(Formatting &f) const;
|
||||
|
||||
public:
|
||||
AcctABC(const std::string &s = "Nullbody", long an = -1, double bal = 0.0);
|
||||
|
||||
void Deposit(double amt);
|
||||
|
||||
//纯虚函数
|
||||
virtual void Withdraw(double amt) = 0;
|
||||
|
||||
double Balance() const {
|
||||
return balance;
|
||||
}
|
||||
|
||||
virtual void ViewAcct() const = 0;
|
||||
|
||||
virtual ~AcctABC() {}
|
||||
};
|
||||
|
||||
class Brass : public AcctABC {
|
||||
public:
|
||||
Brass(const std::string &s = "Nullbody", long an = -1, double bal = 0.0) : AcctABC(s, an, bal) {};
|
||||
|
||||
double Balance() const;
|
||||
//虚函数
|
||||
virtual void Withdraw(double amt);
|
||||
|
||||
//虚函数
|
||||
virtual void ViewAcct() const;
|
||||
|
||||
virtual ~Brass() {};
|
||||
};
|
||||
|
||||
class BrassPlus:public AcctABC{
|
||||
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.10);
|
||||
|
||||
BrassPlus(const Brass &ba, double ml = 500, double r = 0.10);
|
||||
|
||||
//虚函数
|
||||
virtual void ViewAcct() const;
|
||||
|
||||
//虚函数
|
||||
virtual void Withdraw(double amt);
|
||||
|
||||
//析构函数也是虚函数
|
||||
virtual ~BrassPlus() {};
|
||||
|
||||
void ResetMax(double m) {
|
||||
maxLoan = m;
|
||||
}
|
||||
|
||||
void ResetRate(double r) {
|
||||
rate = r;
|
||||
}
|
||||
|
||||
void ResetOwes() {
|
||||
owesBank = 0;
|
||||
}
|
||||
};
|
||||
#endif //BASE_ACCTABC_H
|
99
base/extend/brass.cpp
Normal file
99
base/extend/brass.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/23.
|
||||
//
|
||||
|
||||
#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 std::string &s, long an, double bal) : fullName(s), accNum(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." << endl;
|
||||
} else if (amt <= balance) {
|
||||
balance -= amt;
|
||||
} else {
|
||||
cout << "Withdrawal amount of $ " << amt << " exceeds your balance.\n" << " Withdrawal canceled." << endl;
|
||||
}
|
||||
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: " << accNum << endl;
|
||||
cout << "Balance: $" << balance << endl;
|
||||
restore(initialState, prec);
|
||||
}
|
||||
|
||||
BrassPlus::BrassPlus(const std::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 {
|
||||
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 * 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);
|
||||
}
|
69
base/extend/brass.h
Normal file
69
base/extend/brass.h
Normal file
@ -0,0 +1,69 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/23.
|
||||
//
|
||||
|
||||
#ifndef BASE_BRASS_H
|
||||
#define BASE_BRASS_H
|
||||
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
* 虚函数会根据对象类型来判断执行那个对象的虚方法
|
||||
*
|
||||
* 有元不能是虚函数(友元不是成员函数)
|
||||
*/
|
||||
class Brass {
|
||||
private:
|
||||
std::string fullName;
|
||||
long accNum;
|
||||
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);
|
||||
|
||||
//析构函数也是虚函数
|
||||
virtual ~BrassPlus() {};
|
||||
|
||||
void ResetMax(double m) {
|
||||
maxLoan = m;
|
||||
}
|
||||
|
||||
void ResetRate(double r) {
|
||||
rate = r;
|
||||
}
|
||||
|
||||
void ResetOwes() {
|
||||
owesBank = 0;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif //BASE_BRASS_H
|
19
base/extend/tabtenn.cpp
Normal file
19
base/extend/tabtenn.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/23.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "tabtenn.h"
|
||||
|
||||
TableTennisPlayer::TableTennisPlayer(const string &fn, const string &ln, bool ht) : firstname(fn), lastname(ln),
|
||||
hasTable(ht) {}
|
||||
|
||||
void TableTennisPlayer::Name() const {
|
||||
std::cout << lastname << "," << firstname;
|
||||
}
|
||||
|
||||
RatedPlayer::RatedPlayer(unsigned int r, const string &fn, const string &ln, bool ht) : TableTennisPlayer(fn, ln, ht) {
|
||||
rating = r;
|
||||
}
|
||||
|
||||
RatedPlayer::RatedPlayer(unsigned int r, const TableTennisPlayer &tp) : TableTennisPlayer(tp), rating(r) {}
|
48
base/extend/tabtenn.h
Normal file
48
base/extend/tabtenn.h
Normal file
@ -0,0 +1,48 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/23.
|
||||
//
|
||||
|
||||
#ifndef BASE_TABTENN_H
|
||||
#define BASE_TABTENN_H
|
||||
|
||||
#include <string>
|
||||
|
||||
using std::string;
|
||||
|
||||
class TableTennisPlayer {
|
||||
private:
|
||||
string firstname;
|
||||
string lastname;
|
||||
bool hasTable;
|
||||
public:
|
||||
TableTennisPlayer(const string &fn = "none", const string &ln = "none", bool ht = false);
|
||||
|
||||
void Name() const;
|
||||
|
||||
bool HashTable() const {
|
||||
return hasTable;
|
||||
}
|
||||
|
||||
void RestTable(bool v) {
|
||||
hasTable = v;
|
||||
}
|
||||
};
|
||||
|
||||
class RatedPlayer : public TableTennisPlayer {
|
||||
private:
|
||||
unsigned int rating;
|
||||
public:
|
||||
RatedPlayer(unsigned int r = 0,const string &fn = "none", const string &ln = "none", bool ht = false);
|
||||
|
||||
RatedPlayer(unsigned int r, const TableTennisPlayer &tp);
|
||||
|
||||
unsigned int Rating() const {
|
||||
return rating;
|
||||
}
|
||||
|
||||
void ResetRating(unsigned int r) {
|
||||
rating = r;
|
||||
}
|
||||
};
|
||||
|
||||
#endif //BASE_TABTENN_H
|
28
base/extend/usebrass.cpp
Normal file
28
base/extend/usebrass.cpp
Normal file
@ -0,0 +1,28 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/23.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "brass.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
Brass Piggy("Procelot 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 Acount:\n";
|
||||
Piggy.Withdraw(4200.00);
|
||||
cout << "Pigg account balance: $" << Piggy.Balance() << endl;
|
||||
cout << "Withdrawing $4200 from the Hogg Acount:\n";
|
||||
Hoggy.Withdraw(4200.00);
|
||||
Hoggy.ViewAcct();
|
||||
return 0;
|
||||
}
|
||||
|
59
base/extend/usebrass2.cpp
Normal file
59
base/extend/usebrass2.cpp
Normal file
@ -0,0 +1,59 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/23.
|
||||
//
|
||||
/**
|
||||
* 使用虚函数的特性
|
||||
* 根据类的种类来觉得用虚函数
|
||||
*
|
||||
*/
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include "brass.h"
|
||||
|
||||
using namespace std;
|
||||
const int CLIENTS = 4;
|
||||
|
||||
int main() {
|
||||
|
||||
Brass *p_clients[CLIENTS];
|
||||
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 Acount or 2 for BrassPlus Acount: ";
|
||||
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 interset 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;
|
||||
}
|
||||
|
37
base/extend/usett.cpp
Normal file
37
base/extend/usett.cpp
Normal file
@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by zhuyijun on 2021/8/23.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#include "tabtenn.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
TableTennisPlayer player1("Tara", "Boomdea", false);
|
||||
RatedPlayer rPlayer1(1140, "Mallory", "Duck", true);
|
||||
rPlayer1.Name();
|
||||
if (rPlayer1.HashTable())
|
||||
cout << ": hash a table." << endl;
|
||||
else
|
||||
cout << ": hasn't a table." << endl;
|
||||
player1.Name();
|
||||
if (player1.HashTable())
|
||||
cout << ": hash a table." << endl;
|
||||
else
|
||||
cout << ": hasn't a table." << endl;
|
||||
cout << "Name: ";
|
||||
rPlayer1.Name();
|
||||
cout << "; Rating: " << rPlayer1.Rating() << endl;
|
||||
|
||||
RatedPlayer rplayer2(1212, player1);
|
||||
cout << "Name: ";
|
||||
rplayer2.Name();
|
||||
cout << "; Rating: " << rplayer2.Rating() << endl;
|
||||
|
||||
//可以将子类对象地址赋值给父类引用(其使用隐式重载运算符等于)
|
||||
TableTennisPlayer *rp = &rplayer2;
|
||||
rp->Name();
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user