CPP-基础:String类

已知类String的原型为:
class String
{
public:
  String(const char *str = NULL);                    // 普通构造函数
  String(const String &other);                       // 拷贝构造函数
  ~ String(void);                                    // 析构函数
  String& operator =(const String &other);          // 赋值函数
  String& operator +(const String &other);          //重载运算符+
  String& operator -(const String &other);    //重载运算符-
  bool operator==(const String &other);            //重载"==",判断两个字符串相等 
  bool operator<(const String &other);
  bool operator>(const String &other);
  friend ostream& operator << (ostream &, const String &); //重载<<,这里必须使用friend,因为涉及到两个类之间私有成员的访问

private:
  char *m_data; // 用于保存字符串
};
普通构造函数
String::String(const char *str) 
{   
if(NULL==str)   {     m_data = new char[1]; // 得分点:对空字符串自动申请存放结束标志'\0'的//加分点:对m_data加NULL 判断     if(m_data!=NULL)     {       *m_data = '\0';     }   }   else   {     int length = strlen(str);     m_data = new char[length+1]; // 若能加 NULL 判断则更好     if(m_data!=NULL)     {       strcpy(m_data, str);     }   } }

String的析构函数

String::~String(void)
{
  delete [] m_data;                                        // 或delete m_data;
}
拷贝构造函数
String::String(const String &other)                          // 得分点:输入参数为const型
{     
  int length = strlen(other.m_data);
  m_data = new char[length+1];                             //加分点:对m_data加NULL 判断
  if(m_data!=NULL)
  {  
        strcpy(m_data, other.m_data);  
  }  
}
赋值函数
String& String::operator=(const String &other)              // 得分点:输入参数为const型
{     
  if(this == &other)                                       //得分点:检查自赋值
     return *this;  
  delete [] m_data;
//得分点:释放原有的内存资源   int length = strlen( other.m_data );   m_data = new char[length+1];
//加分点:对m_data加NULL 判断   if(m_data!=NULL) strcpy( m_data, other.m_data );   return *this; //得分点:返回本对象的引用 }
运算符重载+
String& String::operator+(const String &other)               
{
    char temp[200];
    strcpy(temp,m_data); //先把左侧串存放到临时数组。
    int length1 = strlen(this->m_data); //保存左侧串的长度;
    delete [] m_data; //删掉左侧串内存。
    int length2 = strlen(other.m_data);//计算右侧串的长度。
    int length = length1 + length2;//计算左右串总长度。
    m_data=new char[length+1];//把总长度+1,准备接纳拼接后的字符串。
    if(m_data!=NULL)
    {       strcpy(m_data,temp);
    }     strcat(m_data,other.m_data);     
return *this; }

 

运算符重载 -
String& String::operator-(const String &other)
{
     char *temp,*p;
     if( (temp=strstr(m_data,other.m_data)) ==NULL)
     {
         cout<<"没有符合的子串,不能使用'-'操作"<<endl;
         return *this;
     }
     else
     {
         p=temp;
         temp=temp+strlen(other.m_data);
         *p='\0'; //把“o.World!”第一个字符变为'\0',从而让m_data缩减为前半部。
         strcat(m_data,temp);         
     }
     return *this;    
}

 

重载运算符"<<"
ostream& operator<<(ostream &output, const String &other)
{
     output<<other.m_data;
     return output;    
}
重载运算符"<"
bool String::operator<(const String &other)
{
     if(strcmp(m_data,other.m_data)<0)
        return true;
     return false;
}
重载运算符">"
bool String::operator>(const String &other)
{
     if(strcmp(m_data,other.m_data)>0)
        return true;
     return false;
}

运算符重载 ==

bool String::operator==(const String &other)
{
     if(strcmp(m_data,other.m_data)==0)
        return true;
     return false;
}


void main()
{
   String s1("Hello,");    //普通构造函数
     String s2("World!");   //普通构造函数  
     String s3(s2);//调用拷贝构造函数
     String s4(s2);//调用拷贝构造函数     
     String s5("Hello,World"); //普通构造函数
     String s6("Hello,World!");//普通构造函数
    
     String s7("Hello,World!");//普通构造函数
     String s8("o,W");         //普通构造函数
     String s9;                //自动调用普通构造函数
    
     //String s3;//调用构造函数中的默认构造. 
     //s3=s2;// 调用重载后的赋值函数 
    
     cout<<s1<<endl;  //输出对象s1.m_data;
     cout<<s2<<endl;  //输出对象s2.m_data;
     cout<<s3<<endl;  //输出对象s3.m_data;
    
     s3=s1+s2; //调用运算符=重载函数

     cout<<s1<<endl;
     cout<<s2<<endl;   
     cout<<s3<<endl;
    
     if(s4==s2)
       cout<<"两个字符串相等"<<endl;
     else
       cout<<"两个字符串不相等"<<endl;
      
     if(s5<s6)
       cout<<"s5小于s6"<<endl;
     else if(s5>s6)
       cout<<"s5大于s6"<<endl;
     else
       cout<<"s5等于s6"<<endl;
      
     s9=s7-s8;
     cout<<s9<<endl;          
     system("pause");
}

 

转载于:https://www.cnblogs.com/CPYER/p/3296043.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值