//DISPLAY 10.6 /Program to demonstrate the class BankAccount.//Demonstrates an alternative implementation of the class BankAccount.#include<iostream>using namespace std;//Class for a bank account:classBankAccount {public: BankAccount(intdollars,intcents,doublerate);//Initializes the account balance to $dollars.cents and//initializes the interest rate to rate percent.BankAccount(intdollars,doublerate);//Initializes the account balance to $dollars.00 and//initializes the interest rate to rate percent.BankAccount();//Initializes the account balance to $0.00 and the interest rate to 0.0%.voidupdate();//Postcondition: One year of simple interest has been added to the account//balance.doubleget_balance();//Returns the current account balance.doubleget_rate();//Returns the current account interest rate as a percentage.voidoutput(ostream& outs);//Precondition: If outs is a file output stream, then//outs has already been connected to a file.//Postcondition: Account balance and interest rate//have been written to the stream outs.friendbooloperator>(BankAccount a, BankAccount b);friendBankAccountoperator+(BankAccount a, BankAccount b);friendBankAccountoperator+(BankAccount a,intb);private:doublebalance;doubleinterest_rate;doublefraction(doublepercent);//Converts a percentage to a fraction. For example, fraction(50.3)//returns 0.503.};

#include"BankAccount.h"BankAccount::BankAccount(intdollars,intcents,doublerate) {if((dollars < 0) || (cents < 0) || (rate < 0)) { cout <<"Illegal values for money or interest rate.\n"; balance = 0; interest_rate = 0; }else{ balance = dollars + 0.01 * cents; interest_rate = rate; } }BankAccount::BankAccount(intdollars,doublerate) {if((dollars < 0) || (rate < 0)) { cout <<"Illegal values for money or interest rate.\n"; } balance = dollars; interest_rate = rate; }BankAccount::BankAccount() : balance(0), interest_rate(0.0) {//Body intentionally empty.}//Uses iostream:voidBankAccount::output(ostream& outs) { outs.setf(ios::fixed); outs.setf(ios::showpoint); outs.precision(2); outs <<"Account balance $"<< balance << endl; outs <<"Interest rate "<< interest_rate <<"%"<< endl; }voidBankAccount::update() { balance += balance * fraction(interest_rate); }doubleBankAccount::fraction(doublepercent) {returnpercent / 100; }doubleBankAccount::get_balance() {returnbalance; }booloperator>(BankAccount a,BankAccount b) {returna.balance>b.balance; } BankAccountoperator+(BankAccount a,BankAccount b) { BankAccount newAccount; newAccount.balance=a.balance+b.balance; newAccount.interest_rate=max(a.interest_rate,b.interest_rate);returnnewAccount; } BankAccountoperator+(BankAccount a,intb) { BankAccount newAccount; newAccount.balance=a.balance+b; newAccount.interest_rate=a.interest_rate;returnnewAccount; }

#include"BankAccount.h"intmain() { BankAccount account1(100, 8.3), account2(3000, 33, 6.2); cout <<"account1 initialized as follows:\n"; account1.output(cout); cout <<"account2 initialized as follows:\n"; account2.output(cout); account1.update(); account1.update(); account1.update(); account1.update(); account2.update(); cout <<"account1 is:\n"; account1.output(cout); cout <<"account2 is:\n"; account2.output(cout);if(account2 > account1) { cout <<"2 is has more money"; }else{ cout <<"1 is has more money"; } BankAccount a3 = account1 + account2; cout <<"New account is:\n"; a3.output(cout); BankAccount a4 = account1 + 1500; cout <<"New account is:\n"; a4.output(cout);return0; }

This topic: SCMP118/Fall2016 > WebHome > CodeMultiArrays

Topic revision: r3 - 2019-10-31 - JimSkon

Copyright © 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.

Ideas, requests, problems regarding TWiki? Send feedback

Ideas, requests, problems regarding TWiki? Send feedback