#include //for the function strlen #include //for class out_of_range #include "mystring.h" using namespace std; mystring::mystring(const char *s) //plain old constructor : n {strlen(s) + 1}, p {new char[n]} { for (int i {0}; i < n; ++i) { p[i] = s[i]; //Copy the characters into the block of memory. } } mystring::mystring(const mystring& another) //"copy" constructor : n {another.n}, p {new char[n]} { for (int i {0}; i < n; ++i) { p[i] = another.p[i]; } } mystring& mystring::operator=(const mystring& another) { if (this != &another) { delete[] p; //Destruct the old value of this object. n = another.n; p = new char[n]; for (int i {0}; i < n; ++i) { p[i] = another.p[i]; } } return *this; } char mystring::operator[](size_t i) const //member func of const mystring object { if (i >= n) { throw out_of_range("mystring subscript too big"); } return p[i]; } char& mystring::operator[](size_t i) //member func of non-const mystring object { if (i >= n) { throw out_of_range("mystring subscript too big"); } return p[i]; }