CS 302 Data Structures C++ Interlude 1 C++ Classes
Description: CS 302 Data Structures C Interlude 1 C Classes Classes file PlainBox.h ifndef PLAINBOX define PLAINBOX Set the type of data stored in the box typedef double ItemType; Declaration for the class PlainBox class PlainBox
Related Topics
Download Presentation
"CS 302 Data Structures C++ Interlude 1 C++ Classes" is the property of its rightful owner. Permission is granted to download and print the materials on this website for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Presentation Transcript
slide1. CS 302 Data Structures C++ Interlude 1
C++ Classes<br>
slide2. Classes<br>
slide3. /** @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<br>
slide4. /** @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<br>
slide5. Templates<br>
slide6. /** @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<br>
slide7. /** @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<br>
slide8. Inheritance<br>
slide9. /** @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<br>
slide10. /** @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<br>
slide11. Abstract Classes<br>
slide12. /** @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<br>
slide13. End of C++ Interlude 1<br>
C++ Classes<br>
slide2. Classes<br>
slide3. /** @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<br>
slide4. /** @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<br>
slide5. Templates<br>
slide6. /** @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<br>
slide7. /** @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<br>
slide8. Inheritance<br>
slide9. /** @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<br>
slide10. /** @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<br>
slide11. Abstract Classes<br>
slide12. /** @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<br>
slide13. End of C++ Interlude 1<br>