字符串类中的运算符重载

#ifndef _STRING_H_
#define _STRING_H_
#include <iostream>
using namespace std;
class String
{
private:
    int m_length;
    char *m_ptr;
public:
    String();
    String(char *str);
    String(int l,char ch);
    String(const String &s);
    ~String();
    friend ostream &operator <<(ostream &out,const String &s);
    friend istream &operator >>(istream &in, String &s);
    String &operator =(const String &s1);
    String &operator =(char *p);
    char &operator [](int index);
    String operator +(const String &s);
    String operator +(char *p);
    String operator +=(const String &s);
    String operator +=(char *p);
    int operator ==(const String &s);
    //char &operator ==(char *p);
    int operator !=(const String &s);
    //char &operator !=(char *p);
    int operator <(const String &s);
    int operator >(const String &s);
};
#endif




#include "string.h"
#include <cstring>
#include <cstdlib>
#include <iostream>
using namespace std;
String::String()
{
    m_length = 0;
    m_ptr = NULL;
}
String::String(char *str)
{
    m_length = strlen(str);
    m_ptr = new char[m_length + 1];
    strcpy(m_ptr,str);
    m_ptr[m_length] = '\0';
}
String::String(int l,char ch)
{
    m_length = l;
    m_ptr = new char [m_length + 1];
    for(int i = 0;i < m_length;i ++)
    {
        m_ptr[i] = ch;
    }
    m_ptr[m_length] = '\0';
}
String::String(const String &s)
{
    m_length = s.m_length;
    m_ptr = new char[m_length + 1];
    strcpy(m_ptr,s.m_ptr);
    m_ptr[m_length] = '\0';
}
String::~String()
{
    if(m_ptr)
    {
        delete[] m_ptr;
        m_ptr = NULL;
    }
}
//重载"<<"运算符
ostream &operator <<(ostream &out,const String &s)
{
    out << s.m_ptr;
    return out;
}
//重载">>"运算符
istream &operator >>(istream &in, String &s)
{
    char temp[200];
    in >> temp;
    s.m_length = strlen(temp);
    s.m_ptr = new char[s.m_length + 1];
    strcpy(s.m_ptr,temp);
    s.m_ptr[s.m_length] = '\0';
    return in;
}
//重载"="运算符
String &String::operator =(const String &s1)
{
    m_length = s1.m_length;
    if(this == &s1)
    {
        return *this;
    }
    if(m_ptr != NULL)
    {
        delete[] m_ptr;
    }
    if(s1.m_ptr)
    {
        m_ptr = new char[m_length + 1];
        strcpy(m_ptr,s1.m_ptr);
        cout << s1.m_ptr << endl;
        m_ptr[m_length] = '\0';
        cout << m_ptr << endl;
    }
    else
    {
        m_ptr = NULL;
    }
    return *this;
}
String &String::operator =(char *p)
{
    /*if(m_ptr != NULL)
    {
        delete[] m_ptr;
    }
    m_length = strlen(p);
    m_ptr = new char(m_length + 1);
    strcpy(m_ptr,p);
    m_ptr[m_length] = '\0';*/
    *this = String(p);
    return *this;
}
//重载"[]"运算符
char &String::operator [](int index)
{
    if(index < 0 || index >= m_length)
    {
        cout << "out of range" << endl;
    }
    return m_ptr[index];
}
//重载"+"运算符
String String::operator +(const String &s)
{
    String t;
    m_length = strlen(m_ptr) + strlen(s.m_ptr);
    if(m_length > 0)
    {
        t.m_ptr = new char[m_length + 1];
        strcpy(t.m_ptr,m_ptr);
        strcat(t.m_ptr,s.m_ptr);
        t.m_ptr[m_length] = '\0';
    }
    else
    {
        t.m_ptr = 0;
    }
    return t;
}
String String::operator +(char *p)
{
    String t;
    m_length = strlen(m_ptr) + strlen(p);
    if(m_length > 0)
    {
        t.m_ptr = new char[m_length + 1];
        strcpy(t.m_ptr,m_ptr);
        strcat(t.m_ptr,p);
        t.m_ptr[m_length] = '\0';
    }
    else
    {
        t.m_ptr = 0;
    }
    return t;
    /*cout << "111" << endl;
    String t(p);
    cout << "222" << endl;
    t = *this + t;
    cout << "333" << endl;
    return t;*/
}
//重载"+="运算符
String String::operator +=(const String &s)
{
    /*char temp[100];
    strcpy(temp,m_ptr);
    strcat(temp,s.m_ptr);
    return temp;*/
    /*String t;
    m_length = strlen(m_ptr) + strlen(s.m_ptr);
    if(m_length > 0)
    {
        t.m_ptr = new char[m_length + 1];
        strcpy(t.m_ptr,m_ptr);
        strcat(t.m_ptr,s.m_ptr);
        t.m_ptr[m_length] = '\0';
    }
    else
    {
        t.m_ptr = 0;
    }
    return t;*/
    cout << "111" << endl;
    char *temp = m_ptr;
    m_ptr = new char[strlen(temp) + strlen(s.m_ptr) + 1];
    strcpy(m_ptr,temp);
    strcat(m_ptr,s.m_ptr);
    delete[] temp;
    cout << "111" << endl;
    return *this;
}
String String::operator +=(char *p)
{
    char *temp = m_ptr;
    m_ptr = new char[strlen(temp) + strlen(p) + 1];
    strcpy(m_ptr,temp);
    strcat(m_ptr,p);
    delete[] temp;
    return *this;
    /*String t(p);
    *this += t;
    return *this;*/
}
//重载"=="运算符
int String::operator ==(const String &s)
{
    return (strcmp(m_ptr,s.m_ptr) == 0);
}
/*String &String::operator ==(char *p)
{
    return (strcmp(m_ptr,p) == 0);
}*/
//重载"!="运算符
int String::operator !=(const String &s)
{
    return (strcmp(m_ptr,s.m_ptr) != 0);
}
/*char &String::operator !=(char *p)
{
    return (strcmp(m_ptr,p) != 0);
}*/
//重载"<"运算符
int String::operator <(const String &s)
{
    return strcmp(m_ptr,s.m_ptr) < 0;
}
//重载">"运算符
int String::operator >(const String &s)
{
    return strcmp(m_ptr,s.m_ptr) > 0;
}


#include "string.h"
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
    String s1("helloworld");
    String s2(2,'b');
    String s3(s1);
    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    String s4 = s1;
    cout << s4 << endl;
    String s5 = "hellow";
    cout << s5 << endl;
    cout << s5[3] << endl;
    String s6 = s1 + s2;
    cout << s6 << endl;
    String s7 = s5 + "everyone";
    cout << s7 << endl;
    s1 += s2;
    cout << s1 << endl;
    s1 += "wshr";
    cout << s1 << endl;
    if(s1 == s2)
    {
        cout << "s1 == s2" << endl;
    }
    else
    {
        cout << "s1 != s2" << endl;
    }
    if(s1 != s2)
    {
        cout << "s1 != s2" << endl;
    }
    if(s1 < s2)
    {
        cout << "s1 < s2" << endl;
    }
    if(s1 > s2)
    {
        cout << "s1 > s2" << endl;
    }


    return 0;
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值