130 likes | 239 Views
CS 302 Data Structures. C++ Interlude 1 C++ Classes. Classes. /** @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 :
E N D
CS 302 Data Structures C++ Interlude 1 C++ Classes
/** @file PlainBox.h */ #ifndef _PLAIN_BOX #define _PLAIN_BOX // Set the type of data stored in the box typedefdouble ItemType; // Declaration for the class PlainBox class PlainBox { private: // Data field ItemTypeitem; public: // Default constructor PlainBox(); // Parameterized constructor PlainBox(constItemType & theItem); // Method to change the value of the data field void setItem (constItemType & theItem); // Method to get the value of the data field ItemTypegetItem () const; }; // end PlainBox #endif
/** @file PlainBox.cpp */ #include "PlainBox.h" PlainBox::PlainBox () { } // end default constructor PlainBox::PlainBox (constItemType & theItem) { item = theItem; } // end constructor void PlainBox::setItem (constItemType & theItem) { item = theItem; } // end setItem ItemTypePlainBox::getItem () constconst { return item; } // end getItem
/** @file PlainBox.h */ #ifndef _PLAIN_BOX #define _PLAIN_BOX template < class ItemType >; // Indicates this is a template definition // Declaration for the class PlainBoxclass PlainBox { private: // Data field ItemTypeitem; public: // Default constructor PlainBox(); // Parameterized constructor PlainBox(constItemType & theItem); // Mutator method that can change the value of the data field void setItem (constItemType & theItem); // Accessor method to get the value of the data field ItemTypegetItem () const; }; // end PlainBox #include "PlainBox.cpp" // Include the implementation file #endif
/** @file PlainBox.cpp */ template < class ItemType >; PlainBox< ItemType >::PlainBox () { } // end default constructor template < class ItemType >; PlainBox< ItemType >::PlainBox (constItemType & theItem) { item = theItem; } // end constructor template < class ItemType >; void PlainBox < ItemType >::setItem (constItemType & theItem) { item = theItem; } // end setItem template < class ItemType >; ItemTypePlainBox < ItemType >::getItem () constconst { return item; } // end getItem
/** @file ToyBox.h */ #ifndef _TOY_BOX #define _TOY_BOX #include "PlainBox.h" enumColor { BLACK, RED, BLUE, GREEN, YELLOW, WHITE }; template < class ItemType > class ToyBox:publicPlainBox < ItemType > { private: Color boxColor; public: ToyBox(); ToyBox(const Color & theColor); ToyBox(constItemType & theItem, const Color & theColor); Color getColor () const; }; // end ToyBox #include "ToyBox.cpp" #endif
/** @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 (constItemType & 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
/** @file BoxInterface.h */ #ifndef _BOX_INTERFACE #define _BOX_INTERFACE template < class ItemType > class BoxInterface { public: virtual void setItem (constItemType & theItem) = 0; virtual ItemTypegetItem () const = 0; }; // end BoxInterface #endif