#in
#ifndef STRING_H
#define STRING_H
#include<cstring>
#include<iostream>
using namespace std;
class String
{
public:
String();
~String();
void clear();
String(const String ©1);
String(const char * copy);
void operator =(const String ©1);
void operator =(const char*copy);
const char *c_str()const;
protected:
char *entries;
int length;
};
bool operator ==(const String &first,const String &second);
bool operator >(const String &first,const String &second);
bool operator <(const String &first,const String &second);
bool operator >=(const String &first,const String &second);
bool operator <=(const String &first,const String &second);
bool operator !=(const String &first,const String &second);
void strcat(String &add_to,const String &add_on);
String operator + (const String &text,const String &target);
void write(String &s);
int strstr(const String &text,const String &target);
ostream & operator<<(ostream &output,const String &s);
#endif // STRING_H
```tput;
}
1.
```c++
#include "String.h"
String::String(){
entries=NULL,length=0;
}
String::~String(){
delete [] entries;
entries=NULL;
}
String:: String(const String ©1){
const char *copy=copy1.c_str();
int x=strlen(copy1.entries);
if(entries!=NULL) clear();
entries=new char[x+1];
length=x;
strcpy(entries,copy);
}
void String::clear(){
delete [] entries;
entries=NULL;
length=0;
}
String:: String(const char * copy){
int x=strlen(copy);
if(entries!=NULL) clear();
entries=new char[x+1];
length=x;
strcpy(entries,copy);
}
void String:: operator=(const String ©1){
if (this!= ©1) {
if(entries!=NULL) clear();
entries = new char[strlen(copy1.entries) + 1];
strcpy(entries,copy1.entries);
}
}
void String:: operator=(const char*copy){
int x=strlen(copy);
if(entries!=NULL) clear();
entries=new char[x+1];
length=x;
strcpy(entries,copy);
}
const char * String:: c_str()const{
return entries;
}
bool operator ==(const String &first,const String &second){
const char *a=first.c_str(), *b=second.c_str();
int c=strcmp(a,b);
if(c==0) return 1;
else return 0;
}
bool operator >(const String &first,const String &second){
const char *a=first.c_str(), *b=second.c_str();
int c=strcmp(a,b);
if(c>0) return 1;
else return 0;
}
bool operator <(const String &first,const String &second){
const char *a=first.c_str(),*b=second.c_str();
int c=strcmp(a,b);
if(c<0) return 1;
else return 0;
}
bool operator >=(const String &first,const String &second){
const char *a=first.c_str(), *b=second.c_str();
int c=strcmp(a,b);
if(c>=0) return 1;
else return 0;
}
bool operator <=(const String &first,const String &second){
const char *a=first.c_str(),*b=second.c_str();
int c=strcmp(a,b);
if(c<=0) return 1;
else return 0;
}
bool operator !=(const String &first,const String &second){
const char *a=first.c_str(), *b=second.c_str();
int c=strcmp(a,b);
if(c!=0) return 1;
else return 0;
}
void strcat(String &add_to,const String &add_on){
const char *a=add_to.c_str(),*b=add_on.c_str();
char*c=new char[strlen(a)+strlen(b)+1];
strcat(c,a);
strcat(c,b);
add_to=c;
}
String operator + (const String &text,const String &target){
String c;
c="";
strcat(c,text);
strcat(c,target);
return c;
}
void write(String &s){
cout<<s.c_str();}
int strstr(const String &text,const String &target){
const char *a=text.c_str(),*b=target.c_str();
int c=strstr(a,b)-a;
return c;
}
ostream & operator<<(ostream &output,const String &s){
output<<s.c_str();
return output;
}
#include"String.cpp"
int main()
{String s1="s1 string";
cout<<s1.c_str()<<endl;
cout<<s1;
cout<<strlen(s1.c_str())<<endl;
String s2("s2 string");
cout<<s2.c_str()<<endl;
cout<<s2;
cout<<strlen(s2.c_str())<<endl;
cout<<s1+s2;
if(s1>=s2) cout<<"s1>=s2"<<endl;
else cout<<"s1<s2"<<endl;
String s3="s3 string";
strcat(s3,s1);
write(s3);
String text="This is text.";
String target="text";
cout<<strstr(text,target)<<endl;
cin.get();
return 0;
}
为什么输出不了,而且一直显示 3221226356;
但是如果把输入只用一个,比如只s1,或s2,却能出出正确的。