用c++ 的class 来实现strcpy和strcat函数
只需写出class的prototype,成员函数不用具体实现,但要详细说出每个成员函数都有什么作用最好应用到c++的封装,继承,多样化的特征。...
只需写出class的prototype,
成员函数不用具体实现,
但要详细说出每个成员函数都有什么作用
最好应用到c++的封装,继承,多样化的特征。 展开
成员函数不用具体实现,
但要详细说出每个成员函数都有什么作用
最好应用到c++的封装,继承,多样化的特征。 展开
展开全部
CString类的实现
其中的operator=就相当于strcpy
operator+相当于strcat
class CString
{
public:
CString();
CString(const CString &strTag);
CString(const char* szTag);
virtual ~CString();
public:
char* GetBuffer();
public:
CString& operator=(const char* strTag);
CString& operator=(const CString& strTag);
CString& operator+(const char* szAdd);
CString& operator+(const CString& strTag);
public:
void InitData(const char* szInit);
friend ostream& operator<< ( ostream& os, const CString& strOut );
private:
char* m_pszData;
int m_nBufLen;
};
.CPP 文件
#include "CString.h"
CString::CString()
{
m_pszData=NULL;
m_nBufLen = 1;
m_pszData = new char[m_nBufLen];
m_pszData[0] = '';
}
CString::~CString( )
{
if( m_pszData )
{
delete m_pszData;
m_pszData = NULL;
m_nBufLen = 0;
}
}
void CString::InitData(const char *szInit)
{
if( m_pszData )
{
delete m_pszData;
m_pszData = NULL;
m_nBufLen = 0;
}
int nLen = strlen(szInit);
m_pszData = new char[nLen+1];
if( m_pszData )
{
strncpy(m_pszData,szInit,nLen);
m_pszData[nLen] = '';
m_nBufLen = nLen+1;
}
}
CString::CString(const CString &strTag)
{
m_pszData=NULL;
m_nBufLen = 0;
InitData(strTag.m_pszData);
}
CString::CString(const char* szTag)
{
m_pszData=NULL;
m_nBufLen = 0;
InitData(szTag);
}
CString& CString::operator +(const char* szAdd)
{
int nLenthis = strlen(m_pszData);
int nLenAdd = strlen(szAdd);
char* pAdd = new char[nLenthis+nLenAdd+1];
strncpy(pAdd,m_pszData,nLenthis+1);
strcat(pAdd,szAdd);
static CString strAdd;
strAdd = pAdd;
delete pAdd;
return strAdd;
}
CString& CString::operator+(const CString& strTag)
{
return operator+(strTag.m_pszData);
}
CString& CString::operator =(const char *szTag)
{
int nTagLen = strlen(szTag);
char* pszAdd = new char[nTagLen+1];
if( pszAdd )
{
strncpy(pszAdd,szTag,nTagLen+1);
m_nBufLen = nTagLen+1;
delete m_pszData;
m_pszData = pszAdd;
}
return *this;
}
CString& CString::operator =(const CString &strTag)
{
return operator=(strTag.m_pszData);
}
ostream& operator<< ( ostream& os, const CString& strOut )
{
os<<strOut.m_pszData;
return os;
}
char* CString::GetBuffer()
{
return m_pszData;
}
其中的operator=就相当于strcpy
operator+相当于strcat
class CString
{
public:
CString();
CString(const CString &strTag);
CString(const char* szTag);
virtual ~CString();
public:
char* GetBuffer();
public:
CString& operator=(const char* strTag);
CString& operator=(const CString& strTag);
CString& operator+(const char* szAdd);
CString& operator+(const CString& strTag);
public:
void InitData(const char* szInit);
friend ostream& operator<< ( ostream& os, const CString& strOut );
private:
char* m_pszData;
int m_nBufLen;
};
.CPP 文件
#include "CString.h"
CString::CString()
{
m_pszData=NULL;
m_nBufLen = 1;
m_pszData = new char[m_nBufLen];
m_pszData[0] = '';
}
CString::~CString( )
{
if( m_pszData )
{
delete m_pszData;
m_pszData = NULL;
m_nBufLen = 0;
}
}
void CString::InitData(const char *szInit)
{
if( m_pszData )
{
delete m_pszData;
m_pszData = NULL;
m_nBufLen = 0;
}
int nLen = strlen(szInit);
m_pszData = new char[nLen+1];
if( m_pszData )
{
strncpy(m_pszData,szInit,nLen);
m_pszData[nLen] = '';
m_nBufLen = nLen+1;
}
}
CString::CString(const CString &strTag)
{
m_pszData=NULL;
m_nBufLen = 0;
InitData(strTag.m_pszData);
}
CString::CString(const char* szTag)
{
m_pszData=NULL;
m_nBufLen = 0;
InitData(szTag);
}
CString& CString::operator +(const char* szAdd)
{
int nLenthis = strlen(m_pszData);
int nLenAdd = strlen(szAdd);
char* pAdd = new char[nLenthis+nLenAdd+1];
strncpy(pAdd,m_pszData,nLenthis+1);
strcat(pAdd,szAdd);
static CString strAdd;
strAdd = pAdd;
delete pAdd;
return strAdd;
}
CString& CString::operator+(const CString& strTag)
{
return operator+(strTag.m_pszData);
}
CString& CString::operator =(const char *szTag)
{
int nTagLen = strlen(szTag);
char* pszAdd = new char[nTagLen+1];
if( pszAdd )
{
strncpy(pszAdd,szTag,nTagLen+1);
m_nBufLen = nTagLen+1;
delete m_pszData;
m_pszData = pszAdd;
}
return *this;
}
CString& CString::operator =(const CString &strTag)
{
return operator=(strTag.m_pszData);
}
ostream& operator<< ( ostream& os, const CString& strOut )
{
os<<strOut.m_pszData;
return os;
}
char* CString::GetBuffer()
{
return m_pszData;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询