实现string的默认构造函数,拷贝函数,c型字符串参数的构造函数,赋值构造函数,析构函数,重载输出操作符,测试

#include <iostream>
using namespace std;
class TestString;
//extern ostream& operator<<(ostream& out, TestString& testString); 

class TestString
{
      friend ostream& operator<<(ostream& out, TestString& testString);
    
      public:
             TestString();
             ~TestString();
             TestString(const char * charArray);
             TestString& operator=(const char *charArray);
             TestString& operator=(const TestString& testString);
             TestString(const TestString& testString);
            
      private:
             char* m_myString;
             int m_length;
}; 

TestString::TestString()
{
      m_myString =NULL;    
      m_length = 0;              
}

TestString::~TestString()
{
 delete [] m_myString;
}

TestString::TestString(const char * charArray)
{
     if(charArray == NULL)
     {
          m_myString = new char[1];
          m_myString[0] = '\0';    
           m_length = 0;         
     }
    else
    { m_length = strlen(charArray);  
      m_myString = new char[m_length]; 
      memcpy((void*)m_myString,(void*)charArray,m_length);
    }                  
}

TestString& TestString::operator=(const char *charArray)
{
    if(m_myString)
    {
            delete [] m_myString;
           m_myString = NULL;
           m_myString = 0;
    }
    if(charArray == NULL)
    {
             m_myString = new char[1];
          m_myString[0] = '\0';    
           m_length = 0;  
    }
    else
    {
        m_length = strlen(charArray);  
      m_myString = new char[m_length]; 
      memcpy((void*)m_myString,(void*)charArray,m_length);
    }
    return *this;
}

TestString& TestString::operator=(const TestString& testString)
{
        //需要检测自赋值
        if(this == &testString)
        {
            return *this;
        }    
        else
        {
            delete [] m_myString;
            m_length = testString.m_length;
            m_myString = new char[testString.m_length];
            
            memcpy((void*)m_myString,(void*)testString.m_myString,m_length);
            return *this;
        }
}

TestString::TestString(const TestString& testString)
{
   m_length = testString.m_length;
   m_myString = new char[m_length];
   memcpy((void*)m_myString,(void*)testString.m_myString,m_length); 
}

ostream& operator<<(ostream& out, TestString& testString)
{
for(int i=0;i!=testString.m_length;++i)
{
 out<<*((char*)(testString.m_myString+i));    
}
           return out;
}

int main()
{
    TestString test("ad");// = "abcd";
    test = test;
    TestString test2;
    cout<<test<<endl;
     cout<<test2<<endl;
     test2 = test;
     TestString test3(test2);
     cout<<test3<<endl;
    system("pause");
    return 0; 
} 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zanglengyu

敲碗要饭

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值