C++設計CTString類
#include <iostream>using namespace std;
#include "CTString.h"
int main()
{
CTString *pStr = new CTString("I love you!");
cout << pStr->Size() << endl;
cout << pStr->Empty() << endl;
pStr->Print();
pStr->Copy("Hello");
pStr->Print();
pStr->Connect("World");
pStr->Print();
CTString str("HEHE");
str.Print();
pStr->Copy(&str);
pStr->Print();
CTString str1(10, 'I');
str1.Print();
CTString str2(str1);
str2.Print();
CTString str3(*pStr);
str3.Print();
CTString str4("E");
cout << "str3.Find(&str4)=" << str3.Find(&str4) << endl;
cout << "str3.Find(str4)=" << str3.Find(str4) << endl;
cout << "str4.Find('E') = " << str4.Find('E') << endl;
cout << "對象個數" << CTString::getCount() << endl;
delete pStr;
system("pause");
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//*****************CTString類的聲明開始********************
#ifndef _CTSTRING_H_
#define _CTSTRING_H_
class CTString
{
public:
//默認構造函數
CTString();
//用n個字符ch來初始化
CTString(int n, char ch);
//直接用C風格字符串來初始化
CTString(char *pData);
//複製構造函數
CTString(const CTString &src);
//析構函數
~CTString();
//功能: 完成串的複製
//參數: src --待複製的指向CTString類對象串
//返回: dest
CTString *Copy(CTString *src);
//功能: 完成串的複製
//參數: src --待複製的C字符串
//返回: 指向CTString類對象的指針
CTString *Copy(char *src);
//功能: 完成串的連接
//參數: src --待連接在後麵的CTString對象串
//返回: dest
CTString *Connect(CTString *src);
//功能: 完成串的連接
//參數: src --待連接在後麵的C字符串
//返回: dest
CTString *Connect(char *src);
//功能: 在主串中查找子串pSubstr
//參數: pSubstr --指向子串
//返回: 找到則返回指向找到的位置的下標,若沒有找到,則返回-1
int Find(const CTString *pSubstr) const;
//功能: 在主串中查找子串substr
//參數: substr --子串的引用
//返回: 找到則返回指向找到的位置的下標,若沒有找到,則返回-1
int Find(const CTString &substr) const;
//功能: 在中查找單個字符ch
//參數: ch --待查找的字符
//返回: 找到則返回指向該字符在主串中的位置的下標,若沒有找到,剛返回-1
int Find(char ch) const;
//功能: 輸出字符數據
//參數: 無
//返回: 無
void Print() const;
//功能: 判斷字符串是否為空
//參數: 無
//返回: 若不為空,返回真;若為空,返回假
bool Empty() const;
//功能: 返回字符串的長度
//參數: 無
//返回: 返回字符串的長度--int類型
int Size() const;
//重載操作符 +
//friend const CTString & operator+ (CTString & str1, const CTString & str2);
//功能: 返回m_sCount的值
static int getCount();
private:
char *m_pData; //用於保存字符數據
int m_nLen; //記錄字符長度
static int m_sCount; //記錄創建對象的個數
};
#endif
//*****************CTString類的聲明結束********************
#include <iostream>
#include <cstring>
using namespace std;
#include "CTString.h"
//****************靜態成員初始化*****************
int CTString::m_sCount = 0;
//**************默認構造函數實現部分*************
CTString::CTString()
{
m_pData = NULL;
m_nLen = 0;
m_sCount ++;
cout << "constrcutor CTString() called" << endl;
}
//*********構造函數--用n個字符ch來初始化********
CTString::CTString(int n, char ch)
{
m_sCount ++;
m_nLen = n;
m_pData = new char[n+1];
int i;
for (i = 0; i < n; i++)
{
m_pData[i] = ch;
}
m_pData[i] = '\0';
cout << "constrcutor CTString(int n, char ch) called" << endl;
}
//**************帶參數構造函數實現部分***********
CTString::CTString(char *pData)
{
m_sCount ++;
m_nLen = strlen(pData);
m_pData = new char[m_nLen + 1]; //預留一個位置給'\0'
strncpy(m_pData, pData, m_nLen);
m_pData[m_nLen] = '\0';
cout << "constrcutor CTString(char *pData) called" << endl;
}
//***************複製構造函數實現部分************
CTString::CTString(const CTString &src)
{
if (this->m_pData != NULL)
{
delete this->m_pData;
}
m_sCount ++;
this->m_pData = new char[src.m_nLen+1];
strncpy(this->m_pData, src.m_pData, src.m_nLen);
this->m_pData[src.m_nLen] = '\0';
this->m_nLen = src.m_nLen;
cout << "copy constrcutor CTString(const CTString &src) called" << endl;
}
//*****************析構函數實現部分**************
CTString::~CTString()
{
cout << "destructor called" << endl;
delete m_pData;
}
//********************************************************
//功能: 完成串的複製
//參數: dest --src的備份 src --待複製的串
//返回: dest
CTString* CTString::Copy(CTString *src)
{
delete this->m_pData;
this->m_pData = new char[src->m_nLen+1];
strncpy(this->m_pData, src->m_pData, src->m_nLen);
this->m_pData[src->m_nLen] = '\0';
this->m_nLen = src->m_nLen;
return this;
}
//*********************************************************
//功能: 完成串的複製
//參數: src --待複製的C字符串
//返回: 指向CTString類對象的指針
CTString* CTString::Copy(char *src)
{
//釋放原有空間內存
delete this->m_pData;
int len = strlen(src);
this->m_pData = new char[len+1];
strncpy(this->m_pData, src, len);
this->m_pData[len] = '\0';
this->m_nLen = len;
return this;
}
//*********************************************************
//功能: 完成串的連接
//參數: dest --兩個串連接後得到的串 src --待複製的串
//返回: dest
CTString * CTString::Connect(CTString *src)
{
int len = this->m_nLen + src->m_nLen;
char *tmp = new char[len + 1];
strncpy(tmp, this->m_pData, this->m_nLen);
tmp[this->m_nLen] = '\0';
strncat(tmp, src->m_pData, src->m_nLen);
tmp[len] = '\0';
return new CTString(tmp);
}
//**********************************************************
//功能: 完成串的連接
//參數: src --待連接在後麵的C字符串
//返回: dest
CTString* CTString::Connect(char *src)
{
int len = strlen(src) + this->m_nLen;
char *tmp = this->m_pData;
this->m_pData = new char[len+1];
int i, j;
for (i = 0; i < strlen(tmp); i++)
{
this->m_pData[i] = tmp[i];
}
for (j = 0; j < strlen(src); i++, j++)
{
this->m_pData[i] = src[j];
}
this->m_pData[len] = '\0';
delete tmp;
return this;
}
//**********************************************************
//功能: 在主串中查找子串pSubstr
//參數: pSubstr --指向子串
//返回: 找到則返回指向找到的位置下標,若沒有找到,則返回-1
int CTString::Find(const CTString *pSubstr) const
{
int i = 0, j = 0, index = 0;
int len1 = this->m_nLen,
len2 = pSubstr->m_nLen;
i = 0;
while (i < len1 && j < len2)
{
if (this->m_pData[i] == pSubstr->m_pData[j])
{
i ++;
j ++;
}
else
{
index ++; //主串位置回溯
i = index;
j = 0;
}
}
if (j == len2)
{
return index;
}
return -1;
}
//****************************************************
//功能: 在主串中查找子串substr
//參數: substr --子串的引用
//返回: 找到則返回指向找到的位置的下標,若沒有找到,則返回-1
int CTString::Find(const CTString &substr) const
{
int i = 0, j = 0, index = 0;
while (i < this->m_nLen && j < substr.m_nLen)
{
if (this->m_pData[i] == substr.m_pData[j])
{
i ++;
j ++;
}
else
{
index ++;
i = index;
j = 0;
}
}
if (j == substr.m_nLen)
{
return index;
}
return -1;
}
//****************************************************
//功能: 在中查找單個字符ch
//參數: ch --待查找的字符
//返回: 找到則返回指向該字符在主串中的位置的下標,若沒有找到,剛返回-1
int CTString::Find(char ch) const
{
for (int i = 0; i < strlen(this->m_pData); i++)
{
if (this->m_pData[i] == ch)
{
return i;
}
}
return -1;
}
//*************************************************
//功能: 輸出字符數據
//參數: 無
//返回: 無
void CTString::Print() const
{
if (this->m_pData)
{
cout << this->m_pData << endl;
}
}
//*************************************************
//功能: 判斷字符串是否為空
//參數: 無
//返回: 若不為空,返回真;若為空,返回假
bool CTString::Empty() const
{
if (this->m_nLen)
{
return false;
}
return true;
}
//*************************************************
//功能: 返回字符串的長度
//參數: 無
//返回: 返回字符串的長度--int類型
int CTString::Size() const
{
return this->m_nLen;
}
//************************************************
//功能: 返回m_sCount的值
int CTString::getCount()
{
return m_sCount;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
最後更新:2017-04-02 15:15:02