#include <iostream>
#include <cstdlib>

using namespace std;

class BankAccount
{
public:
   BankAccount();
   void deposit(int amount);
   int get_balance() const;
private:
   int balance;
};

BankAccount::BankAccount()
   : balance(0)
{}

void BankAccount::deposit(int amount)
{
   balance = balance + amount;
}

int BankAccount::get_balance() const
{
   return balance;
}

// Here is a completely unrelated class

class Coin
{
public:
   Coin();
   void flip();
   bool get_value() const;
private:
   bool value;
};

Coin::Coin()
   : value(false)
{}

void Coin::flip()
{
   int r = rand();
   if (r % 2 == 0)
      value = false;
   else
      value = true;
}

bool Coin::get_value() const
{
   return value;
}

int main()
{
   // Make an account, deposit some money and print its balance

   BankAccount* harrys_account = new BankAccount();
   harrys_account->deposit(10000);
   cout << "Before coin flip: " << harrys_account->get_balance() << endl;

   // Here we cast the bank account pointer into an unrelated type
   // This is perfectly legal in C++

   Coin* lucky_penny = (Coin*)harrys_account;
   lucky_penny->flip();

   cout << "After coin flip: " << harrys_account->get_balance() << endl;
   return 0;
}

/*

Output:
Before coin flip: 10000
After coin flip: 1

Explanation:

In C++, it is legal to cast pointers from one type to another.
There are neither compile-time nor run-time checks whether that
makes sense. 

When you make such as a cast, the resulting pointer simply accesses
the memory as if had been laid out as described in the target class.

*/
