1 / 13

CS 302 Data Structures

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 :

rivka
Télécharger la présentation

CS 302 Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS 302 Data Structures C++ Interlude 1 C++ Classes

  2. Classes

  3. /** @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

  4. /** @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

  5. Templates

  6. /** @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

  7. /** @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

  8. Inheritance

  9. /** @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

  10. /** @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

  11. Abstract Classes

  12. /** @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

  13. End of C++ Interlude 1

More Related