目录
Account.h
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
public:
enum AccountType { account, savingsAccount, checkingAccount };
Account(double = 0.0, AccountType =account);
virtual void credit(double);
virtual bool debit(double);
void setBalance(double);
double getBalance();
private:
double balance;
AccountType accountType;
};
#endif
Account.cpp
#include "Account.h"
#include<iostream>
using namespace std;
Account::Account(double x, AccountType a)
{
accountType = a;
if (x < 0)
{
cout << "Error: Initial balance" << endl;
exit(0);
}
balance = x;
}
void Account::credit(double x)
{
balance += x;
}
bool Account::debit(double x)
{
if (x > balance)
{
cout << "Debit amount exceeded account balance." << endl;
return false;
}
else
{
balance = balance - x;
return true;
}
}
void Account::setBalance(double x)
{
balance = x;
}
double Account::getBalance()
{
return balance;
}
-
exit(0):表示正常运行程序并退出程序
SavingsAccount.h
#ifndef SAVINGS_H
#define SAVINGS_H
#include "Account.h"
class SavingsAccount : public Account
{
public:
SavingsAccount(double, double = 0.0, AccountType = savingsAccount);
double calculateInterest();
private:
double interestRate;
};
#endif
SavingsAccount.cpp
#include "SavingsAccount.h"
#include<iostream>
using namespace std;
SavingsAccount::SavingsAccount(double x, double y, AccountType a):Account(x,a)
{
if (y < 0)
{
cout << "Error: Initial InterstRate" << endl;
exit(0);
}
interestRate = y;
}
double SavingsAccount::calculateInterest()
{
return getBalance() * interestRate;
}
CheckingAccount.h
#ifndef CHECKING_H
#define CHECKING_H
#include "Account.h"
class CheckingAccount : public Account
{
public:
CheckingAccount(double, double = 0.0, AccountType =checkingAccount);
virtual void credit(double);
virtual bool debit(double);
private:
double transactionFee;
void chargeFee();
};
#endif
CheckingAccount.cpp
#include "CheckingAccount.h"
#include<iostream>
using namespace std;
CheckingAccount::CheckingAccount(double x, double y, AccountType a):Account(x,a)
{
if (y < 0)
{
cout << "Error: Initial TransactionFee" << endl;
exit(0);
}
transactionFee = y;
}
void CheckingAccount::credit(double x)
{
Account::credit(x);
chargeFee();
}
bool CheckingAccount::debit(double x)
{
if (Account::debit(x))
{
chargeFee();
return true;
}
else
return false;
}
void CheckingAccount::chargeFee()
{
setBalance(getBalance() - transactionFee);
cout << "$" << transactionFee << " transaction fee charged." << endl;
}
主函数:
#include <iostream>
#include <iomanip>
#include<vector>
#include <typeinfo>
#include "Account.h"
#include "SavingsAccount.h"
#include "CheckingAccount.h"
using namespace std;
int main()
{
vector < Account* > accounts(2);
accounts[0] = new SavingsAccount (25.0, .03);
accounts[1] = new CheckingAccount (80.0, 1.0);
Account* pAccount[] = { accounts[0],accounts[1] };
cout << fixed << setprecision(2);
cout << "account1 balance: $" << accounts[0]->getBalance() << endl;
cout << "account2 balance: $" << accounts[1]->getBalance() << endl;
cout << "\nAttempting to debit $30.00 from account1." << endl;
accounts[0]->debit(30.0);
cout << "\nAttempting to debit $40.00 from account2." << endl;
accounts[1]->debit(40.0);
cout << "\naccount1 balance: $" << accounts[0]->getBalance() << endl;
cout << "account2 balance: $" << accounts[1]->getBalance() << endl;
cout << "\nCrediting $65.00 to account1." << endl;
accounts[0]->credit(65.0);
cout << "\nCrediting $20.00 to account2." << endl;
accounts[1]->credit(20.0);
cout << "\naccount1 balance: $" << accounts[0]->getBalance() << endl;
cout << "account2 balance: $" << accounts[1]->getBalance() << endl;
if (pAccount[0]->checkingAccount)
{
SavingsAccount* ptr;
ptr = dynamic_cast <SavingsAccount*>(accounts[0]);
double interestEarned = ptr->calculateInterest();
cout << "\nAdding $" << interestEarned << " interest to account1."<< endl;
accounts[0]->credit(interestEarned);
cout << "\nNew account1 balance: $" << accounts[0]->getBalance() << endl;
}
return 0;
}
-
dynamic_castd的使用:详见博客
输出结果:
account1 balance: $25.00
account2 balance: $80.00
Attempting to debit $30.00 from account1.
Debit amount exceeded account balance.
Attempting to debit $40.00 from account2.
$1.00 transaction fee charged.
account1 balance: $25.00
account2 balance: $39.00
Crediting $65.00 to account1.
Crediting $20.00 to account2.
$1.00 transaction fee charged.
account1 balance: $90.00
account2 balance: $58.00
Adding $2.70 interest to account1.
New account1 balance: $92.70