模块化设计:
头文件:
<span style="font-size:18px;">
#ifndef operator_operator_h
#define operator_operator_h
#include <iostream>
#include <string>
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;</span>
功能函数:
<span style="font-size:18px;">
#include "operator.h"
//默认构造函数,初始化为空串
MyString::MyString()
{
len = 0;
p = new char[len + 1];
p[0] = '