33,311
社区成员
发帖
与我相关
我的任务
分享
//this is String.cpp
#include <windows.h>
class String
{
private:
char * m_pCh;
int len;
static int num_strings;//number of string
static const int CINLIM=80;//cin input limit
public:
String();
String(const char * s);
String(const String &s);
int Length()const{return len;};
String & operator=(const String &s);
String & operator=(const char* s);
char & operator[](int i);
const char & operator[](int i)const;
friend bool operator<(const String &s1,const String & s2);
friend bool operator>(const String & s1,const String & s2);
friend bool operator==(const String & s1,const String & s2);
static int HowMany();
};
String & String::operator=(const char *s)
{
delete[] m_pCh;
len = strlen(s);
m_pCh=new char[len+1];
strcpy(m_pCh,s);
return *this;
}
String & String::operator=(const char *s)
{
delete[] m_pCh;
len = strlen(s);
m_pCh=new char[len+1];
strcpy(m_pCh,s);
return *this;
}
放到cpp中,不要放在头文件中