CSCI 330 Lab: String

Implement a class called my_string that provides the string functionality needed by the test code given below. When you do this, you should not use the C++ string class.

The my_string class you create should be implemented in two files: my_string.h and my_string.cpp. The header file my_string.h should contain the class declaration; the file my_string.cpp should contain implementations of the my_string functions.

// test_my_string.cpp

#include <iostream>
#include <string>
#include <cassert>
#include "my_string.h"

using namespace std;

//#define STRING string
#define STRING my_string

int main()
{
   STRING s1; // s1 == ""
   assert(s1.length() == 0);

   STRING s2("hi");  // s2 == "hi"
   assert(s2.length() == 2);

   STRING s3(s2);  // s3 == "hi"
   assert(s3.length() == 2);
   assert(s3[0] == 'h');
   assert(s3[1] == 'i');

   s1 = s2;  // s1 == "hi"
   assert(s1.length() == 2);
   assert(s1[0] == 'h');
   assert(s1[1] == 'i');

   s3 = "bye";  // s3 == "bye"
   assert(s3.length() == 3);
   assert(s3[0] == 'b');
   assert(s3[1] == 'y');
   assert(s3[2] == 'e');
   
   s1 += "re";  // s1 == "hire"
   assert(s1.length() == 4);
   assert(s1[0] == 'h');
   assert(s1[1] == 'i');
   assert(s1[2] == 'r');
   assert(s1[3] == 'e');

   cout << "SUCCESS" << endl;
}