next up previous
Next: Implementation file Up: Classes Previous: Classes

Interface file

Consider the following class interface from your text:


#ifndef _MY_STRING_H
#define _MY_STRING_H
#include <iostream.h>

class StringIndexOutOfBounds{ };

class string
{
public:
  string(const char *cstring = "");  // Constructor.
  string(const string & str);        // Copy constructor.
  ~string() {delete [] buffer;}      // Destructor.

  const string & operator=(const string & rhs);  // Copy.
  const string & operator+=(const string & rhs); // Append.

  const char *c_str() const {return buffer;} // Return c-style string.
  int length () const {return strLength;}    // Return string length.

  char operator[](int k) const; // Accessor operator [].
  char & operator[](int k);     // Mutator operator [].

  enum {MAX_LENGTH = 1024};     // Maximum length for input string.

private:
  char *buffer;        // Storage for characters.
  int strLength;       // Length of string in characters.
  int bufferLength;    // Capacity of buffer.
};

ostream & operator<<( ostream& out, const string& str); // Output
istream & operator>>( istream& in, const string& str);  // Input
istream & getline( istream& in, const string& str);     // Read line
bool operator==(const string& lhs, const string& rhs);  // Compare ==
bool operator!=(const string& lhs, const string& rhs);  // Compare !=
bool operator< (const string& lhs, const string& rhs);  // Compare <
bool operator<=(const string& lhs, const string& rhs);  // Compare <=
bool operator> (const string& lhs, const string& rhs);  // Compare >
bool operator>=(const string& lhs, const string& rhs);  // Compare >=
#endif

This information defines both the storage we will use to manage the new string class and the operations we will allow on string objects. It would typically be stored in an interface file such as "mystring.h". A complete definition of this class can be found in Appendix B of your text.

Looking at the components of the file, we have

We have two functions (c_str() and length) completely defined in the interface file. These will be implemented as inline functions and will not result in function calls being generated. Alternately, we could write:


const char *c_str() const;
in the class definition, with:

inline const char *string::c_str() const
{
  return buffer;
}
added to the end of the file "mystring.h".

We have yet to define the remaining routines. For data hiding and to preserve uniqueness of link symbols, they would typically be placed in a separate file such as "mystring.cpp". The complete definition of this class is contained in Appendix B; however, we will take a quick look at some of the salient points of the definition.


next up previous
Next: Implementation file Up: Classes Previous: Classes
Eric Breimer
9/13/1999