在My_string类的基础上,完成运算符重载

文章详细展示了如何在C++中为自定义的My_string类实现各种运算符重载,包括算术运算符(+),赋值运算符(+=),下标运算符([]),关系运算符(>,<,==,!=)以及输入输出运算符(<<,>>)。同时强调了数据的保护,使用const进行修饰。
摘要由CSDN通过智能技术生成

在My_string类的基础上,完成运算符重载

算术运算符:+

赋值运算符:+=

下标运算符:[]

关系运算符:>、=、

插入提取运算符:>

要求:注意数据的保护(const) 

头文件

#ifndef HEAD_H
#define HEAD_H
#include <iostream>

using namespace std;

class My_string
{
private:
    char *data;
    int size;
public:
    //无参构造默认长度为15
    My_string();
    //有参构造1
    My_string(const char *str);
    //有参构造2
    My_string(int n, char ch);
    //析构函数
    ~My_string();
    //拷贝构造函数
    My_string(const My_string &other);
    //拷贝赋值函数
    My_string &operator=(const My_string &other);
    //c_str函数
    char* My_str();
    //size函数
    int My_size();
    //empty函数
    bool My_empty();
    //at函数
    char &M_at(int n);
    //+运算符
    const My_string operator+(const My_string &R)const;
    //+=运算符
    My_string &operator+=(const My_string &R);
    //[]运算符
    char &operator[](int num)const;
    //>运算符
    bool operator>(const My_string &R)const;
    //<运算符
    bool operator<(const My_string &R)const;
    //==运算符
    bool operator==(const My_string &R)const;
    //!=运算符
    bool operator!=(const My_string &R)const;
    //>=运算符
    bool operator>=(const My_string &R)const;
    //<=运算符
    bool operator<=(const My_string &R)const;
    //<<插入运算符
    friend ostream &operator<<(ostream &L,const My_string &R);
    //>>提取运算符
    friend istream &operator>>(istream &L,const My_string &R);
    
};

#endif // HEAD_H

 源文件

#include <iostream>
#include <cstring>
#include <head.h>

using namespace std;

//无参构造默认长度为15
My_string::My_string():size(15)
{
    data = new char[size];
    data[0] = '\0';
}

//有参构造1
My_string::My_string(const char *str):data(new char[strlen(str)+1]),size(strlen(str))
{
    strcpy(data,str);
}
//有参构造2
My_string::My_string(int n, char ch):data(new char[n+1]),size(n)
{
    for(int i=0;i<n;i++){
        data[i] = ch;
    }
    data[n] = '\0';
}
//析构函数
My_string::~My_string(){
    delete []data;
}
//拷贝构造函数
My_string::My_string(const My_string &other):data(new char[other.size+1]),size(other.size){
    strcpy(data,other.data);
    
}
//拷贝赋值函数
My_string & My_string::operator=(const My_string &other){
    delete []data;
    data = new char[other.size+1];
    strcpy(this->data,other.data);
    this->size = other.size;
    return  *this;
}
//c_str函数
char* My_string::My_str(){
    return this->data;
}
//size函数
int My_string::My_size(){
    return strlen(this->data);
}
//empty函数
bool My_string::My_empty(){
    return data[0] == '\0';
}
//at函数
char &My_string::M_at(int n){
    return this->data[n];
}

//+运算符
const My_string My_string::operator+(const My_string &R)const{
    My_string temp;
    delete []temp.data;
    temp.size = this->size + R.size;
    temp.data = new char[temp.size+1];
    strcpy(temp.data,this->data);
    strcat(temp.data,R.data);
    temp.data[temp.size] = '\0';
    return temp;
}

//+=运算符
My_string &My_string::operator+=(const My_string &R){
    this->size = strlen(this->data) + strlen(R.data);
    char *temp = new char [this->size+1];
    strcpy(temp,this->data);
    strcat(temp,R.data);
    temp[this->size] = '\0';
    delete []this->data;
    this->data = temp;
    return *this;
}
//>运算符
bool My_string::operator>(const My_string &R)const{
    return strcmp(this->data,R.data) > 0;
}
//<运算符
bool My_string::operator<(const My_string &R)const{
    return strcmp(this->data,R.data) < 0;
}
//==运算符
bool My_string::operator==(const My_string &R)const{
    return strcmp(this->data,R.data) == 0;
}
//!=运算符
bool My_string::operator!=(const My_string &R)const{
    return strcmp(this->data,R.data) != 0;
}
//>=运算符
bool My_string::operator>=(const My_string &R)const{
    return strcmp(this->data,R.data) >= 0;
}
//<=运算符
bool My_string::operator<=(const My_string &R)const{
    return strcmp(this->data,R.data) <= 0;
}
//[]运算符
char &My_string::operator[](int num)const{
    if(num < 0 || num > this->size){
        cout << "数组下标访问越界" << endl;
    }
    return this->data[num];
}
//<<插入运算符
ostream &operator<<(ostream &L,const My_string &R){
    L << R.data ;
    return L;
}
//>>提取运算符
istream &operator>>(istream &L,const My_string &R){
    L >> R.data;
    return L;
}

主函数文件

#include <iostream>
#include <cstring>
#include <head.h>

using namespace std;

int main()
{
    My_string s1("hello world");
    cout << "s1 = " << s1 << endl;
    My_string s2("China No.1");
    cout << "s2 = " << s2 << endl;
    My_string s3;
    s3 = s1+s2;
    cout << "+运算符: " <<"s3 = " << s3 << endl;
    My_string s4="figthting";
    s4+=s3;
    cout << "+=运算符: " << "s4 = " << s4 << endl;
    cout << "[]运算符: " <<"s4[6] = " << s4[6] << endl;
    if(s1 > s2){
        cout << ">运算符:" <<"s1大" << endl;
    }else{
        cout << ">运算符:" <<"s2大" << endl;
    }
    if(s1 < s2){
        cout << "<运算符:" <<"s1小" << endl;
    }else{
        cout << "<运算符:" <<"s2小" << endl;
    }
    if(s1 == s2){
        cout << "==运算符:" <<"相等" << endl;
    }else{
        cout << "==运算符:" <<"不相等" << endl;
    }
    if(s1 != s2){
        cout << "!=运算符:" <<"不相等" << endl;
    }else{
        cout << "!=运算符:" <<"相等" << endl;
    }
    if(s1 >= s2){
        cout << ">=运算符:" <<"s1大于等于" << endl;
    }else{
        cout << ">=运算符:" <<"s2大于等于" << endl;
    }
    if(s1 <= s2){
        cout << "<=运算符:" <<"s1小于等于" << endl;
    }else{
        cout << "<=运算符:" <<"s2小于等于" << endl;
    }
    My_string s5;
    cout << "提取运算符>>s5: ";
    cin >> s5;
    cout << "s5 = " << s5 << endl;
    
    return 0;
}

运行结果

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值