#include "DynStr.h" #include /* Private */ void DynStr::free() { delete[] str; } void DynStr::copyFrom(const DynStr& other) { length = other.length; str = new char[length]; for (int i = 0; i < length; i++) str[i] = other.str[i]; } /* Public */ DynStr::DynStr() : DynStr("") {} DynStr::DynStr(const char* str) { int strLen = strlen(str); strLen++; // Make space for \0 this->str = new char[strLen]; strcpy(this->str, str); length = strLen; } DynStr::DynStr(const DynStr& copy) { copyFrom(copy); } DynStr& DynStr::operator=(const DynStr& other) { if (this != &other) { free(); copyFrom(other); } return *this; } DynStr::~DynStr() { free(); } const char* DynStr::get_str() const { return str; } int DynStr::compare(const char* other) const { return strcmp(str, other); }