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+ 需要用友元的方式重載+
	}
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章