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

CS 302 Data Structures

  • Upload
    rivka

  • View
    22

  • Download
    2

Embed Size (px)

DESCRIPTION

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 : - PowerPoint PPT Presentation

Citation preview

Page 1: CS 302  Data Structures

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

Page 2: CS 302  Data Structures

Classes

Page 3: CS 302  Data Structures

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

Page 4: CS 302  Data Structures

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

Page 5: CS 302  Data Structures

Templates

Page 6: CS 302  Data Structures

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

Page 7: CS 302  Data Structures

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

Page 8: CS 302  Data Structures

Inheritance

Page 9: CS 302  Data Structures

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

Page 10: CS 302  Data Structures

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

Page 11: CS 302  Data Structures

Abstract Classes

Page 12: CS 302  Data Structures

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

Page 13: CS 302  Data Structures

End of C++ Interlude 1