C++大学教程第13章实验报告

C++大学教程第13章实验报告
C++大学教程第13章实验报告

姓名:班级:计科1203

日期:2013. 11. 27 学号:

实验六(13.16)

使用Account类层次结构开发的多态性的银行系统1、代码实现:

// Account.h

#ifndef ACCOUNT_H

#define ACCOUNT_H

class Account

{

public:

Account( double = 0.0 );

virtual void credit(double);

virtual bool debit(double);

void setBalance(double);

double getBalance( );

private:

double balance;

};

# endif

// Account.cpp

#include

using std::cout;

using std::cin;

using std::endl;

using std::cerr;

#include"Account.h"

Account::Account( double b )

{

if(b < 0)

{

balance = 0.0;

cerr << "Error: an invalid value !" << endl;

}

else

balance = b;

}

void Account::credit( double amount)

{

balance = balance + amount;

}

bool Account::debit(double amount )

{

if(amount <= balance)

{

balance = balance - amount;

return true;

}

else

{

cout << "Debit amount exceeded account balance !";

return false;

}

}

void Account::setBalance( double newBalance )

{

balance = newBalance;

}

double Account::getBalance( )

{

return balance;

}

// SavingsAccount.h

#include"Account.h"

#ifndef SAVINGSACCOUNT_H

#define SAVINGSACCOUNT_H

class SavingsAccount : public Account

{

public:

SavingsAccount(double, double = 0.0);

~SavingsAccount();

double caculateInterest( );

private:

double interestrate;

};

#endif

// SavingsAccount.cpp

#include

using std::cout;

using std::cin;

using std::endl;

#include"SavingsAccount.h"

SavingsAccount::SavingsAccount( double b, double i) :Account( b )

{

interestrate = i;

}

SavingsAccount::~SavingsAccount( )

{

}

double SavingsAccount::caculateInterest( )

{

double interest;

interest = Account::getBalance( ) * interestrate;

return interest;

}

// CheckingAccount.h

#include"Account.h"

#ifndef CHECKINGACCOUNT_H

#define CHECKINGACCOUNT_H

class CheckingAccount:public Account

{

public:

CheckingAccount(double,double = 0.0);

void setFeechargedpertransaction(double);

double getFeechargedpertransaction();

virtual void credit(double);

virtual bool debit(double);

private:

double feechargedpertransaction;

};

#endif

// CheckingAccount.cpp

#include

using std::cout;

using std::cin;

using std::endl;

#include"CheckingAccount.h"

CheckingAccount::CheckingAccount(double b,double f)

:Account ( b )

{

feechargedpertransaction = f;

}

void CheckingAccount::setFeechargedpertransaction( double f ) {

cout << "enter feechargedpertransaction :" << endl;

cin >> f;

feechargedpertransaction = f;

}

double CheckingAccount::getFeechargedpertransaction( )

{

return feechargedpertransaction;

}

void CheckingAccount::credit( double a )

{

Account::credit( a );

}

bool CheckingAccount::debit(double a)

{

return Account::debit( a );

}

//testAccount.cpp

#include

using std::cout;

using std::endl;

using std::fixed;

#include

using std::setprecision;

#include

using std::vector;

#include"Account.cpp"

#include"SavingsAccount.cpp"

#include"CheckingAccount.cpp"

int main()

{

vector < Account * > accounts(4);

accounts[ 0 ] = new SavingsAccount( 25.0, 0.03 );

accounts[ 1 ] = new CheckingAccount( 80.0, 1.0 );

accounts[ 2 ] = new SavingsAccount( 200.0, 0.02 );

accounts[ 3 ] = new CheckingAccount( 400.0, 1.5 );

cout << fixed << setprecision( 2 );

for( size_t i = 0; i < accounts.size(); i ++ )

{

cout << "Account" << i + 1 << "original balance: $"<< accounts[i]->getBalance() << endl;

cout << "\nwithdraw $260.00 from Account " << i + 1 << ".\n";

accounts[i]->debit( 260.0 ); // 取款

cout << "\nUpdated Account" << i + 1 << "balance: $"<< accounts[i]->getBalance() << endl;

cout << "\ndeposit $150.00 into Account " << i + 1 << ".\n";

accounts[i]->credit( 150.0 ); // 存款

cout << "UpdatedAccount" << i + 1 << " balance: $"<< accounts[i]->getBalance() << endl;

SavingsAccount *savingsAccountPtr = ( SavingsAccount * ) accounts[i];

CheckingAccount *checkingAccountPtr = ( CheckingAccount * ) accounts[i];

if ( i % 2 == 0) //存款账户

{

if ( savingsAccountPtr != NULL )

{

double interestEarned = savingsAccountPtr->caculateInterest();

cout << "\nAdding $" << interestEarned << " interest to Account "

<< i + 1 << "(a SavingsAccount)" << endl;

savingsAccountPtr->credit( interestEarned ); //存款后计算本利和

}

cout << "Updated Account " << i + 1 << " balance: $"

<< savingsAccountPtr->getBalance()<< "\n\n";

}

else //支票账户

{

if ( checkingAccountPtr != NULL )

{

double feeLost = checkingAccountPtr->getFeechargedpertransaction();

cout << "\nSubstracting $" << feeLost << " fee to Account "

<< i + 1 << " (a checkingAccount) " << endl;

checkingAccountPtr->debit( feeLost );

}

cout << "Updated Account " << i + 1 << " balance: $"

<< checkingAccountPtr->getBalance()<< "\n\n";

}

}

return 0;

}

2、测试结果:

相关主题
相关文档
最新文档