SOURCE FILE: main.cpp


//  Created by Frank M. Carrano and Tim Henry.
//  Copyright (c) 2013 __Pearson Education__. All rights reserved.

// Section C2.2

#include <iostream>
#include <string>
#include "PlainBox.h"
#include "MagicBox.h"

using namespace std;

/* Pass a PlainBox as a reference parameter and 
   use its setItem method to put theItem into the box */
void placeInBox(PlainBox<string>& theBox, string theItem)
{
   theBox.setItem(theItem);
} // end placeInBox

int main()
{
   string specialItem = "Riches beyond compare!";
   string otherItem = "Hammer";
   
   PlainBox<string> myPlainBox;
   placeInBox(myPlainBox, specialItem);
   
   MagicBox<string> myMagicBox;
      /* Attempt to store two strings into a MagicBox, 
         by passing it to a functiont that expects a 
         reference parameter that is a PlainBox. */        
   placeInBox(myMagicBox, otherItem);
   placeInBox(myMagicBox, specialItem);     // specialItem is stored!

     /* If this writes "Riches beyond compare!", then 
        the 'wrong' setItem method was called. */   
   cout << myMagicBox.getItem() << endl;    // "Riches beyond compare!"
  
   /* Now try a call directly */
   myMagicBox.setItem(otherItem) ; // "Hammer"
   myMagicBox.setItem(specialItem) ; // "Riches beyond compare!"
        /* When this writes "Hammer", it shows that the 
           setItem of MagicBox was used for the last two calls */
   cout << myMagicBox.getItem() << endl;    // "Hammer"

      /* Initialize a PlainBox using the constructor 
         of the MagicBox class  */   
   PlainBox<string> mySpecialBox = MagicBox<string>();
   mySpecialBox.setItem(otherItem);
   mySpecialBox.setItem(specialItem);      // specialItem is stored!
     /* Again, if this writes "Riches beyond compare!", then 
        the 'wrong' setItem method was called. */   
   cout << mySpecialBox.getItem() << endl; // "Riches beyond compare!"

   PlainBox<string> PlainBox2;
   MagicBox<string> MagicBox2;
   PlainBox2 = MagicBox2 ;
   PlainBox2.setItem(otherItem) ; // "Hammer" 
   PlainBox2.setItem(specialItem) ; // "Riches beyond compare!"
     /* If this writes "Riches beyond compare!", then 
        the 'wrong' setItem method was called. */   
   cout << PlainBox2.getItem() << endl;    // "Riches beyond compare!"

   return 0;
}  // end main

/*
Riches beyond compare!
Hammer
Riches beyond compare!
Riches beyond compare!
*/