//Program to demonstrate the class BankAccount. #include //Class for a bank account: class BankAccount { public: BankAccount(int dollars, int cents, double rate); //Initializes the account balance to $dollars.cents and //initializes the interest rate to rate percent. BankAccount(int dollars, double rate); //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%. void update( ); //Postcondition: One year of simple interest has been added to //the account balance. double get_balance( ); //Returns the current account balance. double get_rate( ); //Returns the current account interest rate as a percent. void output(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: double balance; double interest_rate; double fraction(double percent); //Converts a percent to a fraction. For example, fraction(50.3) returns 0.503. }; int main( ) { 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); return 0; } BankAccount::BankAccount(int dollars, int cents, double rate) { balance = dollars + 0.01*cents; interest_rate = rate; } BankAccount::BankAccount(int dollars, double rate) { balance = dollars; interest_rate = rate; } BankAccount::BankAccount( ) { balance = 0; interest_rate = 0.0; } void BankAccount::update( ) { balance = balance + fraction(interest_rate)*balance; } double BankAccount::fraction(double percent) { return (percent/100.0); } double BankAccount::get_balance( ) { return balance; } double BankAccount::get_rate( ) { return interest_rate; } //Uses iostream.h: void BankAccount::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; }