C++字符串类实现

C++字符串类实现,包括默认构造函数,带一个参数的构造函数,copy构造函数,赋值运算,加法运算等 

在VC6.0下编译通过

by : kvew    www.smatrix.org/bbs   www.secoder.org/bbs

main函数:

#include <iostream>
#include "strings.h"

using namespace std;

int main(){

 Strings  s3;
 Strings  s4;
 Strings  s5;
 s3.display();

 Strings s1("hello");
 s1.display();
 Strings s2(s1);
 s2.display();
 s3 = s2;
 s3.display();
 s4 = s1+s2;
 s4.display();
   
 s5 = s4 + "good";
 s5.display();

return 0;
}

strings.h函数

#ifndef _STRINGS_
#define _STRINGS_
#include<iostream>
using namespace std;

class Strings{

public:

 Strings();
 
 Strings(char *s);
 
 Strings(const Strings &s);

 Strings& operator = (const Strings& s);

 Strings& operator + (const Strings& s);

 Strings& operator + (const  char *s);

 ~Strings(){
  if(str != NULL)
   delete [] str;
 cout<<"~Strings() is called"<<endl;
 }

 display();

private:
 char *str;


};

#endif

 

strings.cpp函数

#include "strings.h"
#include <iostream>
using namespace std;

Strings::Strings(){                                   //  默认构造函数
 
  str = new  char('A');    //初试化为字符A

  //str = "nothing";          试试如此初试话 :-)  呵呵
   cout<<"Strings is called"<<endl;
 }
 
Strings::Strings(char *s){                     
 str = new char[strlen(s) + 1];             //  带一个参数的构造函数
 if(str)
 strcpy(str, s);
 cout<<"Strings(char *s) is called"<<endl;
 }
 
Strings::Strings(const Strings &s){   //  copy构造函数
    str = new char[strlen(s.str) + 1];
 if(str)
 strcpy(str, s.str);
 cout<<"Strings(const Strings &s) is called   "<<endl;
 }

Strings& Strings::operator = (const Strings &s){   //  赋值运算
 if(this != &s){          // 如果str不是它本身
  if(str != NULL)

  delete  [] str;   //防止内存泄露

  str = new char[strlen(s.str) + 1];
  strcpy(str, s.str);
  
  cout<<"Strings(const Strings &s) is called   "<<endl;
 }

  return *this;
 
 }

Strings& Strings::operator + (const Strings& s){   //  加法运算

 char * temp;
 temp = new char[strlen(str)+strlen(s.str)+1];
 strcpy(temp, str);
 delete [] str;
 strcat(temp,s.str);

str = temp;
 return *this;
}

Strings& Strings::operator + (const  char *s){  //  加法运算

 char * temp;
 temp = new char[strlen(str)+strlen(s)+1];
 strcpy(temp, str);
 delete [] str;
 strcat(temp,s);
 str = temp;
 return *this;
}


Strings::display(){                    //  显示函数
 char *p=str;
 while(*p != '/0'){
   cout<<*p;
   p ++;
  }
  cout<<endl;
cout<<"display is called  "<<endl;
 }

自己实现字符串类 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值