Mystring.h
#include <iostream>
#ifndef __MYSTRING_H
#define __MYSTRING_H
class Mystring{
friend void print(Mystring & oop);
public:
/*空串 “”*/
Mystring();
Mystring(char *str);
Mystring(Mystring &oop);
~Mystring();
Mystring& operator=(Mystring &oop);
public:/*[]操作符重载*/
char& operator[](int index);
public:/*==,!=操作符重载*/
bool operator==(Mystring &oop);
bool operator==(const Mystring &oop);
bool operator!=(const Mystring &oop);
bool operator!=(Mystring &oop);
public:/*括号操作符重载*/
char* Mystring::operator()(const char* str);
private:
int str_len;
char *str;
};
void print(Mystring & oop);
#endif
Mystring.cpp
#define _CRT_SECURE_NO_WARNINGS
#include "Mystring.h"
#include <iostream>
#include <cstring>
Mystring::Mystring()
{
str_len = 0;
this->str = new char[this->str_len + 1];
strcpy(this->str, "");
}
Mystring::Mystring(char *str)
{
if (str == NULL)
{
str_len = 0;
this->str = new char[this->str_len + 1];
strcpy(this->str, "");
}
else
{
this->str_len = strlen(str);
this->str = new char[str_len+1];
strcpy(this->str,str);
}
}
/*拷贝构造函数*/
Mystring::Mystring(Mystring & oop)
{
this->str_len = oop.str_len;
this->str = new char[str_len + 1];
strcpy(this->str,oop.str);
}
/*析构函数*/
Mystring::~Mystring()
{
if (this->str != NULL)
{
delete this->str;
this->str = NULL;
this->str_len = 0;
std::cout << "析构函数生效" << std::endl;
}
}
/*输出字符串信息*/
void print(Mystring & oop)
{
std::cout << oop.str << std::endl;
}
/*等号操作符重载*/
Mystring & Mystring::operator=(Mystring &oop)
{
this->str_len = oop.str_len;
this->str = new char[str_len + 1];
strcpy(this->str, oop.str);
return *this;
}
/*[]操作符重载*/
char& Mystring::operator[](int index)
{
return this->str[index];
}
/*重载 == ,!=等操作*/
bool Mystring::operator==(Mystring &oop)
{
if(this->str_len != oop.str_len)
return false;
else
{
if (this->str_len == oop.str_len)
{
for (int i = 0; i < oop.str_len; i++)
{
if (this->str[i] != oop.str[i])
return false;
continue;
}
return true;
}
}
}
bool Mystring::operator==(const Mystring &oop)
{
if (this->str_len != oop.str_len)
return false;
else
{
if (this->str_len == oop.str_len)
{
for (int i = 0; i < oop.str_len; i++)
{
if (this->str[i] != oop.str[i])
return false;
continue;
}
return true;
}
}
}
bool Mystring::operator!=(const Mystring &oop)
{
if (this->str_len != oop.str_len)
return true;
else
{
if (this->str_len == oop.str_len)
{
for (int i = 0; i < oop.str_len; i++)
{
if (this->str[i] == oop.str[i])
continue;
else
return true;
}
return false;
}
}
}
bool Mystring::operator!=(Mystring &oop)
{
if (this->str_len != oop.str_len)
return true;
else
{
if (this->str_len == oop.str_len)
{
for (int i = 0; i < oop.str_len; i++)
{
if (this->str[i] == oop.str[i])
continue;
else
return true;
}
return false;
}
}
}
/*括号操作符重载*/
char* Mystring::operator()(const char* str)
{
this->str_len = strlen(str);
this->str = new char[this->str_len + 1];
strcpy(this->str,str);
return this->str;
}
main.cpp验证:
#include <iostream>
#include "Mystring.h"
#include <cstring>
using namespace std;
class mfghj{
private:
int sl;
int bl;
};
int main()
{
{
Mystring oop1("baibai");
const Mystring oop2("nihao");
Mystring oop3;
char* re = oop3("zaiganma");/*括号运算符验证*/
cout<<re<<endl;
#if 0
Mystring oop3 = oop2;//调用构造函数实现
print(oop3);
oop2 = oop1;//“等号作符验证 字符串复制操作,通过等号运算符重载实现”
print(oop2);
cout << oop2[0] << endl;//[]操作符重载验证
#endif
cout << "判断两个对象:" << (oop1 == oop2) << endl;/*验证==*/
cout << "判断两个对象是否不想等" << (oop1 != oop2) << endl;
}
/*string str1 = "nihao";
string str2 = "nihao";
if (str1 == str2)
{
cout << "相等" << endl;
}
else
cout << "不想等" << endl;
*/
system("pause");
return 0;
}