自己写的 string 类

现在很多面试题都是要求写一个String类,主要检查Big 3,也就是主要检查构造函数,析构函数和赋值函数.(不要告诉我你以为是:姚明,麦蒂,阿泰...),这也是很考验基本功的一个题目.我今天自己写了一个MyString类,顺便加上了StrAdd函数用于字符串相加,Get函数用于输出.详细代码如下,说明见注释.

 
  1. #include<iostream>
  2. #include "assert.h"
  3. using namespace std;
  4.  
  5. // MyString类声明
  6. class MyString
  7. {
  8. public:
  9.     MyString(const char* str=NULL);
  10.     ~MyString();
  11.     MyString(const MyString & str);
  12.     MyString &operator=(const MyString & str);
  13.     MyString &StrAdd(
  14.         const MyString &str1,const MyString &str2);
  15.     char* Get();
  16. private:
  17.     char* m_data;
  18. };
  19.  
  20. // 构造函数
  21. MyString::MyString(const char* str/* =NULL */)
  22. {
  23.     // 空的就给个结束0就ok了
  24.     if (NULL==str)
  25.     {
  26.         m_data=new char[1];
  27.         m_data[0]='\0';
  28.     }
  29.     else
  30.     {
  31.         // 根据str创建新的m_data..记得加1哦...
  32.         m_data=new char[strlen(str)+1];
  33.         strcpy(m_data,str);
  34.     }
  35. }
  36.  
  37. // 析构函数
  38. MyString::~MyString()
  39. {
  40.     // 可以不用[]  因为是内部的
  41.     delete []m_data;
  42. }
  43.  
  44. // 拷贝构造函数
  45. MyString::MyString(const MyString & str)
  46. {
  47.     // 根据str.m_data创建长度...记得加1哦...
  48.     m_data=new char[strlen(str.m_data)+1];
  49.     strcpy(m_data,str.m_data);
  50. }
  51.  
  52. // 赋值函数
  53. MyString &MyString::operator=(const MyString & str)
  54. {
  55.     // 检查不是自己给自己赋值的才构建...否则直接返回了...
  56.     if (&str!=this)  // 注意:不要写成str!=*this  这样是挂了的...
  57.     {
  58.         // only 一次机会...不要内存泄露了哦...
  59.         delete [] m_data;
  60.         // new一个...创建空间
  61.         m_data=new char[strlen(str.m_data)+1];
  62.         strcpy(m_data,str.m_data);
  63.     }
  64.     // 实现链式操作...
  65.     return *this;
  66. }
  67.  
  68. // 相加函数
  69. MyString &MyString::StrAdd(
  70.         const MyString &str1,const MyString &str2)
  71. {
  72.     assert((&str1!=NULL) && (&str2!=NULL));
  73.     delete []m_data;
  74.     m_data=new char[strlen(str1.m_data)+strlen(str2.m_data)+1];
  75.     strcpy(m_data,str1.m_data);
  76.     strcat(m_data,str2.m_data);
  77.     return *this;
  78. }
  79.  
  80. // 获得字符串函数
  81. char* MyString::Get()
  82. {
  83.     return m_data;
  84. }
  85.  
  86. void main(void
  87.     // a-f中只有c是利用拷贝构造函数初始化,其余利用一般构造函数
  88.     // d=b是让d使用赋值函数
  89.     // f为了表示利用了链式操作,e表示利用StrAdd
  90.     MyString a("hello,");   // 一般构造函数初始化
  91.     MyString b("world!");   // 一般构造函数初始化
  92.     MyString c(a);          // 拷贝构造函数初始化
  93.     MyString d,e,f;         // 一般拷贝构造函数初始化,m_data仅仅有个'\0'
  94.     d=b;                    // 赋值函数对d赋值,来自b.m_data
  95.     f.StrAdd(e.StrAdd(a,b),e);  // 利用StrAdd进行链式操作,并相加
  96.  
  97.     // 下面均使用Get()进行输出
  98.     cout<<"a="<<a.Get()<<endl;
  99.     cout<<"b="<<b.Get()<<endl;
  100.     cout<<"c="<<c.Get()<<endl;
  101.     cout<<"d="<<d.Get()<<endl;
  102.     cout<<"e="<<e.Get()<<endl;
  103.     cout<<"f="<<f.Get()<<endl;
  104.  
  105.     system("pause");

 


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值