Input/output utilities

Header file classio.h

C++ is convenient in that the input/output operators can be overloaded so that reading and writing objects of any class can look the same. For example, the C++ code to read a list of objects from standard input might look like

double a,b;
int c;
point d; // where `point' might be a user-defined class
cin >> a >> b >> c >> d;

The down side is that the definition of the input and output methods is somewhat cryptic, for example

// define a class consisting of a pair of coordinates defining a point
class point
{
   double x, y; // x and y coordinates

   // method to read from an input stream
   friend istream& operator>>(istream& stream, point& a)
   {
      stream >> a.x >> a.y;
      return stream;
   }

   // method to write to an output stream
   friend ostream& operator<<(ostream& stream, point& a)
   {
      stream << a.x << ' ' << a.y;
      return stream;
   }
};

Class `Classio' provides an easier framework for input and output. It is intended to be inherited by any user-defined class. These classes can then use a simpler form of input and output method (member methods `Read and Write') which `Classio' calls with the cryptic friend method as above.

Here is a skeleton for a class inheriting from `classio':

#include <classio.h>
...
class MY_CLASS_NAME: public Classio
{
   TYPE1 NAME1;
   ...

   void Read(istream& stream)
   {
      stream >> NAME1 >> ... ;

   void Write(ostream& stream) const // N.B. the `const'
   {
      stream << NAME1 << ... ;
   }
};
e.g. for the previous example:
#include <classio.h>
class point: public Classio
{
   double x, y;

   void Read(istream& stream)
   { stream >> x >> y; }
   void Write(ostream& stream) const
   { stream << x << ' ' << y; }
};