01 String类测试题

实现以下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;
		  };
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值