Listing 1 (dates.cpp) Simple Date Class

#include <iostream.h>
#include <iomanip.h>

// Crude date class to demonstrate
// customized stream I/O -- no error checking attempted

class date
     {
     unsigned int year;  // 0-99
     unsigned int mon;   // 1-12
     unsigned int day;   // 1-31
// Allow the I/O stream operators to access the private members
// of the date class. You can't define these as members
// because the first argument is a stream, not a date.
     friend ostream & operator <<(ostream &s,date dt);
     friend istream & operator >>(istream &s,date &dt);
public:
// constructor
     date(int _mon=1,int _day=1,int _year=0)
        { mon=_mon; day=_day; year=_year; }
     };

// Output a date as: MM/DD/YY
// No attempt was made to pad the elements with zeros
ostream & operator <<(ostream &s,date dt)
   {
   s << dt.mon << "/" << dt.day << "/" << dt.year;
   return s;
   }

// Input a date in the format: MM/DD/YY -- allow any character
// to seperate the elements (i.e., MM-DD-YY, MM,DD,YY, etc.)
// Notice that the date is a reference (&dt) so we modify
// the actual date -- not a copy passed by value.
istream & operator >>(istream &s,date &dt)
   {
   int m,d,y;
   char dummy;  // this char holds the seperator
   s >> m >> dummy >> d >> dummy >> y;
   dt.mon=m;
   dt.day=d;
   dt.year=y;
   return s;
   }

// Simple demo for dates. Notice how the stream I/O operators
// have been overloaded to accept the date class.
main()
  {
  date jan1(1,1,70);
  date bday;
  cout << "Enter your birthday (MM/DD/YY): ";
  cin >> bday;
  cout << "The first date is " << jan1 << "\n";
  cout << "Your birthday is " << bday << "\n";
  cout << 1;
  }

// End of File