C++ 实现运算符重载

代码:

cpp 复制代码
#include <iostream>
#include <cstring>
 
using namespace std;
 
class myString
{
private:
    char *str;          //记录c风格的字符串
    int size;            //记录字符串的实际长度
public:
    
    //无参构造
    myString():size(10)
    {
        str = new char[size];         //构造出一个长度为10的字符串
        strcpy(str,"");         //赋值为空串
    }
 
    //有参构造
    myString(const char *s)          //string  s("hello world")
    {
        size = strlen(s)+1;
        str = new char[size];
        strcpy(str, s);
    }
 
    //拷贝构造
    myString(char *s,int i):str(new char(*s)),size(i){
        strcpy(this->str,s);
        this->size = i;
        cout<<"拷贝构造函数"<<endl;
    }
 
  //析构函数
    ~myString(){
        delete str;
    }
  
    //拷贝赋值函数
    myString &operator = (const myString &other){
        if(&other != this){
            this->str = new char[size];
            memset(str,0,size);
            strcpy(str,other.str);
        }
        return *this;
    }
 
  //判空函数
    bool empty(){
        if(strlen(str) == 0)
            return false;
        else
            return true;
    }
 
  //size函数
    int mysize(){
        return size;
    }
 
  //c_str函数
    const char* c_str(){
        return str;
    }
 
  //at函数
    char &at(int pos){
        if(pos>=size || pos<0){
            cout<<"访问越界"<<endl;
        }
        return str[pos];
    }
 
  //加号运算符重载
    const myString operator+(const myString &s)const{
       myString m;
       strcpy(m.str,this->str);
       strcat(m.str,s.str);
       m.size = strlen(m.str);
       return m;
    }
 
  //加等于运算符重载
    const myString operator+=(const myString &s){
        strcat(this->str,s.str);
        this->size = strlen(this->str);
        return *this;
    }
 
  //关系运算符重载(>)
    bool operator >(const myString &s)const{
        if(this->size < s.size){
            return false;
        }
        else{
            for(int i=0;i<this->size;i++){
                if(*(this->str+i)<*(s.str+i)){
                    return false;
                }
            }
        }
        return true;
    }
 
  //中括号运算符重载
    char & operator[](const int pos)const{
        if(pos>=size || pos<0){
            cout<<"访问越界"<<endl;
        }
        return *(str+pos);
    }
};
 
 
int main(){
 
    //展示字符串
    myString s1("ssssyyyy");
    myString s2("wwwwhhhh");
    cout<<s1.c_str()<<"     "<<s2.c_str()<<endl;
 
    //重载加法运算符
    myString s3;
    cout<<s3.mysize()<<endl;
    s3=s1+s2;
    cout<<s3.c_str()<<endl;
 
    //at运算
    cout<<s3.at(3)<<endl;
 
    //加等于
    s3+=s1;
    cout<<s3.c_str()<<endl;
    cout<<s3.mysize()<<endl;
 
    //关系运算
    if(s3>s1){
        cout<<"s3>s1"<<endl;
    }
    else
        cout<<"s3<s1"<<endl;
 
    //中括号
    cout<<s2[3]<<"     "<<s2[6]<< "     "<<s2[8]<<endl;
 
 
 
    return 0;
}
相关推荐
blasit1 天前
笔记:Qt C++建立子线程做一个socket TCP常连接通信
c++·qt·tcp/ip
肆忆_2 天前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星2 天前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛4 天前
delete又未完全delete
c++
端平入洛5 天前
auto有时不auto
c++
郑州光合科技余经理6 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1236 天前
matlab画图工具
开发语言·matlab
dustcell.6 天前
haproxy七层代理
java·开发语言·前端
norlan_jame6 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20216 天前
信号量和信号
linux·c++