//DISPLAY 10.5 The BankAccount Class//Program to demonstrate the class BankAccount.#include<iostream>using namespace std;//Class for a bank account:classBankAccount {public:voidset(intdollars,intcents,doublerate);//Postcondition: The account balance has been set to $dollars.cents;//The interest rate has been set to rate percent.voidset(intdollars,doublerate);//Postcondition: The account balance has been set to $dollars.00.//The interest rate has been set to rate percent.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.private:doublebalance;doubleinterest_rate;doublefraction(doublepercent);//Converts a percentage to a fraction. For example, fraction(50.3)//returns 0.503.};intmain( ) { BankAccount account1, account2; cout <<"Start of Test:\n"; account1.set(123, 99, 3.0); cout <<"account1 initial statement:\n"; account1.output(cout); account1.set(100, 5.0); cout <<"account1 with new setup:\n"; account1.output(cout); account1.update( ); cout <<"account1 after update:\n"; account1.output(cout); account2 = account1; cout <<"account2:\n"; account2.output(cout);return0; }voidBankAccount::set(intdollars,intcents,doublerate) {if((dollars < 0) || (cents < 0) || (rate < 0)) { cout <<"Illegal values for money or interest rate.\n"; } balance = dollars + 0.01*cents; interest_rate = rate; }voidBankAccount::set(intdollars,doublerate) {if((dollars < 0) || (rate < 0)) { cout <<"Illegal values for money or interest rate.\n"; } balance = dollars; interest_rate = rate; }voidBankAccount::update( ) { balance = balance + fraction(interest_rate)*balance; }doubleBankAccount::fraction(doublepercent_value) {return(percent_value/100.0); }doubleBankAccount::get_balance( ) {returnbalance; }doubleBankAccount::get_rate( ) {returninterest_rate; }//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; }

//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.private:doublebalance;doubleinterest_rate;doublefraction(doublepercent);//Converts a percentage to a fraction. For example, fraction(50.3)//returns 0.503.};intmain( ) { BankAccount account1(100, 2.3), account2; cout <<"account1 initialized as follows:\n"; account1.output(cout); cout <<"account2 initialized as follows:\n"; account2.output(cout); account1 = BankAccount(999, 99, 5.5); cout <<"account1 reset to the following:\n"; account1.output(cout);return0; }BankAccount::BankAccount(intdollars,intcents,doublerate) {if((dollars < 0) || (cents < 0) || (rate < 0)) { cout <<"Illegal values for money or interest rate.\n"; } 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; }

//DISPLAY 10.7 Alternative BankAccount Class Implementation//Demonstrates an alternative implementation of the class BankAccount.#include<iostream>#include<cmath>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.private:intdollars_part;intcents_part;doubleinterest_rate;//expressed as a fraction, for example, 0.057 for 5.7%doublefraction(doublepercent);//Converts a percentage to a fraction. For example, fraction(50.3)//returns 0.503.doublepercent(doublefraction_value);//Converts a fraction to a percentage. For example, percent(0.503)//returns 50.3.};intmain( ) { BankAccount account1(100, 2.3), account2; cout <<"account1 initialized as follows:\n"; account1.output(cout); cout <<"account2 initialized as follows:\n"; account2.output(cout); account1 = BankAccount(999, 99, 5.5); cout <<"account1 reset to the following:\n"; account1.output(cout);return0; }BankAccount::BankAccount(intdollars,intcents,doublerate) {if((dollars < 0) || (cents < 0) || (rate < 0)) { cout <<"Illegal values for money or interest rate.\n"; } dollars_part = dollars; cents_part = cents; interest_rate = fraction(rate); }BankAccount::BankAccount(intdollars,doublerate) {if((dollars < 0) || (rate < 0)) { cout <<"Illegal values for money or interest rate.\n"; } dollars_part = dollars; cents_part = 0; interest_rate = fraction(rate); }BankAccount::BankAccount( ) : dollars_part(0), cents_part(0), interest_rate(0.0) {//Body intentionally empty.}doubleBankAccount::fraction(doublepercent_value) {return(percent_value/100.0); }//Uses cmath:voidBankAccount::update( ) {doublebalance = get_balance( ); balance = balance + interest_rate*balance; dollars_part = floor(balance); cents_part = floor((balance - dollars_part)*100); }doubleBankAccount::get_balance( ) {return(dollars_part + 0.01*cents_part); }doubleBankAccount::percent(doublefraction_value) {return(fraction_value*100); }doubleBankAccount::get_rate( ) {returnpercent(interest_rate); }//Uses iostream:voidBankAccount::output(ostream& outs) { outs.setf(ios::fixed); outs.setf(ios::showpoint); outs.precision(2); outs <<"Account balance $"<< get_balance( ) << endl; outs <<"Interest rate "<< get_rate( ) <<"%"<< endl; }

Edit | Attach | Print version | History: r1 | Backlinks | Raw View | Raw edit | More topic actions

Topic revision: r1 - 2015-11-09 - JimSkon

Copyright &© 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