基本运算符重载实例

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;
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值