原创博文,转载请标明出处--周学伟http://www.cnblogs.com/zxouxuewei/
一,创建测试程序包
测试代码如下:
/* Date: 2017-5-4
* Description: String operator overload test program
* Filename: string_operator.h
* Author: Myron Zhou
*/ #ifndef __STRING_OPERATOR_h_
#define __STRING_OPERATOR_h_ #include <iostream>
#include <string.h> using namespace std; class MyString
{
public:
//三个重载的构造函数
MyString();
MyString(const char* str);
MyString(const MyString& str);
//析构函数
~MyString(); //重载运算符
MyString& operator = (const MyString& str);
char& operator[](int index); //访问下标
friend ostream& operator << (ostream& out, const MyString& str); //重载输出操作符
friend istream& operator >> (istream& in, MyString& str); //重载输入操作符
friend MyString operator + (const MyString& str1, const MyString& str2); //重载加号操作符,注意返回引用不行
friend MyString operator += (MyString& str1, const MyString& str2); //重载+=操作符
friend bool operator == (const MyString& str1, const MyString& str2); //重载相等操作符
friend bool operator != (const MyString& str1, const MyString& str2); //重载不相等操作符 private:
char* p;
int len;
}; #endif
/* Date: 2017-5-4
* Description: String operator overload test program
* Filename: string_operator.cpp
* Author: Myron Zhou
*/
#include "string_operator.h"
using namespace std; //默认构造函数,初始化为空串
MyString::MyString()
{
len = ;
p = new char[len + ];
p[] = '\0';
}
//构造函数,用一个字符串初始化
MyString::MyString(const char* str)
{
len = strlen(str);
p = new char[strlen(str) + ];
strncpy(p, str,len+);
}
//拷贝构造函数,用另外一个string初始化
MyString::MyString(const MyString& str)
{
len = str.len;
p = new char[strlen(str.p) + ];
strncpy(p, str.p, strlen(str.p) + );
}
//析构函数
MyString::~MyString()
{
delete[] p;
}
//重载赋值操作符( = )
MyString& MyString::operator = (const MyString& str)
{
if (this->p == str.p)
{
return *this;
}
delete[] p;
len = str.len;
p = new char[len + ];
strncpy(p, str.p, len + );
return *this;
}
//重载输出操作符( << )
ostream& operator << (ostream& out, const MyString& str)
{
out << str.p;
return out;
}
//重载输入操作符( >> )
istream& operator >> (istream& in, MyString& str)
{
in >> str.p;
return in; }
//重载加号操作符( + )
MyString operator + (const MyString& str1, const MyString& str2)
{
MyString str;
delete[] str.p;
str.len = str1.len + str2.len;
str.p = new char[str.len + ];
strncpy(str.p, str1.p, str.len + );
strncat(str.p, str2.p, str.len + );
return str;
}
//重载相加赋值操作符( += )
MyString operator += (MyString& str1, const MyString& str2)
{
str1 = str1 + str2;
return str1;
}
//重载相等操作符
bool operator == (const MyString& str1, const MyString& str2)
{
if (strcmp(str1.p, str2.p) == )
{
return true;
}
return false;
}
//重载不相等操作符
bool operator != (const MyString& str1, const MyString& str2)
{
if (strcmp(str1.p, str2.p) == )
{
return false;
}
return true;
} //重载下标([])
char& MyString::operator[](int index)
{
return p[index];
}
/* Date: 2017-5-4
* Description: String operator overload test program
* Filename: main.cpp
* Author: Myron Zhou
*/ #include "string_operator.h" int main()
{
MyString mystr("this is test program!"); //测试构造函数,用一个字符串初始化
cout << mystr[] << endl;
mystr[] = 'd';
cout << mystr <<endl;
MyString mystr2(mystr); //用另外一个string初始化
cout << mystr2 << endl;
MyString mystr3;
mystr3 = mystr + mystr2; //测试加号运算符,测试赋值运算符
cout << mystr + mystr2 << endl;
mystr3 += mystr; //测试+=运算符
cout << mystr3 << endl;
cout << (mystr == mystr2) << endl; //测试 ==
cout << (mystr != mystr3) << endl; //测试 !=
MyString mystr4;
cout << "good luck!" << endl;
cin >> mystr4; //测试重载的输入符号( >> )
cout << mystr4 << endl; return ;
}
二,编译运行代码