今天这个题真的是把我整吐了,写的时间挺长的,下面就把自己的一些心得和收获写在下面了:
#include <cstdlib>
#include <iostream>
using namespace std;
int strlen(const char * s)
{ int i = 0;
for(; s[i]; ++i);
return i;
}
void strcpy(char * d,const char * s)
{
int i = 0;
for( i = 0; s[i]; ++i)
d[i] = s[i];
d[i] = 0;
}
int strcmp(const char * s1,const char * s2)
{
for(int i = 0; s1[i] && s2[i] ; ++i) {
if( s1[i] < s2[i] )
return -1;
else if( s1[i] > s2[i])
return 1;
}
return 0;
}
void strcat(char * d,const char * s)
{
int len = strlen(d);
strcpy(d+len,s);
}
class MyString
{
public:
MyString(const char* s) {
if (s == NULL) return;
p = new char[strlen(s) + 1];
strcpy(p, s);
//p[strlen(s)] = '\0';
}
MyString() { p = NULL; };
MyString(const MyString& S)//注意用const养成一个良好的习惯
{
if ( S.p==NULL) return;
else{
//if (p) delete[] p;
p = new char[strlen(S.p) + 1];
strcpy(p, S.p);
}
}
~MyString() { if (p) delete[] p; }
//重载<<
friend ostream& operator<<(ostream& out,const MyString str) {
if(str.p!=NULL)
out << str.p;
return out;
}
//重载类之间的相加
MyString operator+(const MyString& str) {
//注意这里返回的是一个临时的对象,因此不需要返回值是引用
if (str.p == NULL) return*this;
MyString rhs;
rhs.p = new char[strlen(this->p) + strlen(str.p) + 1];
strcpy(rhs.p, p);
strcat(rhs.p, str.p);
return rhs;
}
//重载 char*+类 类型
friend MyString operator + (const char* s, const MyString& str)
//同样注意返回类型
{
MyString rhs(s);
return rhs+str;
}
//重载 类+char* 类型
MyString operator+(const char* s) {
//同样注意返回类型
MyString str(s);
return *this+str;
}
//重载+=
MyString& operator+=(const char* s) {
MyString str1(s);
*this = *this + str1;
return *this;
}
//重载"()"
char* operator () (int a, int b) {
char* s = new char[b + 1];
for (int i = a, j = 0; j < b; i++, j++) {
s[j] = p[i];
}
s[b] = '\0';
return s;
}
//重载类之间的<
bool operator < (const MyString& str2) {
if (strcmp(p, str2.p) < 0)
return true;
else return false;
}
//重载类之间的>
bool operator > (const MyString& str2) {
if (strcmp(p, str2.p) > 0)
return true;
else return false;
}
//重载类之间的==
bool operator == (const MyString& str2) {
if (strcmp(p, str2.p) == 0)
return true;
else return false;
}
//重载两个类之间的=
MyString& operator=(const MyString& str) {
if (str.p == p) return*this;
else {
if (p) delete[] p;//这个判断很重要
p = new char[strlen(str.p) + 1];
strcpy(p, str.p);
}
return *this;
}
//重载 类=char*类型 的等号(其实这个重载可以不写的)
MyString& operator=(char* s) {
if (p) delete[] p;
p = new char[strlen(s) + 1];
strcpy(p, s);
return *this;
}
//重载 "[]"
char& operator [](int i) {
return p[i];
}
private:
char* p = NULL;
};
int CompareString( const void * e1, const void * e2)
{
MyString * s1 = (MyString * ) e1;
MyString * s2 = (MyString * ) e2;
if( * s1 < *s2 )
return -1;
else if( *s1 == *s2)
return 0;
else if( *s1 > *s2 )
return 1;
}
int main()
{
MyString s1("abcd-"),s2,s3("efgh-"),s4(s1);
MyString SArray[4] = {"big","me","about","take"};
cout << "1. " << s1 << s2 << s3<< s4<< endl;
s4 = s3;
s3 = s1 + s3;
cout << "2. " << s1 << endl;
cout << "3. " << s2 << endl;
cout << "4. " << s3 << endl;
cout << "5. " << s4 << endl;
cout << "6. " << s1[2] << endl;
s2 = s1;
s1 = "ijkl-";
s1[2] = 'A' ;
cout << "7. " << s2 << endl;
cout << "8. " << s1 << endl;
s1 += "mnop";
cout << "9. " << s1 << endl;
s4 = "qrst-" + s2;
cout << "10. " << s4 << endl;
s1 = s2 + s4 + " uvw " + "xyz";
cout << "11. " << s1 << endl;
qsort(SArray,4,sizeof(MyString),CompareString);
for( int i = 0;i < 4;i ++ )
cout << SArray[i] << endl;
//s1的从下标0开始长度为4的子串
cout << s1(0,4) << endl;
//s1的从下标5开始长度为10的子串
cout << s1(5,10) << endl;
return 0;
}
这个题目到这里就结束了,我自己在写这个题的时候吸取了一些教训,希望对于大家有用:
- 在重载函数中新创建的对象不要用引用,因为他只在这里起作用。
- 在写重载类是注意用const
- 注意适当的判断和delete