/* Simple bank account using classes and objects. */ #include using namespace std; class Account { public: Account(); Account(string fname, string lname, int number, double balance); void Set(string fname, string lname, int number, double balance); void Deposit(double amount); void Withdrawal(double amount); double Balance(); void PrintInfo(); private: string m_firstName; string m_lastName; int m_number; double m_balance; }; int main() { // Create and initialize Account object Account act("Chris", "Marron", 14314, 0.0); // Good programmer! Uses defined functions // that include error checking act.PrintInfo(); act.Deposit(100.0); // Deposit(act, 100.0) for struct cout << "Balance is " << act.Balance() << endl; act.Withdrawal(150.0); cout << "Balance is " << act.Balance() << endl << endl; // Bad programmer! Bypasses error checking // This doesn't work since balance is declared 'private' act.m_balance -= 150.0; cout << "Balance is " << act.Balance() << endl; return 0; } Account::Account() { // default constructor - do nothing } Account::Account(string fname, string lname, int number, double balance) { m_firstName = fname; m_lastName = lname; m_number = number; if (balance >= 0.0) m_balance = balance; else m_balance = 0.0; } void Account::Set(string fname, string lname, int number, double balance) { m_firstName = fname; m_lastName = lname; m_number = number; if (balance >= 0.0) m_balance = balance; else cerr << "Set: initial balance must be >= 0." << endl; } void Account::Deposit(double amount) { // could add loging, etc. if (amount > 0.0) m_balance += amount; else cerr << "Deposit: amount must be positive." << endl; } void Account::Withdrawal(double amount) { // could add loging, etc. if (amount > 0.0) if (amount <= m_balance) m_balance -= amount; else cerr << "Withdrawal: insufficient funds." << endl; else cerr << "Withdrawal: amount must be positive." << endl; } double Account::Balance() { // could add loging, etc. return m_balance; } void Account::PrintInfo() { cout << "Account number: " << m_number << endl; cout << "Owner: " << m_firstName << " " << m_lastName << endl; cout << "Balance: " << m_balance << endl << endl; }