Solution

Here follows the header file.

/*******************************************************
** Name:        timestamp.h
** Author:      Leo Liberti
** Source:      GNU C++
** Purpose:     www exploring topologizer: TimeStamp class header
** History:     060820 work started
*******************************************************/

#ifndef _WETTIMESTAMPH
#define _WETTIMESTAMPH

#include<iostream>
#include<sys/time.h>

class TimeStampException {
 public:
  TimeStampException();
  ~TimeStampException();
};

class TimeStamp {

 public:

  TimeStamp() : timestamp(0) { } ;
  ~TimeStamp() { }

  long get(void) const;
  void set(long theTimeStamp);

  // read the system clock and update the timestamp
  void update(void) throw (TimeStampException);

 private:

  long timestamp;
  
};

// print the timestamp in humanly readable form
std::ostream& operator<<(std::ostream& s, TimeStamp& t) 
     throw (TimeStampException);

#endif
The following points are worth emphasizing.
  1. All header file code is structured as follows:
    % latex2html id marker 2457
\fbox{\begin{minipage}{8cm}{\tt
\char93 ifndef \_I...
...ar93 define \_INCLUDEFILENAMEH \\ \
[...] \\
\char93 endif
}\end{minipage}}
    The purpose of this is to avoid that the contents of a header file should be compiled more than once even though the header file itself is referenced more than once (this may happen should a .cxx file reference two header files $ A,B$ where $ A$ also references $ B$ as its own header -- this situation occurs quite often in practice).
  2. All exception classes referred to in the main class being declared in the header should be declared first.
  3. A function can be declared const (i.e. const is appended at the end of the declaration) as a promises not to change its owner object. This is a good way to catch some bad bugs, as the compiler signals an error if a const function has some code that might potentially change the *this object.
  4. Notice that class and namespace closing braces are followed by a colon (';').
  5. The overloaded operator« function is declared outside the class (it takes an object of the class as an argument)

Here follows the implementation file.

/*******************************************************
** Name:        timestamp.cxx
** Author:      Leo Liberti
** Source:      GNU C++
** Purpose:     www exploring topologizer: TimeStamp class 
** History:     060820 work started
*******************************************************/

#include <iostream>
#include <ctime>
#include "timestamp.h"

TimeStampException::TimeStampException() { }
TimeStampException::~TimeStampException() { }

long TimeStamp::get(void) const {
  return timestamp;
}

void TimeStamp::set(long theTimeStamp) {
  timestamp = theTimeStamp;
}

void TimeStamp::update(void) throw (TimeStampException) {
  struct timeval tv;
  struct timezone tz;
  using namespace std;
  try {
    int retVal = gettimeofday(&tv, &tz);
    if (retVal == -1) {
      cerr << "TimeStamp::updateTimeStamp(): couldn't get system time" << endl;
      throw TimeStampException();
    }
  } catch (...) {
    cerr << "TimeStamp::updateTimeStamp(): couldn't get system time" << endl;
    throw TimeStampException();
  }
  timestamp = tv.tv_sec;
}

std::ostream& operator<<(std::ostream& s, TimeStamp& t) 
  throw (TimeStampException) {
  using namespace std;
  time_t theTime = (time_t) t.get();
  char* buffer;
  try {
    buffer = ctime(&theTime);
  } catch (...) {
    cerr << "TimeStamp::updateTimeStamp(): couldn't print system time" << endl;
    throw TimeStampException();
  }
  buffer[strlen(buffer) - 1] = '\0';
  s << buffer;
  return s;
}
  1. Include files are usually included as follows: first the standard includes, then the user-defined ones. It helps to differentiate them by employing the two (equivalent) forms: \fbox{\tt
\char93 include<file.h>} and \fbox{\tt \char93 include ''file.h''} for standard and user-defined respectively.
  2. Methods in an implementation file must be defined using the scope operator ::
    \fbox{{\it returnType ClassName}{\tt ::}{\it
MethodName}{\tt (}{\it args}{\tt ) \{}{\it definition}{\tt \}}}
  3. The overloaded operator « is outside the class definition, so there is no prepended scoping operator.

Leo Liberti 2008-01-12