#include <iostream> #include <string> #include "MyString.h" using namespace std; int main() { MyString s1("abc"); MyString s2("ddd"); cout << s1 + s2 << endl; cout << s1 << endl; cout << s2 << endl;
s2 = "abc";
if (s1 == s2)
{
cout << "相等" << endl;
}
else
{
cout << "不相等" << endl;
}
return 0; } int main01() { MyString s1("abc"); MyString s3 = "123"; MyString s2(s1); cout << s1 << endl; cout << s2 << endl; s1[1] = ‘x‘; cout << s1 << endl; s3 = s1; cout << s3 << endl; cin >> s1; cout << s1 << endl; system("pause"); return 0; }
#include "MyString.h" MyString::MyString() { this->len = 0; this->str = NULL; } MyString::MyString(const char* str) { if (str == NULL) { this->len = 0; this->str = new char[0 + 1]; strcpy(this->str, ""); } else { int len = strlen(str); this->len = len; this->str = new char[len + 1]; strcpy(this->str, str); } } //初始化时候被调用的 MyString::MyString(const MyString &another) { this->len = another.len; this->str = new char[this->len + 1]; strcpy(this->str, another.str); } MyString::~MyString() { if (this->str != NULL) { cout << this->str << "~MyString" << endl; delete this->str; this->str = NULL; this->len = 0; } } ostream& operator<<(ostream& os, MyString& s) { os << s.str; return os; } char& MyString::operator[](int index) { return this->str[index]; } MyString& MyString::operator=(const MyString& another) { if (this == &another) { return *this; } if (this->str != NULL) { delete[] this->str; this->str = NULL; this->len = 0; } this->len = another.len; this->str = new char[this->len + 1]; strcpy(this->str, another.str); return *this; } istream& operator>>(istream& is, MyString & s) { //将s字符串的空间 释放掉 if (s.str != NULL) { delete[] s.str; s.str = NULL; s.len = 0; } //创建新的 char temp_str[2048] = { 0 }; cin >> temp_str; int len = strlen(temp_str); s.str = new char[len + 1]; strcpy(s.str, temp_str); s.len = len; return is; } MyString MyString::operator+(MyString& another) { MyString temp; int len = this->len + another.len; temp.len = len; temp.str = new char[len + 1]; /* memset(temp.str, 0, len+1); strcat(temp.str, this->str); */ strcpy(temp.str, this->str); strcat(temp.str, another.str); return temp; }
bool MyString::operator==(MyString& another) { if (this->len != another.len) { return false; } for (int i = 0; i < another.len; i++) { if (this->str[i] != another.str[i]) { return false; } } return true; } //!= bool MyString::operator!=(MyString& another) { return !(*this == another); }
#pragma once #define _CRT_SECURE_NO_WARNINGS #include <iostream> using namespace std; class MyString { public: MyString(); //MyString(int len); //创建一个长度为len的string MyString(const char* str); MyString(const MyString &another); ~MyString(); //重载[] char& operator[](int index); //<< friend ostream& operator<<(ostream& os, MyString& s); //>> friend istream& operator>>(istream& is, MyString & s); //= MyString& operator=(const MyString& another); //+ MyString operator+(MyString& another); //==
bool operator==(MyString& another);
//!=
bool operator!=(MyString& another);
private: int len; char *str; };
原文:https://www.cnblogs.com/seraphgabriel/p/9417333.html