C++ 银行账户的多态继承层次结构

这篇博客展示了如何使用C++实现一个账户基类`Account`,包括子类`SavingsAccount`和`CheckingAccount`。每个类都有其特定的功能,如储蓄账户的利息计算和支票账户的交易费。程序中使用了`dynamic_cast`进行类型转换,以在主函数中正确地调用特定账户类型的方法。博客还讨论了`exit(0)`在程序中的作用,表示程序正常结束。
摘要由CSDN通过智能技术生成

目录

Account.h

Account.cpp

exit(0):表示正常运行程序并退出程序

SavingsAccount.h

SavingsAccount.cpp

CheckingAccount.h

CheckingAccount.cpp

主函数:

  dynamic_castd的使用:详见博客


 

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

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ItsNorth

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值