重载运算符实现精简版string类

c语言利用数组保存字符串,经常在不经意中浪费了大量的空间,使用起来十分的不方便,而且容易出错,为了解决c语言字符串的问题,c++增加了一个string类。本例就是为了说明通过运算符的重载而实现的。

代码如下:


  1. #include<iostream> 
  2. using namespace std; 
  3. class String 
  4. public
  5.     String(); 
  6.     ~String(); 
  7.     String(const String &rs); 
  8.     String(const char* const ch); 
  9.     char &operator[](unsigned short int length ); 
  10.     char operator[](unsigned short int  length) const
  11.  
  12.     String &operator=(const String &s); 
  13.     String operator+(const String &); 
  14.     void operator+=(const String &rs); 
  15.  
  16.     friend ostream &operator<<(ostream & o,const String &str) 
  17.     { 
  18.         o<<str.str; 
  19.         return o; 
  20.      
  21.          
  22.      
  23.     } 
  24.  
  25.     friend istream &operator>>(istream &i,String str) 
  26.     { 
  27.      
  28.         i>>str.str; 
  29.         return i; 
  30.     } 
  31.  
  32.     friend bool operator<(const String &str1,const String &str2) 
  33.     { 
  34.      
  35.         if(strcmp(str1.str,str2.str)<0) 
  36.             return 1; 
  37.         else  
  38.             return 0; 
  39.     } 
  40.  
  41.     friend bool operator>(const String &str1,const String &str2) 
  42.     { 
  43.      
  44.         if(strcmp(str1.str,str2.str)>0) 
  45.             return 1; 
  46.         else  
  47.             return 0; 
  48.     } 
  49.  
  50.         friend bool operator==(const String &str1,const String &str2) 
  51.     { 
  52.      
  53.         if(strcmp(str1.str,str2.str)==0) 
  54.             return 1; 
  55.         else  
  56.             return 0; 
  57.     } 
  58.  
  59.         unsigned short int getlen() const {return len;} 
  60.         const char* getstr() const{return str;} 
  61.  
  62.  
  63.  
  64. private
  65.     String(unsigned short int); 
  66.     unsigned short int len; 
  67.     char *str; 
  68.  
  69.  
  70.  
  71.  
  72. }; 
  73.  
  74. String::String(unsigned short int length) 
  75.  
  76.     str = new char[length + 1]; 
  77.     int i; 
  78.     for(i = 0;i <length;i++) 
  79.         str[i] = '\0'
  80.     len = length; 
  81.  
  82. String::String() 
  83.  
  84.     len = 0; 
  85.     str = new char[1]; 
  86.  
  87.     str[0] = '\0'
  88.  
  89.  
  90. String::~String() 
  91.  
  92.     delete []str; 
  93.     len = 0; 
  94.  
  95. String::String(const String &rs) 
  96.  
  97.     len = rs.getlen(); 
  98.     str = new char[len + 1]; 
  99.     for(int i =0 ;i < len ;i++ ) 
  100.         str[i] = rs[i]; 
  101.     str[len] = '\0'
  102.  
  103.  
  104.  
  105. char &String ::operator [](unsigned short int length) 
  106.  
  107.     if(length > len) 
  108.         return str[len - 1]; 
  109.     else 
  110.         return str[length]; 
  111.  
  112.  
  113.  
  114. char String::operator [](unsigned short int length) const 
  115.  
  116.         if(length > len) 
  117.         return str[len - 1]; 
  118.     else 
  119.         return str[length]; 
  120.  
  121.      
  122.  
  123.  
  124. String &String::operator =(const String &s) 
  125.  
  126.     if(this == &s) 
  127.     return *this
  128.     delete []str; 
  129.     len = s.getlen(); 
  130.     str = new char[len + 1]; 
  131.     for(int i=0;i<len;i++) 
  132.     { 
  133.      
  134.         str[i] = s[i]; 
  135.  
  136.     } 
  137.     str[len] = '\0'
  138.  
  139.     return *this
  140.  
  141.  
  142.  
  143.  
  144.  
  145. String String::operator +(const String &rs) 
  146.  
  147.     int total = len + rs.getlen(); 
  148.     String temp(total); 
  149.     int i,j; 
  150.     for( i = 0;i < len;i++) 
  151.         temp[i] = str[i]; 
  152.     for(j = 0;j<rs.getlen();j++,i++) 
  153.         temp[i] = rs[j]; 
  154.     temp[total] = '\0'
  155.      
  156.     return temp; 
  157.  
  158.  
  159.  
  160. void String::operator +=(const String &rs) 
  161.  
  162.     int total = len + rs.getlen(); 
  163.     String temp(total); 
  164.     int i,j; 
  165.     for(i = 0;i < len;i++) 
  166.         temp[i] = str[i]; 
  167.     for(j = 0;j < rs.getlen();j++,i++) 
  168.         temp[i] = rs[j]; 
  169.     temp[total] = '\0'
  170.     *this = temp; 
  171.  
  172.  
  173. String::String(const char* const ch) 
  174.  
  175.     len = strlen(ch); 
  176.     str = new char[len+1]; 
  177.     for(int i = 0;i < len;i++) 
  178.         str[i] = ch[i]; 
  179.     str[len] = '\0'
  180.  
  181.  
  182. int main() 
  183.  
  184.     String s1; 
  185.     cout<<"s1的长度:"<<s1.getlen()<<endl; 
  186.     char*temp = "help me"
  187.     s1 = temp; 
  188.     cout<<"s1: "<<s1.getstr()<<"\t s1的长度: "<<s1.getlen()<<endl; 
  189.     char ch[10]; 
  190.     strcpy(ch,"all right"); 
  191.     s1 += ch; 
  192.     cout<<"ch:\t"<<ch<<endl; 
  193.     cout<<"s1: \t"<<s1.getstr()<<endl; 
  194.     cout<<"s1:"<<s1<<endl; 
  195.  
  196.     s1[2] = 'o'
  197.     cout<<"s1:"<<s1<<endl; 
  198.     cout<<"s1[999]: "<<s1[999]<<endl; 
  199.     String s2 = "mother"
  200.  
  201.     String s3("Mother"); 
  202.     cout<<"s2: "<<s2<<"\t s3: "<<s3<<endl; 
  203.     String s4 = s2 + s3; 
  204.     cout<<" s2 + s3 = "<<s4<<endl; 
  205.     int check = s2>s3; 
  206.     cout<<"s2>s3: "<<check<<endl; 
  207.     check = s2 < s3; 
  208.     cout<<"s2<s3: "<<check<<endl; 
  209.  
  210.     check = s2 == s2; 
  211.     cout<<"s2 == s2: "<<check<<endl; 
  212.     cin>>s2[0]>>s3[0]; 
  213.     s2 = s2 + s3; 
  214.     cout<<"s2: "<<" \t"<<s2<<endl; 
  215.  
  216.     return 0; 
#include<iostream>
using namespace std;
class String
{
public:
	String();
	~String();
	String(const String &rs);
	String(const char* const ch);
	char &operator[](unsigned short int length );
	char operator[](unsigned short int  length) const;

	String &operator=(const String &s);
	String operator+(const String &);
	void operator+=(const String &rs);

	friend ostream &operator<<(ostream & o,const String &str)
	{
		o<<str.str;
		return o;
	
		
	
	}

	friend istream &operator>>(istream &i,String str)
	{
	
		i>>str.str;
		return i;
	}

	friend bool operator<(const String &str1,const String &str2)
	{
	
		if(strcmp(str1.str,str2.str)<0)
			return 1;
		else 
			return 0;
	}

	friend bool operator>(const String &str1,const String &str2)
	{
	
		if(strcmp(str1.str,str2.str)>0)
			return 1;
		else 
			return 0;
	}

		friend bool operator==(const String &str1,const String &str2)
	{
	
		if(strcmp(str1.str,str2.str)==0)
			return 1;
		else 
			return 0;
	}

		unsigned short int getlen() const {return len;}
		const char* getstr() const{return str;}



private:
	String(unsigned short int);
	unsigned short int len;
	char *str;




};

String::String(unsigned short int length)
{

	str = new char[length + 1];
	int i;
	for(i = 0;i <length;i++)
		str[i] = '\0';
	len = length;
}

String::String()
{

	len = 0;
	str = new char[1];

	str[0] = '\0';

}

String::~String()
{

	delete []str;
	len = 0;
}

String::String(const String &rs)
{

	len = rs.getlen();
	str = new char[len + 1];
	for(int i =0 ;i < len ;i++ )
		str[i] = rs[i];
	str[len] = '\0';


}

char &String ::operator [](unsigned short int length)
{

	if(length > len)
		return str[len - 1];
	else
		return str[length];


}

char String::operator [](unsigned short int length) const
{

		if(length > len)
		return str[len - 1];
	else
		return str[length];

	

}

String &String::operator =(const String &s)
{

	if(this == &s)
	return *this;
	delete []str;
	len = s.getlen();
	str = new char[len + 1];
	for(int i=0;i<len;i++)
	{
	
		str[i] = s[i];

	}
	str[len] = '\0';

	return *this;




}

String String::operator +(const String &rs)
{

	int total = len + rs.getlen();
	String temp(total);
	int i,j;
	for( i = 0;i < len;i++)
		temp[i] = str[i];
	for(j = 0;j<rs.getlen();j++,i++)
		temp[i] = rs[j];
	temp[total] = '\0';
	
	return temp;


}

void String::operator +=(const String &rs)
{

	int total = len + rs.getlen();
	String temp(total);
	int i,j;
	for(i = 0;i < len;i++)
		temp[i] = str[i];
	for(j = 0;j < rs.getlen();j++,i++)
		temp[i] = rs[j];
	temp[total] = '\0';
	*this = temp;

}

String::String(const char* const ch)
{

	len = strlen(ch);
	str = new char[len+1];
	for(int i = 0;i < len;i++)
		str[i] = ch[i];
	str[len] = '\0';
}


int main()
{

	String s1;
	cout<<"s1的长度:"<<s1.getlen()<<endl;
	char*temp = "help me";
	s1 = temp;
	cout<<"s1: "<<s1.getstr()<<"\t s1的长度: "<<s1.getlen()<<endl;
	char ch[10];
	strcpy(ch,"all right");
	s1 += ch;
	cout<<"ch:\t"<<ch<<endl;
	cout<<"s1: \t"<<s1.getstr()<<endl;
	cout<<"s1:"<<s1<<endl;

	s1[2] = 'o';
	cout<<"s1:"<<s1<<endl;
	cout<<"s1[999]: "<<s1[999]<<endl;
	String s2 = "mother";

	String s3("Mother");
	cout<<"s2: "<<s2<<"\t s3: "<<s3<<endl;
	String s4 = s2 + s3;
	cout<<" s2 + s3 = "<<s4<<endl;
	int check = s2>s3;
	cout<<"s2>s3: "<<check<<endl;
	check = s2 < s3;
	cout<<"s2<s3: "<<check<<endl;

	check = s2 == s2;
	cout<<"s2 == s2: "<<check<<endl;
	cin>>s2[0]>>s3[0];
	s2 = s2 + s3;
	cout<<"s2: "<<" \t"<<s2<<endl;

	return 0;
}


运行的结果是:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值