#include <iostream>
#include <string>
#include <vector>
using namespace std;
class CString
{
public:
CString(const char* str = NULL);
CString(const CString &other);
~CString();
CString & operator = (const CString &other);
public:
char *m_data;
};
CString::~CString()
{
delete []m_data;
}
CString::CString(const char*str)
{
if (str == NULL)
{
m_data = new char[1];
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length + 1];
strcpy(m_data, str);
}
}
CString::CString(const CString &other)
{
cout<<"Copy Constructor is defined by user!"<<endl;
int length = strlen(other.m_data);
m_data = new char[length + 1];
strcpy(m_data, other.m_data);
}
CString &CString::operator =(const CString &other)
{
if (this == &other)//s1 = s1, then this will EQUAL to address of other
{
return *this;
}
//free all memory, then distribute new memory
delete []m_data;
int length = strlen(other.m_data);
m_data = new char[length + 1];
strcpy(m_data, other.m_data);
return *this;
}
int main()
{
CString str("hello");
CString s1("hello");
CString s2;
s1 = s1;
s2 = str;
cout<<str.m_data<<" "<<s2.m_data<<endl;
vector<char> myVec;
char ch;
while( (ch = getchar()) != EOF)
{
myVec.push_back(ch);
}
vector<char>::iterator itr = myVec.begin();
while (itr != myVec.end())
{
cout << *itr++ << endl;
}
return 0;
}