hqyj—C++—day4

自定义字符串类,并进行个别运算符的重载

#include <iostream>
#include <cstring>
using namespace std;

class myString;

class myString
{
public:
    //无参构造
    myString()  ;
    //有参构造
    myString(const char *s);
    //拷贝构造
    myString( const myString &other);
    //拷贝赋值
    myString  operator=(const myString &other);
    //析构构造
    ~myString();
    //判空函数
    bool myempty();
    //size函数
    int mysize();
    //c_str函数
    const char* myc_str();
    //at函数
    char my_at(int n);
    // [] 运算符重载
    char operator[](int n) const;
    // + 运算符重载
    myString operator+(const myString &other);
    // == 运算符重载
    bool operator==(const myString &other);
    // != 运算符重载
    bool operator!=(const myString &other);
    //  < 运算符重载
    bool operator<(const myString &other);
    //  > 运算符重载
    bool operator>(const myString &other);
    //  <= 运算符重载
    bool operator<=(const myString &other);
    //  >= 运算符重载
    bool operator>=(const myString &other);
    //输出字符串函数
    void show();
    //将插入运算符<<设置成友元函数
    friend ostream &operator<<(ostream &l ,const myString &r);
    //将提取运算符<<设置成友元函数
    friend istream &operator>>(istream &l , myString &r);

private:
    int size;
    char *str;
};
//  << 运算符重载
ostream &operator<<(ostream &l ,const myString &r);
//  >> 运算符重载
istream &operator>>(istream &l , myString &r);

int main()
{
    //验证有参构造函数
    myString s1("hello world");
    cout<<"字符串s1:"<<endl;
    s1.show();
    //验证拷贝构造函数
    myString s2(s1);
    cout<<"字符串s2:"<<endl;
    s2.show();
    //验证c_str , at 函数
    char ch[20] ;
    strcpy(ch,s1.myc_str());
    cout<<"ch = "<<ch<<endl;
    cout<<"s1.my_at[1] : "<<s1.my_at(1)<<endl;
    cout<<"s1.my_at[13] : "<<s1.my_at(13)<<endl;
    //验证拷贝赋值函数
    myString s3;
    s3 = "nihao ";
    s3 = s3+s1;
    s3.show();
    //验证 [] 运算符
    cout<<"s3[4] : "<<s3[4]<<endl;
    //验证 == 运算符
    if(s1 == s2)
    {
        cout<<"s1 == s2"<<endl;
    }
    //验证 != 运算符
    if(s3 != s2)
    {
        cout<<"s3 != s2"<<endl;
    }
    //验证 < 运算符
    myString s4("aaaa");
    myString s5("bbbb");
    if(s4 < s5)
    {
        cout<<"s4 < s5"<<endl;
    }
    //验证 > 运算符
    if(s5 > s4)
    {
        cout<<"s5 > s4"<<endl;
    }
    //验证 <= 运算符
    if(s4 <= s5)
    {
        cout<<"s4 <= s5"<<endl;
    }
    //验证 >= 运算符
    if(s1 >= s2)
    {
        cout<<"s1 >= s2"<<endl;
    }
    //验证 <<  >> 运算符
    cout<<s1<<endl;
    cout<<"输入数据:";
    cin>>s2;
    cout<<"输出数据:";
    cout<<s2<<endl;

    return 0;
}

//无参构造
myString::myString():size(20)
{
    str = new char[size];
    strcpy(str,"");
}

//有参构造
myString::myString(const char *s)
{
    size = strlen(s);
    str = new char[size+1];
    strcpy(str,s);
    //cout<<"stu::有参构造,this = "<<this<<endl;
}

//拷贝构造
myString::myString( const myString &other): size(other.size),str(new char[size+1]{*(other.str)})
{
    strcpy(str,other.str);
    //cout<<"stu::拷贝构造,this = "<<this<<endl;
}

//析构构造
myString::~myString()
{
    delete []str;
    //cout<<"stu::析构函数,this = "<<this<<endl;
}

//拷贝赋值
myString  myString:: operator=(const myString &other)
{
    this->size = other.size;
    strcpy(this->str,other.str);
    return *this;
}

//判空函数
bool myString::myempty()
{
    return strlen(str) == 0;
}

//size函数
int myString::mysize()
{
    return strlen(str);
}

//c_str函数
const char* myString::myc_str()
{
    return  str;
}

//at函数
char myString::my_at(int n)
{
    if(n < 0||n >=size)
    {
        cout<<"越界"<<endl;
        return '\0';
    }
    return str[n];
}

// [] 运算符重载
char myString::operator[](int n) const
{
    if(n < 0||n >=size)
    {
        cout<<"越界"<<endl;
        return '\0';
    }
    return str[n];
}

// + 运算符重载
myString myString::operator+(const myString &other)
{
    strcat(this->str,other.str);
    this->size = strlen(this->str);
    return *this;
}

// == 运算符重载
bool myString::operator==(const myString &other)
{
    return  strcmp(this->str,other.str) == 0;
}

// != 运算符重载
bool myString::operator!=(const myString &other)
{
    return  strcmp(this->str,other.str) != 0;
}

//  < 运算符重载
bool myString::operator<(const myString &other)
{
    return  strcmp(this->str,other.str) < 0;
}

//  > 运算符重载
bool myString::operator>(const myString &other)
{
    return  strcmp(this->str,other.str) > 0;
}

//  <= 运算符重载
bool myString::operator<=(const myString &other)
{
    return  strcmp(this->str,other.str) <= 0;
}

//  >= 运算符重载
bool myString::operator>=(const myString &other)
{
    return  strcmp(this->str,other.str) >= 0;
}

//输出字符串函数
void myString::show()
{
    cout<<"size = "<<size<<endl;
    cout<<"str = "<<str<<endl;
}

//  << 运算符重载
ostream &operator<<(ostream &l ,const myString &r)
{
    l<<r.str;
    return  l;
}

//  >> 运算符重载
istream &operator>>(istream &l , myString &r)
{
    l>>r.str;
    return  l;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值