(教学版本,main函数可以自己写)
MyString.h
#ifndef __MYSTRING_H__
#define __MYSTRING_H__
#include <iostream>
using namespace std;
class MyString
{
friend ostream & operator<<(ostream& out, const MyString& str);
friend istream & operator>>(istream& in, const MyString& str);
public:
MyString(int len=0);
MyString(const char *p);
MyString(const MyString & str);
~MyString();
public:
MyString& operator=(const char *p);
MyString& operator=(const MyString &str);
char& operator[](const int index);
bool operator==(const char *p) const;
bool operator==(const MyString& str) const;
bool operator!=(const char *p) const;
bool operator!=(const MyString& str) const;
bool operator<(const char *p) const;
bool operator<(const MyString& str) const;
bool operator>(const char *p) const;
bool operator>(const MyString& str) const;
public:
char *c_str()
{
return m_p;
}
const char *c_str2()
{
return m_p;
}
int length()
{
return m_len;
}
private:
int m_len;
char *m_p;
};
#endif
MyString.cpp
#include "MyString.h"
#include <string.h>
ostream & operator<<(ostream& out, const MyString& str)
{
out << str.m_p;
return out;
}
istream & operator>>(istream& in, const MyString& str)
{
in >> str.m_p;
return in;
}
MyString::MyString(int len)
{
if (len == 0)
{
m_len = 0;
m_p = new char[m_len + 1];
strcpy(m_p, "");
}
else
{
m_len = len;
m_p = new char[m_len + 1];
memset(m_p, 0, m_len+1);
}
}
MyString::MyString(const char *p)
{
if (p == NULL)
{
m_len = 0;
m_p = new char[m_len + 1];
strcpy(m_p, "");
}
else
{
m_len = strlen(p);
m_p = new char[m_len + 1];
strcpy(m_p, p);
}
}
MyString::MyString(const MyString &str)
{
m_len = str.m_len;
m_p = new char[m_len + 1];
strcpy(m_p, str.m_p);
}
MyString::~MyString()
{
if (m_p != NULL)
{
delete [] m_p;
m_p = NULL;
m_len = 0;
}
}
MyString& MyString::operator=(const char *p)
{
if (m_p != NULL)
{
delete [] m_p;
}
if (p == NULL)
{
m_len = 0;
m_p = new char[m_len + 1];
strcpy(m_p, "");
}
else
{
m_len = strlen(p);
m_p = new char[m_len + 1];
strcpy(m_p, p);
}
return *this;
}
MyString& MyString::operator=(const MyString &str)
{
if (m_p != NULL)
{
delete [] m_p;
}
m_len = str.m_len;
m_p = new char[m_len + 1];
strcpy(m_p, str.m_p);
return *this;
}
char& MyString::operator[](const int index)
{
return m_p[index];
}
bool MyString::operator==(const char *p) const
{
if (p == NULL)
{
if (m_len == 0)
return true;
else
return false;
}
else
{
return !strcmp(m_p, p);
}
}
bool MyString::operator==(const MyString& str) const
{
return !strcmp(m_p, str.m_p);
}
bool MyString::operator!=(const char *p) const
{
return !(*this == p);
}
bool MyString::operator!=(const MyString& str) const
{
return !(*this == str);
}
bool MyString::operator<(const char *p) const
{
return (strcmp(m_p, p) < 0);
}
bool MyString::operator<(const MyString& str) const
{
return (strcmp(m_p, str.m_p) < 0);
}
bool MyString::operator>(const char *p) const
{
return (strcmp(m_p, p) > 0);;
}
bool MyString::operator>(const MyString& str) const
{
return (strcmp(m_p, str.m_p) > 0);
}
Main.cpp
int main1()
{
MyString s1;
MyString s2("s2");
MyString s2_2 = NULL;
MyString s3 = s2;
MyString s4 = "s42323233";
s4 = s2;
s4 = "headad";
cout << s4[2] << endl;
cout << s4 << endl;
return 0;
}
int main2()
{
MyString s1;
MyString s2("s2");
MyString s3("s223123");
if (s2 == NULL)
{
cout << "相等" << endl;
}
else
{
cout << "不相等" << endl;
}
if (s2 == s3)
{
cout << "相等" << endl;
}
else
{
cout << "不相等" << endl;
}
return 0;
}
int main3()
{
MyString s1;
MyString s2("s2");
MyString s3("s3");
if (s3 < "bbbb")
{
cout << "s3 < bbbb" << endl;
}
else
{
cout << "s3 > bbbb" << endl;
}
if (s3 > s2 )
{
cout << "s3 > s2" << endl;
}
else
{
cout << "s3 < s2" << endl;
}
return 0;
}
int main()
{
MyString s1(128);
cin >> s1;
cout << s1 << endl;
return 0;
}
我的版本(较粗糙)
String.h
#ifndef _STRING_H_
#define _STRING_H_
#include <iostream>
using namespace std;
class String
{
private:
char *m_data;
int m_len;
public:
String();
String(const String &s);
String(char *data);
String(int n,char a);
~String();
//void print();
friend ostream &operator <<(ostream &out,const String &s);
friend istream &operator >>(istream &in, String &s);
String &operator =(const String &s);
String &operator =( char *s);
String &operator +=(const String &s);
char &operator [](int index);
bool operator ==(const String &s);
};
#endif
String.cpp
#include <iostream>
#include <string.h>
#include "String.h"
using namespace std;
String ::String()
{
m_data = new char[1];
//m_data[0] = '0';
m_len = 1;
}
String ::String(char *data)
{
m_len = strlen(data);
m_data = new char[m_len +1];
strcpy(m_data,data);
}
String ::String(int n,char a)
{
int i;
char b[n];
m_len = n;
m_data = new char[m_len + 1];
for(i=0;i<n;i++)
{
b[i] = a;
}
for(i=0;i<n;i++)
{
m_data[i] = b[i];
}
m_data[n] = '\0';
}
String ::String(const String &s)
{
m_len = s.m_len;
m_data = new char[m_len + 1];
strcpy(m_data,s.m_data);
}
/*void String::print()
{
cout << m_data << endl;
cout << m_len << endl;
}*/
String::~String()
{
if(m_data != NULL)
{
delete[] m_data;
m_data = NULL;
m_len = 0;
}
}
ostream &operator <<(ostream &out,const String &s)
{
out << s.m_data;
return out;
}
istream &operator >>(istream &in,String &s)
{
char tmp[1024] = {0};
in >> tmp;
if(s.m_data != NULL)
{
delete s.m_data;
}
s.m_len = strlen(tmp);
s.m_data = new char[s.m_len +1];
strcpy(s.m_data,tmp);
return in;
}
String &String::operator =(const String &s)
{
if(*this == s)
{
return *this;
}
m_len = s.m_len;
if(m_data != NULL)
{
delete m_data;
}
m_data = new char[m_len + 1];
strcpy(m_data,s.m_data);
}
String &String::operator =( char *s)
{
if(*this == s)
{
return *this;
}
m_len = strlen(s);
if(m_data != NULL)
{
delete m_data;
}
m_data = new char[m_len +1];
strcpy(m_data,s);
}
bool String::operator ==(const String &s)
{
if(this->m_data == s.m_data)
{
return true;
}
else
{
return false;
}
}
String & String::operator +=(const String &s)
{
m_len = m_len + s.m_len;
char *tmp = new char[m_len + 1];
strcpy(tmp,m_data);
strcat(tmp,s.m_data);
delete m_data;
m_data = tmp;
return *this;
}
char &String::operator [](int index)
{
return m_data[index];
}
main.cpp
#include <iostream>
#include "String.h"
using namespace std;
int main()
{
String s3(10,'b');
String s2("aba");
String s1;
String s4(s2);
String s5;
String s6;
cout << s1 << endl;
cout << s2 << endl;
cout << s3 << endl;
cout << s4 << endl;
cin >> s1;
cout << s1 << endl;
cout << (s2 += s3) << endl;
cout << s2[1] << endl;
s5 = s3;
s6 = "hello";
cout << s5 << endl;
cout << s6 << endl;
return 0;
}