//Bank Account Class (with Constructors) //Program to demonstrate the class BankAccount. //Author: A. Guercio - Feb.18, 2008 #include #include using namespace std; //Class for a bank account starts here: class BankAccount { public: //first we define the constructors //constructor number 1 BankAccount(double amount, double rate) //Initializes the account balance to the amount //initializes the interest rate to rate percent. {if ((amount < 0) || (rate < 0)) { cout << "Illegal values for money or interest rate.\n"; exit(1); } balance = amount; interest = 0.0; number = rand(); interest_rate = rate; } //constructor number 2 BankAccount( ) //Initializes the account balance to $0.00 and the interest rate to 0.0%. {balance = 0.0; interest = 0.0; interest_rate = 0.0; } //here starts the list of the methods of the class each with its implementation void deposit(double amount) //The amount is added to the balance of the account. { balance = balance + amount; } void withdraw(double amount) //The amount is subtracted to the balance of the account. { balance = balance - amount; } void compute_interest() //Computes the interest of the account {interest = balance * interest_rate; } void update() //Updates the current account with the interest { balance = balance + interest; } int get_number() //Returns the number of the current account. { return number; } double get_interest() //Returns the interest of the current account. {return interest; } double get_balance( ) //Returns the current account balance. {return balance; } double get_rate( ) //Returns the current account interest rate as a percentage. {return interest_rate; } //finally this is the part were we hide the information (the attributes of the class) private: int number; double balance; double interest; double interest_rate; }; //this is the end of the class //this is the beginning of the test driver (another term to indicate the main) int main( ) {double balance; const double RATE = 0.05; cout << "How much money to you want to start the account with? " << endl; cout << "Insert the amount here: $"; cin >> balance; // we create a bank account by using the constructor number 1 BankAccount account1(balance, RATE); cout << "Your account has just been created. The ID number of your account is " << account1.get_number() << "." << endl; cout << "Your account has $" << account1.get_balance() << " in it." << endl; //we ask the account to add the amount to deposit to the balance using the deposit method of the account account1.deposit(1000); cout << "Your deposit has been performed. Your account now has $" << account1.get_balance() << " in it." << endl; //we ask the account to subtract the amount to withdraw from the balance using the withdraw method of the account account1.withdraw(300); cout << "Your withdrawal has been performed. Your account now has $" << account1.get_balance() << " in it." << endl; //we ask the account to compute the interest of the account account1.compute_interest(); cout << "The interest has been computed. Your account has matured $" << account1.get_interest() << " in it." << endl; account1.update(); cout << "Your account has been updated with your interest. Your balance now is $" << account1.get_balance() << endl; cout << "Bye! " << endl; return 0; }