Code Listings Chapter CP 1

Listing C1-1
Listing C1-2
Listing C1-3
Listing C1-4
Listing C1-5
Listing C1-6
Listing C1-7
Listing C1-8
Listing C1-9

 

Listing C1-1  The header file for the class PlainBox

/** @file PlainBox.h */
#ifndef _PLAIN_BOX
#define _PLAIN_BOX

// Set the type of data stored in the box
typedef double ItemType;

// Declaration for the class PlainBox
class PlainBox
{
  private:
// Data field
  ItemType item;
  public:
// Default constructor
  PlainBox ();

// Parameterized constructor
  PlainBox (const ItemType & theItem);

// Method to change the value of the data fi eld
  void setItem (const ItemType & theItem);

// Method to get the value of the data fi eld
  ItemType getItem () const;

};  // end PlainBox
#endif

 

 

Listing  C1-2 Implementation file for the PlainBox class

/** @file PlainBox.cpp */
#include "PlainBox.h"

PlainBox::PlainBox ()
{
}   // end default constructor

PlainBox::PlainBox (const ItemType & theItem)
{
  item = theItem;
}   // end constructor

void
PlainBox::setItem (const ItemType & theItem)
{
  item = theItem;
}   // end setItem

ItemType
PlainBox::getItem () const const
{
  return item;
}   // end getItem

 

Listing  C1-3 Template header file for the PlainBox class

/** @file PlainBox.h */
#ifndef _PLAIN_BOX
#define _PLAIN_BOX
template < class ItemType >;	// Indicates this is a template defi nition
// Declaration for the class PlainBox

class PlainBox
{
  private:
// Data fi eld
  ItemType item;

  public:
// Default constructor
  PlainBox ();

// Parameterized constructor
  PlainBox (const ItemType & theItem);

// Mutator method that can change the value of the data fi eld
  void setItem (const ItemType & theItem);

// Accessor method to get the value of the data fi eld
  ItemType getItem () const;

};  // end PlainBox

#include "PlainBox.cpp"	// Include the implementation fi le
#endif

 

 

Listing C1-4  Implementation file for the PlainBox template class

/** @file PlainBox.cpp */
template < class ItemType >;

PlainBox < ItemType >::PlainBox ()
{
}   // end default constructor

template < class ItemType >;
PlainBox < ItemType >::PlainBox (const ItemType & theItem)
{
  item = theItem;
}   // end constructor

template < class ItemType >;
void PlainBox < ItemType >::setItem (const ItemType & theItem)
{
  item = theItem;
}   // end setItem

template < class ItemType >;
ItemType PlainBox < ItemType >::getItem () const const
{
  return item;
}   // end getItem

 

Listing  C1-5,  Template header file for the class ToyBox

/** @file ToyBox.h */
#ifndef _TOY_BOX
#define _TOY_BOX
#include "PlainBox.h"

enum Color
{ BLACK, RED, BLUE, GREEN, YELLOW, WHITE };

template < class ItemType > class ToyBox:public PlainBox < ItemType >
{
private:
  Color boxColor;

public:
  ToyBox ();
  ToyBox (const Color & theColor);

  ToyBox (const ItemType & theItem, const Color & theColor);

  Color getColor () const;

};  // end ToyBox

#include "ToyBox.cpp"
#endif

 

Listing C1-6  Implementation file for the class ToyBox

/** @file ToyBox.cpp */

template < class ItemType > ToyBox < ItemType >::ToyBox ()
{
  PlainBox < ItemType > ();
  boxColor = BLACK;
}   // end default constructor

template < class ItemType > ToyBox < ItemType >::ToyBox (const Color & theColor)
{
  PlainBox < ItemType > ();
  boxColor = theColor;
}   // end constructor

template < class ItemType > ToyBox < ItemType >::ToyBox (const ItemType & theItem, const Color & theColor)
  PlainBox <
ItemType > ();

PlainBox < ItemType >::setItem (theItem);
boxColor = theColor;
}   // end constructor

template < class ItemType > Color ToyBox < ItemType >::getColor () constconst
{
  return boxColor;
}   // end getColor

 

Listing C1-7  Header file for the class MagicBox

/** @file MagicBox.h */
#ifndef _MAGIC_BOX
#define _MAGIC_BOX
#include "PlainBox.h"
template < class ItemType > class MagicBox:public PlainBox < ItemType >
{
private:
  bool firstItemStored;

public:
  MagicBox ();
  MagicBox (const ItemType & theItem);

  void setItem (const ItemType & theItem);

};  // end MagicBox

#include "MagicBox.cpp"
#endif

 

Listing C1-8 Implementation file for the class MagicBox

/** @file MagicBox.cpp */
template < class ItemType > MagicBox < ItemType >::MagicBox ()
{
  PlainBox < ItemType > ();
  fi rstItemStored = false;	// Box has no magic initially
}   // end default constructor

template < class ItemType > MagicBox < ItemType >::MagicBox (const ItemType & theItem)
{
  fi rstItemStored = false;	// Box has no magic initially

  setItem (theItem);
// Box has magic now
}   // end constructor

template < class ItemType > void MagicBox < ItemType >::setItem (const ItemType & theItem)
{
  if (!fi rstItemStored)
    {
      PlainBox < ItemType >::setItem (theItem);
      fi rstItemStored = true;	// Box now has magic
    }	// end if
}   // end setItem

 

Listing C1-9  An abstract class that is an interface for the ADT box

/** @file BoxInterface.h */
#ifndef _BOX_INTERFACE
#define _BOX_INTERFACE

template < class ItemType > class BoxInterface
{
public:

  virtual void setItem (const ItemType & theItem) = 0;

  virtual ItemType getItem () const = 0;

};  // end BoxInterface

#endif