C++学习之运算符重载在项目开发中的应用

实现一个字符串类

  1. 构造函数要求:
    MyString a;
    MyString a("dasfa");
    MyString b = a;
  2. 常用的操作符: << >> != == > < =
  3. C语言中,没有字符串这种类型,是通过数组来模拟字符串;C++中我们来设计一个字符串 ,以零结尾的字符串

  1. MyString.h
#include<iostream>
using namespace std;

class MyString
{
//<<
friend ostream& operator<<(ostream &out,MyString &s);
private:
	int m_len; char *m_p;
public://构造析构函数
	MyString(int len = 0);
	MyString(const char *p);
	MyString(const MyString& s);
	~MyString();
public://重写 =
	Mystring &opareator=(const MyString &s);
	MyString& operatir=(const char*p);
	//重写[]
	char& operator[](int index);
public://重写 ==
	bool operator==(const MyString& s);	
	//重写 !=
	bool operator!=(char *p);

};
  1. MyString.cpp
#include"MyString.h"
// <<
ostream& operator<<(ostream &out,MyString &s)
{
	out<<s.m_p;
	return out;
}
//构造析构
//空串
MyString::MyString(int len)
{
	if(len == 0)
	{
		m_len = 0;//空串
		m_p = new char[m_len + 1];
		strcpy(m_p,"");
	}
	else
	{
		m_len = len;
		m_p = new char[m_len + 1];
		strcpy(m_p,"");
	}
}
//s2("s2");
MyString::MyString(const char *p)
{
	if(p == NULL)
	{
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p,"");
	}
	else
	{
		m_len = strlen(p);
		m_p = new char[m_len + 1];
		strcpy(m_p,p);
	}
}
//copy构造函数 s3 = s2
MyString::MyString(MyString const &s)
{
	m_len = s.m_len;
	m_p = new char[m_len + 1];
	strcpy(m_p,s.m_p);
}
//析构
MyString::~MyString()
{
	if(m_p != NULL)
	{
		delete[]m_p;
		m_p = NULL;
		m_len = 0;
	}
}
//重写 =
//s4 = s2
MyString& MyString::operator=(const MyString &s)
{
	//1 释放旧内存
	if(m_p != NULL)
	{
		delete[]m_p;
		m_len = 0;
	}
	//2 根据s2分配内存
	m_len = s.m_len;
	m_p = new char [m_len + 1];
	strcpy(m_p,s.m_p);
	return *this;
}
//s4 = "s444"
MyString& MyString::operator=(char *p)
{
	//1 释放旧内存
	if(m_p != NULL)
	{
		delete[]m_p;
		m_len = 0;
	}
	//2 根据p分配内存
	if(p == NULL)
	{
		m_len = 0;
		m_p = new char[m_len + 1];
		strcpy(m_p,"");
	}
	else
	{
		m_len = strlen(p);
		m_p = new char[m_len +1 ];
		strcpy(m_p,p);
	}
	return *this;
}
//重写[]
char& MyString::operator[](int index)
{
	return m_p[index];
}
//重写==
bool operator==(const MyString& s)
{
	if(m_len == s.m_len)
	{
		return true;
	}
	return strcmp(m_p,s.m_p);
}
//重写!=
bool operator!=(char *p)
{
	if(p == NULL)
	{
		if(m_len == 0)
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		if(m_len != strlen(p))
		{
			return strcmp(m_p,p);
		}
		else
		{
			return true;
		}
	}
	return false;
}
  1. MyStringTest.cpp
#include<iostream>
using namespace std;
#include"MyString.h"
void main01()
{
	MyString s1;
	MyString s2("s2");
	MyString s3 = s2;	
	MyString s4 = "s4444";

	//测试 =
	s4 = s2;
	//Mystring &opareator=(const MyString &s2)
	s4 = "s44";
	//MyString& operatir=(const char*p)
	//测试[]
	s4[3] = '8';
	//char& operator[](int dex);
	//测试 <<
	cout<<s4<<endl;
	//ostream& operator<<(ostream &out,MyString &s)
	system("pause");
}

//重载 ==  !=
void main02()
{
	MyString s1;
	MyString s2("s2");
	MyString s3 = s2;
	//测试 ==
	if(s3 == s2)
	{
		printf("相等\n");
	}
	else
	{
		printf("不相等\n");
	}
	//bool operator==(const MyString& s)
	//测试!=
	if(s3 != s2)
	{
		printf("不相等\n");
	}
	else
	{
		printf("相等\n");
	}
	//bool operator!=(char *p);
	system("pause");
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值