深圳唱将音乐俱乐部:C++运行没结果

来源:百度文库 编辑:高考问答 时间:2024/05/05 23:05:51
#include <iostream.h>
#include <string.h>
class CStrOne
{
public:
getstr(char *str)
{
int n =strlen(str);
strcpy(str1,str); str1[n]='\n';
}
print ()
{
cout<<"字符串1"<<str1;
}

char str1[30];
};
class CStrTwo :public CStrOne
{
public:
CStrTwo(char *str)
{
int n=strlen(str);
strcpy(str2,str); str2[n]='\n';
}
addstr()
{
strcat(str1,str2);
}
print2()
{
cout<<"字符串2"<<str2;

}
char str2[10];
};
void main()
{
cout<<"-------------\n";
CStrTwo xxx("人类");
xxx.getstr("我是");
xxx.print();
xxx.addstr();
xxx.print2();
}

能通过编译 但运行没结果

#include <iostream.h>
#include <string.h>
class CStrOne
{
public:
void getstr(char *str)
{
char *p;
p=str1;
int n =strlen(str);
strcpy(p,str); str1[n]='\0';
}
void print ()
{
char *p;
p=str1;
cout<<"字符串1"<<p;
}

char str1[30];
};
class CStrTwo :public CStrOne
{
public:
CStrTwo(char *str)
{
char *p;
p=str2;
int n=strlen(str);
strcpy(p,str); p[n]='\0';
}
void addstr()
{
char *p1,*p2;
p2=str2;
p1=str1;
strcat(p1,p2);
}
void print2()
{
cout<<"字符串2"<<str2;

}
char str2[10];
};
void main()
{
cout<<"-------------\n";
CStrTwo xxx("人类");
xxx.getstr("我是");
xxx.print();
xxx.addstr();
xxx.print2();
}

这回你再看看,原因是数组的名字是个指针常量,是定值,不能改变,你的把它们给一个同类型的变量...

改成下面再试试
str2[n+1]='\0';
str1[n+1]='\0';