一个简单实现的string类
发布时间:2020-12-14 03:55:20 所属栏目:Windows 来源:网络整理
导读:为了复习c++知识,简单的实现一个string类,类名为CMyString 环境说明:windows 7 64位 开发工具:Visual Studio 2015 CMyString类的头文件CMyString.h 1 #include iostream 2 3 #ifndef __C_MY_STRING__ 4 #define __C_MY_STRING__ 5 6 class CMyString 7 {
为了复习c++知识,简单的实现一个string类,类名为CMyString环境说明:windows 7 64位开发工具:Visual Studio 2015CMyString类的头文件CMyString.h1 #include <iostream> 2 3 #ifndef __C_MY_STRING__ 4 #define __C_MY_STRING__ 5 6 class CMyString 7 { 8 public: 9 //默认构造函数 10 CMyString(); 11 //带参数的构造函数 12 CMyString(const char* str); 13 //拷贝构造函数 14 CMyString(const CMyString&); 15 //析构函数 16 ~CMyString(); 17 18 //重载赋值运算符 19 CMyString& operator=(const CMyString&); 20 CMyString& operator=(const char*); 21 //重载[]运算符(可修改) 22 char& operator[](const int); 23 //重载[]运算符(不可修改) 24 const char& operator[](const int) const; 25 //重载==运算符 26 bool operator==(const CMyString&) const; 27 //重载!=运算符 28 bool operator!=(const CMyString&) const; 29 //重载>运算符 30 bool operator>(const CMyString&) const; 31 //重载<运算符 32 bool operator<(const CMyString&) const; 33 //重载>=运算符 34 bool operator>=(const CMyString&) const; 35 //重载>=运算符 36 bool operator<=(const CMyString&) const; 37 //重载<<运算符 38 friend std::ostream& operator<<(std::ostream&,const CMyString &); 39 private: 40 char* m_pdata; 41 }; 42 43 #endif // !__C_MY_STRING__ ? CMyString类的实现文件CMyString.cpp1 #include "CMyString.h" 2 3 CMyString::CMyString() 4 { 5 //创建一个空的data,占一个字节空间 6 m_pdata = new char[1]; 7 m_pdata[0] = ‘ |