1/13+2

运算符重载

复制代码
myString.h
#ifndef MYSTRING_H
#define MYSTRING_H
#include <cstring>
#include <iostream>
using namespace std;
class myString
{
    private:
        char *str;          //记录c风格的字符串
        int size;            //记录字符串的实际长度
        int capacity;           //记录字符串的容量
    public:
        //无参构造
        myString():size(10), capacity(10)
        {
            str = new char[size];         //构造出一个长度为10的字符串
        }
        //有参构造
        myString(const char *s);              //有参构造     string  s("hello wirld");
        //有参构造
        myString(int n, char ch);                //string   s(5, 'A');
        //析构函数
        ~myString();
        void show();
        //拷贝构造函数
        myString(const myString &other);
        //拷贝赋值函数
        myString& operator=(const myString &other);
        //判空函数
        bool empty() const;
        //size函数
        int getSize() const;
        //c_str函数
        const char* c_str() const;
        //at函数
        char &at(int index);
        //二倍扩容
        void resize(int newCapacity);
        //实现+=运算符重载
        myString& operator+=(const myString &other);
        //取地址运算符重载
        myString* operator&();

        //将[]运算符重载
        char& operator[](const int index);
        //
        //将+重载
        myString& operator+(const myString &other);
        //将==重载
        bool operator==(const myString &other) const;
        //将!=重载
        bool operator!=(const myString &other) const;
        //将>重载
        bool operator>(const myString &other) const;
        //将<重载
        bool operator<(const myString &other) const;
        //将>=重载
        bool operator>=(const myString &other) const;
        //将<=重载
        bool operator<=(const myString &other) const;

        // 友元函数,重载<<运算符
        friend ostream& operator<<(ostream &os, const myString &s)
        {
            os << s.str;
            return os;
        }
        // 友元函数,重载>>运算符
        friend istream& operator>>(istream &is, const myString &s)
        {
            is>> s.str;
            return is;
        }
};
#endif // MYSTRING_H

myString.cpp

#include"myString.h"
//有参构造
myString::myString(const char *s)
{
    if(s)
    {
        size=strlen(s);
        capacity=size+1;
        str=new char[size];
        strcpy(str, s);
    }else {
        size = 0;
        capacity = 10;
        str = new char[size];
    }
}
//有参构造
myString::myString(int n, char ch): size(n), capacity(n + 1)
{
    str = new char[size];
    memset(str, ch, n);
}
//析构函数
myString::~myString()
{
    delete[]str;
}

void myString::show()
{
    cout<<"字符串为:"<<this->str<<endl;
}
//拷贝构造函数
myString::myString(const myString &other): size(other.size), capacity(other.capacity)
{
    str = new char[size];
    strcpy(str, other.str);
}
//拷贝赋值函数
myString &myString::operator=(const myString &other)
{
    if (this != &other)
    {
        delete[] str;
        size = other.size;
        capacity = other.capacity;
        str = new char[size];
        strcpy(str, other.str);
    }
    return *this;
}
//判空函数
bool myString::empty() const
{
    return size == 0;
}
//size函数
int myString::getSize() const
{
    return size;
}
// c_str函数
const char *myString::c_str() const
{
    return str;
}
// at函数
char &myString::at(int index)
{
    if (empty()||index < 0 || index >= size)
    {
        cout<<"访问元素失败"<<endl;
    }
    return str[index];
}
//二倍扩容
void myString::resize(int newCapacity)
{
    char *newStr = new char[newCapacity];
    strcpy(newStr, str);
    delete[] str;
    str = newStr;
    capacity = newCapacity;
}
//实现+=运算符重载
myString &myString::operator+=(const myString &other)
{
    int newSize = size + other.size;
    if (newSize >= capacity) {
        resize(newSize * 2);
    }
    strcat(str, other.str);
    size = newSize;
    return *this;
}
//取地址运算符重载
myString *myString::operator&()
{
    return this;
}
//将[]运算符重载
char &myString::operator[](const int index)
{
    if(index<0||index>=size)
    {
        cout<<"重载失败"<<endl;
    }
    return str[index];
}
//将+重载
myString &myString::operator+(const myString &other)
{
    int newSize=size+other.size;
    if (newSize >= capacity) {
        resize(newSize * 2);
    }
    strcpy(this->str,str);
    strcat(this->str,other.str);
    return *this;
}

//将==重载
bool myString::operator==(const myString &other) const
{
    return strcmp(str,other.str)==0;
}
//将!=重载
bool myString::operator!=(const myString &other) const
{
     return strcmp(str,other.str)!=0;
}
//将>重载
bool myString::operator>(const myString &other) const
{
    return strcmp(str,other.str)>0;
}
//将<重载
bool myString::operator<(const myString &other) const
{
    return strcmp(str,other.str)<0;
}
//将>=重载
bool myString::operator>=(const myString &other) const
{
    return strcmp(str,other.str)>=0;
}
//将<=重载
bool myString::operator<=(const myString &other) const
{
    return strcmp(str,other.str)<=0;
}

main.cpp

#include"myString.h"

int main()
{
    myString s1("Hello");
    myString s2(" World");
    s1 += s2;
    s1.show();       // 输出 "Hello World"
    cout << "size: " << s1.getSize() << endl;  // 输出 "Size: 11"

    cout<<s1[0]<<endl;
    myString s3=s1+s2;
    s3.show();
    myString s4("aaaaa");
    myString s5("bbbbb");
    if(s4==s5)
    {
        cout<<"yes"<<endl;
    }else{
        cout<<"no"<<endl;
    }
    if(s4!=s5)
    {
        cout<<"yes"<<endl;
    }else{
        cout<<"no"<<endl;
    }
    if(s4>s5)
    {
        cout<<"yes"<<endl;
    }else{
        cout<<"no"<<endl;
    }
    if(s4<s5)
    {
        cout<<"yes"<<endl;
    }else{
        cout<<"no"<<endl;
    }
    if(s4>=s5)
    {
        cout<<"yes"<<endl;
    }else{
        cout<<"no"<<endl;
    }
    if(s4<=s5)
    {
        cout<<"yes"<<endl;
    }else{
        cout<<"no"<<endl;
    }
    myString s6;
    cin>>s6;
    s6.show();
    return 0;
}
相关推荐
吾名招财1 小时前
open3d+opencv实现矩形框裁剪点云操作(C++)
c++·opencv·open3d·点云裁剪
诚丞成1 小时前
字符串算法篇——字里乾坤,算法织梦,解构字符串的艺术(下)
c++·算法
我想学LINUX2 小时前
【2024年华为OD机试】(C卷,100分)- 攀登者1 (Java & JS & Python&C/C++)
java·c语言·javascript·c++·python·游戏·华为od
Ring__Rain4 小时前
野指针bug
c++·bug
xqhoj6 小时前
C++学习指南(七)——stack/queue/priority_queue
开发语言·c++
埃菲尔铁塔_CV算法7 小时前
双线性插值算法:原理、实现、优化及在图像处理和多领域中的广泛应用与发展趋势(二)
c++·人工智能·算法·机器学习·计算机视觉
叫我龙翔7 小时前
【算法日记】从零开始认识动态规划(一)
c++·算法·动态规划·代理模式
Pafey7 小时前
c++ 中的容器 vector、deque 和 list 的区别
开发语言·c++
大草原的小灰灰7 小时前
C++ STL之容器介绍(vector、list、set、map)
数据结构·c++·算法
疯狂学习GIS8 小时前
互联网大中小厂实习面经:滴滴、美团、货拉拉、蔚来、信通院等
c++·python