c++运行时遇到问题
#include <iostream.h>
#include <string.h>
class String
{
public:
~String(){delete[] str;}
int SetStr(int l,char *s);
int SetStr(char *s);
//int Set(char *s);
int GetLen(){return len;}
char *GetStr(){return str;}
void Print(){cout<<str<<endl;}
private:
int len;
char *str;
};
class EditString:public String
{
public:
void MoveCursor(int off){cursor=off;}
int Add(String *s);
int Replace(String *s);
void Delete(int pos);
int GetCursor(){return cursor;}
private:
int cursor;
};
int String::SetStr(int l,char *s)
{
len=l;
if(!str)delete str;
str=new char[len+1];
strcpy(str,s);
return len;
}
int String::SetStr(char *s)
{
len=strlen(s);
if(!str)delete str;
str=new char[len+1];
strcpy(str,s);
return len;
}
int EditString::Add (String *s)
{
int n,k,m;
char *cp, *pt;
n=s->GetLen();
pt=s->GetStr();
cp=this->GetStr();
m=this->GetLen();
char *news=new char[n+m+1];
for(int i=0;i<cursor;i++)
news[i]=cp[i];
k=i;
for(int j=0;j<n;i++,j++)
news[i]=cp[j];
news[i]='\0';
SetStr(news);
delete news;
return cursor;
}
int EditString::Replace(String *s)
{
int n,m;
char *pt, *news;
n=s->GetLen();
pt=s->GetStr();
m=this->GetLen();
news=new char[m>n+cursor?m+1:n+cursor+1];
news=this->GetStr();
for(int i=cursor,j=0;i<n+cursor;j++,i++)
news[i]=pt[j];
if(m<n+cursor)news[i]='\0';
cursor=i;
SetStr(news);
delete news;
return cursor;
}
void EditString::Delete(int pos)
{
int m;
char *cp, *news;
cp=this->GetStr();
m=this->GetLen();
for(int i=cursor;i<m;i++)
cp[i]=cp[i+pos];
cp[i]='\0';
}
void main()
{
String s1;
EditString s2;
char *cp;
s1.SetStr("Object-oriented Programming!");
// s1.Print();
cp=s1.GetStr();
s2.Print();
s2.MoveCursor(16);
s1.SetStr("Windows");
s2.Add(&s1);
s2.Print();
s2.MoveCursor(7);
s2.Delete(9);
s2.Print();
s2.SetStr("TTT");
s2.Replace(&s1);
s2.Print();
}
这是程序有问题还是其余的原因啊
谢谢