1 回答
TA贡献1802条经验 获得超5个赞
#include<iostream>
using namespace std;
class String;
istream& operator>>(istream& in,String& s);
ostream& operator<<(ostream& out,String& s);
class String
{
public:
enum {MaxSize=100};
friend istream& operator>>(istream& in,String& s);
friend ostream& operator<<(ostream& out,String& s);
String()
{
str=new char[MaxSize];
Size=0;
}
String(String& s);
String& operator+(String& s);
void operator=(char *s);
void operator=(String& s);
char operator[](int i);
bool empty()
{
if(Size==0)
return 1;
else
return 0;
}
int size() { return Size;}
int length() {return Size;}
int max_size() {return MaxSize;}
~String() {delete[] str;}
private:
char *str;
int Size;
};
String::String(String& s)
{
str=new char[MaxSize];
Size=0;
*this=s;
}
String& String::operator+(String& s2)
{
static String s1;
s1=*this;
s1=s1+s2;
return s1;
}
void String::operator=(char *s)
{
int i=0;
Size=0;
while(s[i]!='\0')
str[Size++]=s[i++];
}
void String::operator=(String& s)
{
int i;
Size=0;
for(i=0;i<s.Size;i++)
str[Size++]=s[i];
}
char String::operator[](int i)
{
return str[i];
}
istream& operator>>(istream& in,String& s)
{
char c[s.MaxSize];
gets(c);
s=c;
return in;
}
ostream& operator<<(ostream& out,String& s)
{
int i;
for(i=0;i<s.Size;i++)
out<<s.str[i];
return out;
}
int main()
{
system("pause");
return 0;
}
这是我写的String类的一部分
添加回答
举报