#include
#include
class STR
{
public:
STR(char*x);
STR();
void disp();
int getlength();
void addchar(char a);
private:
char*contents;
int length;
};//类的实现
STR::STR(char*x)
{
length=strlen(x)+1;
contents=new char[length];
for(int i=0;i contents[i]=x[i];
}
void STR::disp()
{
for(int i=0;icout<cout<}
STR::STR()
{
length=0;
contents=NULL;
}
int STR::getlength()
{
return length;
}
void STR::addchar(char a)
{
char *temp;
temp=new char[length];
strcpy(temp,contents);
length++;
delete contents;
contents= new char[length];
for(int i=0;i contents[i]=temp[i];
contents[length-2]=a;
contents[length-1]='/n';
}//主函数
void main()
{
STR string("hello");
cout<<"The length of the sting hello is: "< string.addchar('j');
cout<<"After adding 'j' is: ";
string.disp();
}
--------------------next---------------------
阅读(1302) | 评论(0) | 转发(0) |