Thursday, November 15, 2012

C++ classes: Simple String

____________________________________

First: Header file.h (functions prototype).

____________________________________ 



// File simpleString.h
// Simple string class definition

#ifndef SIMPLESTRING_H
#define SIMPLESTRING_H

class simpleString
{
    public:
      // Member Functions
      // constructors

      simpleString();
      simpleString(int );

      // Destructor
      ~simpleString();

      // Function Prototype definition
      // Read a simple string

      void readString();
 
      // Display a simple string
      void writeString() const;
 
      // Retrieve the character at a specified position
      // Returns the character \0 if position is out of bounds

      char at(int) const;

      // Return the string length
      int getLength() const;
 
      // Return the string capacity
      int getCapacity() const;

      void getContents(char[]) const;

    private:
      // Data members (attributes)
      // maximum size

      int capacity;
      // pointer to storage array
      char *s;
      // current length
      int length;
};
 
#endif //SIMPLESTRING_H

________________________________________________ 

second: Implementation file.cpp 

_____________________________________


// File simplestring.cpp
// Simple string class implementation

#include "simplestring.h"
#include <iostream>
using namespace std;

// Member Functions...
// constructors
// default constructor, capacity = 255
simpleString::simpleString()
{
    s = new char[255];    
    capacity = 255; length = 0;
}

// Constructor with argument, capacity is mVal
simpleString::simpleString(int mVal)
{
    s = new char[mVal];    
    capacity = mVal; length = 0;
}

// Class Destructor
simpleString::~simpleString()
{ delete [] s;}
   

// Read a simple string
void simpleString::readString()
{
   // Local data...
   char next;             
   int pos = 0;           

   cin.get(next);        
   while ((next != '\n') && (pos < capacity))
   {
      // Insert next in array contents
      s[pos] = next;
      pos++;
      cin.get(next);    
   }

   length = pos;
}

// Write a simple string
void simpleString::writeString() const
{
   for (int pos = 0; pos < length; pos++)
      cout << s[pos];
}

// Retrieve the character at a specified position
// Returns the character \0 if position is out
// of bounds
char simpleString::at(int pos) const
{
   // Local data
   const char nullcharacter = '\0';
   if ((pos < 0) || (pos >= length))
    {
        cerr << "position " <<    pos << " not defined." << endl;
        return nullcharacter;
    }
   else  return s[pos];
}

// Return the string length
int simpleString::getLength() const
{
   return length;
}

// Return the string capacity
int simpleString::getCapacity() const
{
   return capacity;
}
void simpleString::getContents(char str[]) const
{
   for (int i = 0; i < length; i++)
      str[i] = s[i];
}

________________________________________________ 

finally class test

_____________________________________

// File: simpleStringTest.cpp
// Tests the simple string class

#include "simpleString.h"
#include "simpleString.cpp"
#include <iostream>

using namespace std;

int main()
{
    simpleString S1;
    simpleString S2(20);

    cout << S1.getCapacity() <<" "<<S1.getLength() << endl;
    cout << S2.getCapacity() <<" "<<S2.getLength() << endl;

    // Read in a string.
    cout << "Enter a string and press RETURN: ";
    S1.readString();

   // Display the string just read.
   cout << "The string read was: ";
   S1.writeString();
   cout << endl;

   // Display each character on a separate line.
   cout << "The characters in the string follow:" << endl;
   for (int pos = 0; pos < S1.getLength(); pos++)
       cout << S1.at(pos) << endl;

  
   return 0;
}


No comments:

Post a Comment