C ++中字符串类运算符重载示例

版权声明:版权所有,如需转载请联系作者邮箱:[email protected] https://blog.csdn.net/gaojixu/article/details/84755188

一、基本概念
(一) 函数重载的含义
所谓重载,就是重新赋予新的含义。函数重载就是对一个已有的函数赋予新的含义,使之实现新功能,因此,一个函数名就可以用来代表不同功能的函数,也就是”一名多用”。

(二) 为什么要进行函数重载
一般情况下,编译器对现有操作符的操作数是有一定的限制,但是通常情况下用户使用class会自定义类型,而编译器是不认可这种类型的操作符运算的,所以可以将操作符进行重载达到可以根据自定义类型进行运算;

(三)函数重载的一般格式
返回值类型 operator op(参数)

(四)一般符号重载的实现步骤:
一般情况下函数重载使用成员函数实现

  • 1)要承认操作符重载是一个函数,写出函数名称operator op ()
  • 2)根据所要重载的操作符的操作数,写出函数参数
  • 3)根据函数的目的,完善函数返回值(看函数是返回引用 还是指针 元素)
  • 4)实现具体的函数内容

二、具体的程序示例
程序分为三部分:
声明:MyString.h
具体实现:MyString.cpp
应用:example.cpp
运行平台为:windows 10 & VS2017,所有程序运行结果均正确

1.MyString.h

#pragma once
#include "iostream"
using namespace std;

class MyString
{
public:
//这是构造函数和析构函数
	MyString();
	MyString(const char *p);
	MyString(const MyString &s);
	~MyString();
private:
	int m_len;
	char* m_p;

public:
//实现  =  操作符的重载
	MyString& operator=(const char *p);
	MyString& operator= (const MyString &s);
//实现 []  操作符的重载
	char& operator[](int index);
//实现  <<  操作符的重载
	friend ostream & operator<<(ostream &out, MyString &s);
//实现 ==  和 !=  操作符的重载
	bool operator==(const char *p);
	bool operator!=(const char *p);
	bool operator==(const MyString&s);
	bool operator!=(const MyString &s);
//实现  <   操作符的重载
	int operator<(const char *p);
	int operator<(const MyString &s);
};

2.MyString.cpp

#include "MyString.h"
#include"math.h"
#include "iostream"
#pragma warning(disable:4996)

//构造函数:
	//将对象初始化为空字符串
MyString::MyString() 
{
	m_len = 0; 
	m_p = new char[m_len + 1]; 
	//开辟一个存放字符数组的空间,大小为(m_len + 1) *1 ,并且返回首地址赋值给指针变量m_p 
	strcpy(m_p, " ");
}

  //将对象初始化为一个字符串
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);
	}
}

//拷贝构造函数
	//实现MyString s3 = s2;
 MyString::MyString(const MyString &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 = "s2222"
 MyString&MyString:: operator=(const char *p)
	{
		//因为s4已经分配内存,应该先将旧的内存空间删掉然后再分配新的
		
		//1.释放旧内存
		if (m_p != NULL)
		{
			delete[] m_p;
			m_p = NULL;
			m_len = 0;
		}else
		{
			//分配新的内存
			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;
		}
	}


		//用于实现s4 = s2
 MyString&MyString:: operator= (const MyString &s)
	{
		if (m_p != NULL)
		{
			delete[] m_p;
			m_p = NULL;
			m_len = 0;
		}else
		{
			//根据s(对应于s2)分配新的内存
			m_len = s.m_len;
			m_p = new char[m_len + 1];
			strcpy(m_p, s.m_p);
			return *this;
		}
	}


//实现[] 操作符重载
 char&MyString::operator[](int index)
 {
	 return m_p[index];
 }


 //注意这个是全局函数,所以函数名前面不能加上MyString::  
 ostream& operator<<(ostream &out, MyString &s)
 {
	 cout << s.m_p;
	 return out;
 }



 //下面是实现==和!= 的重载,其中分为类和字符串的比较与类和类的比较
 bool MyString::operator==(const char *p)
 {
	 if (p == NULL)
	 {
		 if (m_len == 0)
		 {
			 return true;
		 }
		 else
		 {
			 return false;
		 }
	 }
	 else
	 {
		 if (m_len == strlen(p))
		 {
			 return !strcmp(m_p, p);
		 } 
		 else
		 {
			 return false;
		 }
	 }
	 return true;
 }

 bool MyString::operator!=(const char *p)
 {
	 return !(*this == p);
 }


 //两个类之间的比较
 bool MyString::operator==(const MyString&s)
 {
	if (m_len != s.m_len)
	{
		return false;
	}
	return !strcmp(m_p, s.m_p);
 }

 bool MyString::operator!=(const MyString &s)
 {
	 return !(*this == s);
 }






 //实现  <  的重载

 int MyString::operator<(const char *p)
 {
	 return strcmp(this->m_p, p);
 }
 int MyString::operator<(const MyString &s)
 {
	 return strcmp(this->m_p, s.m_p);
 }

3.example.cpp

// 实现一个字符串类
//C语言中 没有字符串这种类型,是通过数组来模拟字符串

//C++中 我们来设计一个字符串类 以零结尾的字符串

//若len为0,表示空串

#include "iostream"
#include "MyString.h"
using namespace std;
#pragma  warning (disable: 4996)


int main()
{
	MyString s1;
	MyString s2("s2");
	MyString s2_2 = NULL;
	MyString s3 = s2;


//下面进行操作符重载
	//=操作符
	//两种调用方式;
	MyString s4 = "adfdfdn";
	
	s4 = "s2222"; 

	//调用方式二;
	s4 = s2;

//实现[]重载
  //当[]当右值的时候
	s4[1] = 'a';
	cout << "s4[1] = " << s4[1] << endl;

//实现<<操作符的重载
	cout << s4 << endl;   //相当于实现字符串的整体输出

//实现== 和!= 的重载
	MyString s5 = "ahhhh";
	
	if (s5 == "shhsk")
	{
		cout << "true" << endl;
	}
	else
	{
		cout << "false" << endl;
	}

	if (s5 != "sjfddsj")
	{
		cout << "false" << endl;
	}
	else
	{
		cout << "true" << endl;
	}
	
	//两个类之间做判断
	
	if (s5 == s2)
	{
		cout << "true" << endl;
	}
	else
	{
		cout << "false" << endl;
	}

	if (s5 != s2)
	{
		cout << "false" << endl;
	}
	else
	{
		cout << "true" << endl;
	}



//实现大于小于号的符号重载

	MyString s6 = "skdjfkld";
	if (s6 < "kdjfkdj")
	{
		cout << "s6 smaller than  skdjfkld" << endl;
	} 
	else
	{
		cout << "s6  bigger than  skdjfkld" << endl;
	}


	if (s6 < s5)
	{
		cout << "s6 smaller than s5" << endl;
	}
	else
	{
		cout << "s6 bigger than s5" << endl;
	}


	//使用类中的private:的指针

	MyString s7 = "jdkfjdklfjdl";
	strcpy(s7.c_str(), "lskjdfkljdklf");
	cout << s7 << endl;
}


猜你喜欢

转载自blog.csdn.net/gaojixu/article/details/84755188