string類中運算子過載的實現
阿新 • • 發佈:2019-02-14
#include<iostream>
using namespace std;
class MyString
{
public:
MyString();
MyString(const int number);
MyString(const char *ptr);
MyString(const MyString &str);
~MyString();
MyString& operator =(const MyString &str);
char& operator [](const int index);
bool operator ==(const MyString& str);
bool operator !=(const MyString& str);
bool operator >(const MyString& str);
bool operator <(const MyString& str);
MyString operator +(const MyString &str);
friend ostream& operator <<(ostream &out, const MyString &str);
friend istream& operator >>(istream &in, MyString &str);
private:
int len;
char *pstr;
};
MyString::MyString()
{
len = 0;
pstr = new char[len + 1];
strcpy(pstr, "");
cout << "預設建構函式" << endl;
}
MyString::MyString(const int length)
{
if (length == 0)
{
len = 0 ;
pstr = new char[len + 1];
strcpy(pstr, "");
}
else
{
len = length;
pstr = new char[len + 1];
memset(pstr, 0, len);
}
}
MyString::MyString(const char *ptr)
{
if (ptr == nullptr)
{
len = 0;
pstr = new char[len + 1];
strcpy(pstr, "");
}
else
{
len = strlen(ptr);
pstr = new char[len + 1];
strcpy(pstr, ptr);
}
}
//拷貝建構函式
MyString::MyString(const MyString &str)
{
len = strlen(str.pstr);
pstr = new char[len + 1];
strcpy(pstr, str.pstr);
cout << "拷貝建構函式" << endl;
}
MyString::~MyString()
{
delete [] pstr;
pstr = nullptr;
len = 0;
cout << "destructor" << endl;
}
MyString& MyString::operator=(const MyString &str)
{
if (this == &str)
return *this;
if (pstr != nullptr)
delete[] pstr;
len = strlen(str.pstr);
pstr = new char[len + 1];
strcpy(pstr, str.pstr);
return *this;
}
char& MyString::operator[](const int index)
{
return *(pstr + index);
}
bool MyString::operator==(const MyString &str)
{
if (strcmp(pstr, str.pstr) == 0)
return true;
else
return false;
}
bool MyString::operator!=(const MyString &str)
{
if (strcmp(pstr, str.pstr) != 0)
return true;
else
false;
}
bool MyString::operator<(const MyString &str)
{
if (strcmp(pstr, str.pstr) < 0)
return true;
else
return false;
}
bool MyString::operator>(const MyString &str)
{
if (strcmp(pstr, str.pstr) > 0)
return true;
else
false;
}
MyString MyString::operator +(const MyString& str)
{
MyString temp;
// len = len + str.len;
temp.pstr = new char[strlen(pstr) + strlen(str.pstr) + 1];
sprintf(temp.pstr, "%s%s", pstr, str.pstr);
temp.len = strlen(temp.pstr);
return temp;
}
ostream& operator<<(ostream &out, const MyString &str)
{
cout << str.pstr;
return out;
}
istream& operator>>(istream &in, MyString &str)
{
str.pstr = new char[256];
cin >> str.pstr;
str.len = strlen(str.pstr);
return in;
}
void main()
{
MyString str1("abc");
cout << "str1經過初始化之後值為:" << str1 << endl;
MyString str2;
str2 = str1;
cout << "str2經過str1初始化之後值為:" << str2 << endl;
MyString str3;
str3 = str1 + str2;
cout << "str1與str2之和str3為:" << str3 << endl;
cout << "請輸入字串str4:" << endl;
MyString str4;
cin >> str4;
cout << "你輸入的str4為:" << str4 << endl;
if (str3 != str4)
{
cout << "str3不等於str4" << endl;
}
if (str3>str4)
{
cout << str3 << "大於" << str4 << endl;
}
if (str3<str4)
{
cout << str3 << "小於" << str4 << endl;
}
system("pause");
}