Overload the -> operator

Previous      Next      acount.cc

#include <string.h>
#include <iostream>
using namespace std;

class Account {
   double account_balance;
   char *owner;

public:
   Account (const char *name) {
      owner = new char[strlen(name)+1];
      strcpy(owner, name);
      account_balance = 0;
   }
   
   double balance () { return account_balance; }
   
   void deposit (double amount) { account_balance += amount; }
};

int main () {
   Account john("John");
   john.deposit(10.19);
   cout << "$" << john.balance() << "\n";
   john.deposit(21.30);
   cout << "$" << john.balance() << "\n";
   john.deposit(87.29);
   cout << "$" << john.balance() << "\n";
}