#include "MyString.h"
MyString::MyString()
{
this->m_len = 0;
this->m_space = new char[m_len + 1];
strcpy(this->m_space, "");
}
MyString::MyString(const char* p)
{
if (p == NULL)
{
this->m_len = 0;
this->m_space = new char[m_len + 1];
strcpy(this->m_space, "");
}
else
{
this->m_len = strlen(p);
this->m_space = new char[m_len + 1];
strcpy(this->m_space, p);
}
}
MyString::MyString(const MyString& obj)
{
this->m_len = obj.m_len;
this->m_space = new char[m_len + 1];
strcpy(this->m_space, obj.m_space);
}
MyString::~MyString()
{
if (this->m_space != NULL)
{
delete[] this->m_space;
this->m_space = NULL;
this->m_len = 0;
}
}
MyString& MyString::operator=(MyString& s)
{
if (this->m_space != NULL)
{
delete[] this->m_space;
this->m_len = 0;
}
this->m_len = s.m_len;
this->m_space = new char[m_len];
strcpy(this->m_space, s.m_space);
return *this;
}
MyString& MyString::operator=(const char* p)
{
if (this->m_space != NULL)
{
delete[] this->m_space;
this->m_len = 0;
}
if (p == NULL)
{
this->m_len = 0;
this->m_space = new char[m_len + 1];
strcpy(this->m_space, "");
}
else
{
this->m_len = strlen(p);
this->m_space = new char[m_len + 1];
strcpy(this->m_space, p);
}
return *this;
}
char& MyString::operator[](int index)
{
return this->m_space[index];
}
bool MyString::operator==(MyString& s)
{
if (this->m_len != s.m_len)
{
return false;
}
if (strcmp(this->m_space, s.m_space) == 0)
{
return true;
}
else
{
return false;
}
}
bool MyString::operator==(const char* p)
{
if (strcmp(this->m_space, p) == 0)
{
return true;
}
else
{
return false;
}
}
bool MyString::operator!=(MyString& s)
{
if (!(*this == s))
{
return true;
}
else
{
return false;
}
}
bool MyString::operator!=(const char* p)
{
if (!(*this == p))
{
return true;
}
else
{
return false;
}
}
|