实现以下String类并测试这个类。
class String
{
private:
char * s;
public:
String();
String(const char *);
String(const String &);
~String();
String & operator=(const char *);
String & operator=(const String &);
String operator+(const char *);
String operator+(const String &);
String & operator+=(const char *);
String & operator+=(const String &);
friend istream & operator>>(istream &, String &);
friend ostream & operator<<(ostream &, const String &);
friend bool operator==(const String &, const char *);
friend bool operator==(const String &, const String &);
friend bool operator!=(const String &, const char *);
friend bool operator!=(const String &, const String &);
};
使用以下的main函数进行测试:
int main()
{
String s;
s += "hello";
cout<<s<<endl;
String s1("String1");
String s2("copy of ");
s2 += "String1";
cout << s1 << "\n" << s2 << endl;
String s3;
cin >> s3;
cout << s3 << endl;
String s4("String4"), s5(s4);
cout << (s5 == s4) << endl;
cout << (s5 != s4) << endl;
String s6("End of "), s7("my string.");
s6 += s7;
cout << s6 << endl;
return 0;
}
输入
s3的值
输出
一连串String类的字符串,详见样例
输入样例 1
String3
输出样例 1
hello String1 copy of String1 String3 1 0 End of my string.
#include<iostream>
#include<cstring>
using namespace std;
#include <iostream>
#include <cstring>
using namespace std;
class String
{
private:
char * s;
public:
String()//空构造函数
{
s=new char[1];
s[0]='\0'; //!!!
};
String(const char *a)//构造函数
{
int length=strlen(a);
s=new char[length+1];
strcpy(s,a);
};
String(const String &a)//拷贝构造函数
{
int length=strlen(a.s);
this->s=new char[length+1];
strcpy(this->s,a.s);
};
~String()
{
delete [] s;
};
String & operator=(const char *a)
{
delete [] s;
s=new char[strlen(a)+1];
strcpy(this->s, a);
return *this;
};
String & operator=(const String &a)
{
delete [] s;
s=new char[strlen(a.s)+1];
strcpy(this->s, a.s);
return *this;
};
String operator+(const char *a)//为什么不能用*this.s
{
String t;
delete[] t.s;
// t=*this;
int length=strlen(s)+strlen(a);
t.s=new char[length+1];
strcpy(t.s,s);
strcat(t.s,a);
return t;
};
String operator+(const String &a)
{
String t;
delete[] t.s;
// t=*this;
int length=strlen(s)+strlen(a.s);
t.s=new char[length+1];
strcpy(t.s,s);
strcat(t.s,a.s);
return t;
};
String & operator+=(const char *a)
{
// String t(a);
*this=*this+a;
return *this;
};
String & operator+=(const String &a)
{
*this=*this+a;
return *this;
};
friend istream & operator>>(istream &is, String &a)
{
char temp[255];
is >> temp;
a = temp;
return is;
};
friend ostream & operator<<(ostream &os, const String &a)
{
os<<a.s;
return os;
};
friend bool operator==(const String &a, const char *b)
{
String t(b);
if(t.s==a.s)
return true;
else
return false;
};
friend bool operator==(const String &a, const String &b)
{
if(strcmp(a.s,b.s)==0)
return true;
else
return false;
};
friend bool operator!=(const String &a, const char *b)
{
String t(b);
if(strcmp(a.s,t.s)==1)
return true;
else
return false;
};
friend bool operator!=(const String &a, const String &b)
{
if(strcmp(a.s,b.s)==1)
return true;
else
return false;
};
};