#include<iostream.h>
#include<string.h>
class String
{
public:
String(char *);
virtual ~String();
void strlen();
void strcat(char *);
private:
char *cn;
int carlen;
};
String::String(char *pn)
{
int j=0;
for(int i=0;pn[i]!=NULL ;i++)
j++;
cn=new char[j];
strcpy(cn,pn);
carlen=j;
}
void String::strlen()
{
cout<<carlen<<endl;
}
void String::strcat(char *q)
{
for(int i=0 ; q[i]!=NULL ; i++,carlen++)
cn[carlen]=q[i];
for(int j=0 ;j<carlen; j++)
cout<<cn[j];
cout<<endl;
}
String::~String()
{
//delete []cn;
}
void main()
{
String string("hello");
string.strlen();
string.strcat("you");
}
创建CString类,写出Strlen函数和Strcat()函数,而不要直接调用微软的String.h里的。
代码可能很粗糙,但是最后我还是做了出来,考试啊考试。