Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Interlude 1 C++ Classes

Similar presentations


Presentation on theme: "C++ Interlude 1 C++ Classes"— Presentation transcript:

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

2 Classes

3 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 field void setItem (const ItemType & theItem); // Method to get the value of the data field ItemType getItem () const; }; // end PlainBox #endif

4 /. @file PlainBox. cpp. / #include "PlainBox
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) } // end setItem ItemType PlainBox::getItem () const const return item; } // end getItem

5 Templates

6 PlainBox.h */ #ifndef _PLAIN_BOX #define _PLAIN_BOX template < class ItemType >; // Indicates this is a template definition // Declaration for the class PlainBox class PlainBox { private: // Data field ItemType item; public: // Default constructor PlainBox (); // Parameterized constructor PlainBox (const ItemType & theItem); // Mutator method that can change the value of the data field void setItem (const ItemType & theItem); // Accessor method to get the value of the data field ItemType getItem () const; }; // end PlainBox #include "PlainBox.cpp" // Include the implementation file #endif

7 PlainBox.cpp */ template < class ItemType >; PlainBox < ItemType >::PlainBox () { } // end default constructor PlainBox < ItemType >::PlainBox (const ItemType & theItem) item = theItem; } // end constructor void PlainBox < ItemType >::setItem (const ItemType & theItem) } // end setItem ItemType PlainBox < ItemType >::getItem () const const return item; } // end getItem

8 Inheritance

9 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

10 ToyBox.cpp */ template < class ItemType > ToyBox < ItemType >::ToyBox () { PlainBox < ItemType > (); boxColor = BLACK; } // end default constructor ToyBox < ItemType >::ToyBox (const Color & theColor) boxColor = theColor; } // end constructor ToyBox < ItemType >::ToyBox (const ItemType & theItem, const Color & theColor) PlainBox < ItemType >::setItem (theItem); Color ToyBox < ItemType >::getColor () constconst return boxColor; } // end getColor

11 Abstract Classes

12 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

13 End of C++ Interlude 1


Download ppt "C++ Interlude 1 C++ Classes"

Similar presentations


Ads by Google