/* name: simplestring.h ** author: L. Liberti */ #ifndef _SIMPLESTRINGH #define _SIMPLESTRINGH #include<iostream> class SimpleString { public: // default constructor: initialise an empty string SimpleString(); // initialise a string from an array of characters SimpleString(char* initialiseFromArray); // destroy a string ~SimpleString(); // return the size (in characters) int size(void) const; // return the i-th character (overloading of [] operator) char& operator[](int i); // copy from another string void copyFrom(SimpleString& s); // append a string to this void append(SimpleString& suffix); // does this string contain needle? bool contains(SimpleString& needle) const; private: int theSize; char* buffer; int theAllocatedSize; static const int theMinimalAllocationSize = 1024; char theZeroChar; }; std::ostream& operator<<(std::ostream& out, SimpleString& s); #endifTest it with with the following main program:
/* name: ssmain.cxx author: Leo Liberti */ #include<iostream> #include "simplestring.h" int main(int argc, char** argv) { using namespace std; SimpleString s("I'm "); SimpleString t("very glad"); SimpleString u("so tired I could pop"); SimpleString v("very"); cout << s << endl; cout << t << endl; cout << u << endl << endl; SimpleString w; w.copyFrom(s); s.append(t); w.append(u); cout << s << endl; cout << w << endl << endl; if (s.contains(v)) { cout << "s contains very" << endl; } else { cout << "s does not contain very" << endl; } if (w.contains(v)) { cout << "w contains very" << endl; } else { cout << "w does not contain very" << endl; } return 0; }and check that the output is as follows.
I'm very glad so tired I could pop I'm very glad I'm so tired I could pop s contains very w does not contain very
Write also a makefile for building the object file simplestring.o and the executable ssmain.