myString.h
#pragma once
#include <iostream>
using namespace std;
class myString
{
public:
myString();
myString(int len);
myString(const myString& Str);
myString(const char* str);
~myString();
//char& getString();//多余
int getLen();
myString& operator+(myString& str);
myString& operator=(myString& str);
char& operator[](int index);
bool operator==(const myString& str)const;//const修饰this指针
bool operator!=(const myString& str)const;
bool operator>(const myString& str)const;
bool operator<(const myString& str)const;
private:
char* m_p;
int m_len;
friend istream& operator>>(istream& in, myString& str);
friend ostream& operator<<(ostream& out, myString& str);
};
myString.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "myString.h"
myString& myString::operator=(myString& str)
{
if (this->m_p != NULL)
{
delete[] m_p;
m_p = NULL;
m_len = 0;
}
this->m_len = str.m_len;
m_p = new char[this->m_len + 1];
for (int i = 0; i < m_len; i++)
{
this->m_p[i] = str.m_p[i];
}
return *this;
}
char& myString::operator[](int index)
{
return m_p[index];
}
bool myString::operator==(const myString& str) const
{
if (this->m_len != str.m_len)
return false;
else
{
for (int i = 0; i < m_len; i++)
{
if (this->m_p[i] != str.m_p[i])
return false;
}
return true;
}
}
bool myString::operator!=(const myString& str) const
{
return !(*this == str);
}
bool myString::operator>(const myString& str) const
{
if (strcmp(this->m_p, str.m_p) > 0)
return true;
else
{
return false;
}
}
bool myString::operator<(const myString& str) const
{
if (strcmp(this->m_p, str.m_p) < 0)
return true;
else
{
return false;
}
}
istream& operator>>(istream& in, myString& str)
{
char buf[1024] = { 0 };
char* tmp = buf;
cout << "请输入1个字符串:";
in >> tmp;
str.m_len = strlen(tmp);
str.m_p = new char[str.m_len + 1];
strcpy(str.m_p, buf);
return in;
}
ostream& operator<<(ostream& out, myString& str)
{
for (int i = 0; i < str.m_len; i++)
{
out << str.m_p[i];
}
out << endl;
// TODO: 在此处插入 return 语句
return out;
}
myString::myString()
{
this->m_len = 0;
this->m_p = NULL;
}
myString::myString(int len)
{
this->m_len = len;
m_p = new char[m_len + 1];
}
myString::myString(const myString& Str)
{
this->m_len = Str.m_len;
m_p = new char[m_len + 1];
strcpy(m_p, Str.m_p);
}
myString::myString(const char* str)
{
this->m_len = strlen(str);
this->m_p = new char[this->m_len + 1];
for (int i = 0; i < m_len; i++)
{
this->m_p[i] = str[i];
}
}
myString::~myString()
{
if (this->m_p != NULL)
{
delete[] m_p;
m_p = NULL;
m_len = 0;
}
}
int myString::getLen()
{
return this->m_len;
}
myString& myString::operator+(myString& str)
{
//int len = m_len + str.m_len;
strcat(this->m_p, str.m_p);
return *this;
}
myStringMain.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include "myString.h"
using namespace std;
int main()
{
myString s1(5), s2(5);
cin >> s1;
cout << s1 << endl;
cin >> s2;
cout << s2 << endl;
myString s3(10);
cout << s1[2] << endl;
if (s1 == s2)
cout << "===" << endl;
if (s1 > s2)
cout << "s1 > s2" << endl;
if (s1 < s2)
cout << "s1 < s2" << endl;
return 0;
}