C++学习笔记 lesson6 重载String类

定义MyString类

 MyString.h

#ifndef _MYSTRING_H_
#define _MYSTRING_H_
namespace PoEdu {
	class MyString
	{
	public:
		MyString(const char *str="");   //空指针 空字符串(有地址)
		~MyString();
		//重载赋值
		MyString&operator=(const MyString&other);
		MyString&operator=(const char *str);
		MyString&operator=(const int num);
		MyString&operator=(const double num);
		//拷贝构造
		MyString(const MyString &other);
		//重载&
		//重载*

		//重载+
		MyString  operator+(const MyString &other);//返回加之前的对象
		
		friend MyString&operator+(const char *str, const MyString&other);
				
		//friend MyString operator+(const char *str1, const char *str, const MyString& other);
		//没有这种重载,参数太多,无法实现 "string"+"string"+MyString 除非原本的二者支持+ 比如 1+2+MyString
		//连加最终为形成多个参数的形式,
		
		//1 运算的数量 + - 2元 1元
		//2 连加的操作 对象本身就支持+    int 支持+ 

		//重载+=
		MyString& operator+=(const MyString &other);//返回加之后的对象
		//重载[]
		char& operator[](unsigned int index)
		{
			//return data_[index];
			//非const会去调用const
			return const_cast<char&>((static_cast<const MyString>(*this))[index]);
		}


		const char & operator[](unsigned int index)const
		{
			//和非const函数构成重载
			//const对象只能访问const函数
			return data_[index];
		}


	private:
		char *data_;
		char *Alloc(const char *data );
	};
}
#endif


MyString.cpp

#include "MyString.h"
#include<cstring>
#include<cstdlib>
#include<cstdio>
const int MAXSIZE = 255;//全局变量不要写在.h文件中,写在.cpp源文件中
						//写在头文件中会导致重复定义
namespace PoEdu
{
	
	MyString::MyString(const char *str)
	{
		//深拷贝
		/*int len = strlen(str) + sizeof(char);
		data_ = new char[len];
		strcpy(data_, str);*/
		data_ = Alloc(str);
	}


	MyString::~MyString()
	{
		 
		delete[]data_; 
	}
	MyString & MyString::operator=(const MyString & other)
	{
		delete[]data_;
		data_ = Alloc(other.data_);
		return *this;
	}
	MyString & MyString::operator=(const char * str)
	{
		delete[]data_;
		data_ = Alloc(str);
		return *this;
	}
	MyString & MyString::operator=(const int num)
	{
		char str[MAXSIZE] = {0};
			itoa(num,str,10);
			delete[]data_;
			data_ = Alloc(str);
			return *this;
	}
	MyString & MyString::operator=(const double num)
	{
		char str[MAXSIZE] = { 0 };
		sprintf(str, "%f", num);
		delete[]data_;
		data_ = Alloc(str);
		return *this;
	}
	MyString::MyString(const MyString & other)
	{
		data_ = Alloc(other.data_);
	}
	MyString MyString::operator+(const MyString & other)
	{
		
		MyString temp = *this;//使用临时对象保存
		temp += other;//调用重载的+=
		return MyString();
	}
	MyString& MyString::operator+=(const MyString & other)
	{
		int len = strlen(data_);//先拿到长度
		len += strlen(other.data_) + sizeof(char);
		char *newData = new char[len];
		strcpy(newData, data_);
		strcat(newData, other.data_);
		delete[]data_;
		return *this;
			
	}
	char& MyString::operator[](unsigned index)
	{
		return data_[index];
	}
	char * MyString::Alloc(const char * data )
	{
		int len = strlen(data) + sizeof(char);
		char *newData = new char[len];
		strcpy(newData, data);
		return newData;
	}
}

main.cpp
#include "MyString.h"


int main()
{
	using namespace PoEdu;
	{
		//MyString demo1;
		//MyString demo2 = demo1;
		
		MyString demo = "Hello";
		//调用构造函数
		demo += "Mark";
		//先将Mark转化成 temp临时对象 "Mark"=MyString("Mark")
		//再调用 demo.operator+=
		//demo = "Love" + demo;
		//不能通过编译
		// 所有的运算符的重载实质是 当前对象调用当前方法 
		//demo.operator=("Love".operator+(demo))
		//没有string.operator+ 需要用友元的方式重载+
	}
}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值