#pragma once
#include <iostream>
using namespace std;
class MyString
{
public:
MyString();
MyString(int len);
MyString(const char*p);
MyString(const MyString& obj);
~MyString(void);
void print();
int getLen();
MyString& operator=(const char *p);
MyString& operator=(const MyString &obj);
char& operator[](int index) const;
bool operator==(const char* p) const;
bool operator!=(const char* p) const;
bool operator==(const MyString& obj) const;
bool operator!=(const MyString& obj) const;
bool operator<(const char* p) const;
bool operator>(const char* p) const;
bool operator<(const MyString &obj) const;
bool operator>(const MyString &obj) const;
private:
int m_len;
char *m_p;
friend ostream& operator<<(ostream &out, const MyString &obj);
friend istream& operator>>(istream &in, MyString &obj);
};
#include "MyString.h"
#include <iostream>
using namespace std;
MyString::MyString()
{
//初始化为空串
this->m_len = 0;
this->m_p = new char[m_len+1];
strcpy(m_p,"");
}
MyString::MyString(int len)
{
if (len < 0)
{
this->m_len = 0;
this->m_p = new char[m_len+1];
strcpy(m_p,"");
}
this->m_len = len;
this->m_p = new char[m_len+1];
}
MyString::MyString(const char*p)
{
if (p==NULL)
{
this->m_len = 0;
this->m_p = new char[m_len+1];
strcpy(m_p,"");
}
this->m_len = strlen(p);
this->m_p = new char[m_len+1];
strcpy(this->m_p,p);
}
MyString::MyString(const MyString& obj)
{
this->m_len = obj.m_len;
this->m_p = new char[m_len+1];
strcpy(this->m_p,obj.m_p);
}
MyString::~MyString(void)
{
if (this->m_p!=NULL)
{
delete[] this->m_p;
this->m_p = NULL;
this->m_len =0;
}
}
void MyString::print()
{
cout<<this->m_p<<endl;
}
int MyString::getLen()
{
return this->m_len;
}
MyString& MyString::operator=(const char *p)
{
if (this->m_p!=NULL)
{
delete[] this->m_p;
this->m_p = NULL;
this->m_len =0;
}
if(p==NULL)
{
this->m_len = 0;
this->m_p = new char[m_len+1];
strcpy(m_p,"");
return *this;
}
this->m_len = strlen(p);
this->m_p = new char[m_len+1];
strcpy(this->m_p,p);
return *this;
}
MyString& MyString::operator=(const MyString &obj)
{
if (this->m_p!=NULL)
{
delete[] this->m_p;
this->m_p = NULL;
this->m_len =0;
}
this->m_len = obj.m_len;
this->m_p = new char[m_len+1];
strcpy(this->m_p,obj.m_p);
return *this;
}
char& MyString::operator[](int index) const
{
return this->m_p[index];
}
bool MyString::operator==(const char* p) const
{
if (p==NULL)
{
if (m_len==0)
{
return true;
}
return false;
}
if (strcmp(this->m_p,p) == 0)
{
return true;
}
return false;
}
bool MyString::operator!=(const char* p) const
{
return !(*this==p);
}
bool MyString::operator==(const MyString& obj) const
{
if (this->m_len != obj.m_len)
{
return false;
}
if (strcmp(this->m_p,obj.m_p) != 0)
{
return false;
}
return true;
}
bool MyString::operator!=(const MyString& obj) const
{
return !(*this == obj);
}
bool MyString::operator<(const char* p) const
{
if (strcmp(this->m_p,p) < 0)
{
return true;
}
return false;
}
bool MyString::operator>(const char* p) const
{
if (strcmp(this->m_p,p) > 0)
{
return true;
}
return false;
}
bool MyString::operator<(const MyString &obj) const
{
if (strcmp(this->m_p,obj.m_p) < 0)
{
return true;
}
return false;
}
bool MyString::operator>(const MyString &obj) const
{
if (strcmp(this->m_p,obj.m_p) > 0)
{
return true;
}
return false;
}
ostream& operator<<(ostream &out, const MyString &obj)
{
out<<obj.m_p;
return out;
}
istream& operator>>(istream &in, MyString &obj)
{
in>>obj.m_p;
return in;
}
#include <iostream>
#include "MyString.h"
using namespace std;
int main()
{
MyString obj1;
MyString obj2("abcdefg");
MyString obj3 = obj2;
MyString obj4 = "abc";
MyString obj5(obj4);
MyString obj6(5);
//obj1 = obj5;
//obj1[4];
/*cout<<obj1<<endl;
cout<<obj1[4]<<endl;
obj1[4] = '3';
cout<<obj1[4]<<endl;*/
/* if (obj2 == "abcdefg123")
{
cout<<"equal"<<endl;
}
else
{
cout<<"not equal"<<endl;
}
if (obj3 == obj4)
{
cout<<"equal"<<endl;
}
else
{
cout<<"not equal"<<endl;
}
if (obj2 > "defg")
{
cout<<">"<<endl;
}
else
{
cout<<"<"<<endl;
}
if (obj2 > obj4)
{
cout<<">"<<endl;
}
else
{
cout<<"<"<<endl;
}*/
cin>>obj6;
cout<<obj6<<endl;
system("pause");
return 0;
}