DAY5 C++运算符重载

1.类实现> 、<、!=、||、!和后自增、前自减、后自减运算符的重载

代码:

cpp 复制代码
#include <iostream>

using namespace std;
class Complex
{
    int rel;
    int vir;
public:
    Complex(){};
    Complex(int rel,int vir):rel(rel),vir(vir){cout << "有参构造" << endl;}
    bool operator>(const Complex other);
    bool operator<(const Complex other);
    bool operator!=(const Complex other);
    bool operator||(const Complex other);
    bool operator!();
    Complex operator++();
    Complex operator--();
    friend Complex operator++(Complex &other,int);
    friend Complex operator--(Complex &other,int);
    void show();
};
bool Complex::operator>(const Complex other)
{
    return  rel>other.rel && vir >other.vir;
}
bool Complex::operator<(const Complex other)
{
    return  rel<other.rel && vir<other.vir;
}
bool Complex::operator!=(const Complex other)
{
    return  (rel!=other.rel) || (vir!=other.vir);
}
bool Complex::operator||(const Complex other)
{
    return  (rel||vir) || (other.rel||other.vir);
}
bool Complex::operator!()
{
    return  !(rel&&vir);
}
Complex Complex::operator++()
{
    Complex temp;
    temp.rel=++this->rel;
    temp.vir=++this->vir;
    return temp;
}
Complex Complex::operator--()
{
    Complex temp;
    temp.rel=--this->rel;
    temp.vir=--this->vir;
    return temp;
}
Complex operator++(Complex &other,int)
{
    Complex temp;
    temp.rel=other.rel++;
    temp.vir=other.vir++;
    return temp;
}
Complex operator--(Complex &other,int)
{
    Complex temp;
    temp.rel=other.rel--;
    temp.vir=other.vir--;
    return temp;
}
void Complex::show()
{
    cout << rel << "+" << vir << "i" << endl;
}
int main()
{
    Complex s(1,3);
    Complex p(5,6);
    Complex ss;
    bool rrr=(s<p);
    cout << rrr << endl;
    s=++p;
    ss=p++;
    s.show();
    ss.show();
    return 0;
}

2.My_string类中的+,==,>运算符重载

代码:

cpp 复制代码
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
char c = '\0';
class My_string
{
    char *str;     //记录C风格的字符串
    int size;      //记录字符串长度
public:
    //无参构造
    My_string():str(new char('\0')),size(0){cout << "无参构造" << endl;}
    //有参构造
    My_string(const char *s):str(new char[strlen(s)+1]),size(strlen(s)+1)//分配足够的空间来接收字符串,将接收到的字符串赋值给str
    {
        strcpy(this->str,s);
        cout << "有参构造" << endl;
    }
    //拷贝构造
    My_string(const My_string &other):str(new char[other.size+1])
    {
        strcpy(this->str,other.str);
        this->size=other.size;
        cout << "拷贝构造" << endl;
    }
    //拷贝赋值
    My_string &operator=(const My_string &other)
    {
        delete []str;
        str = new char[other.size+1];
        strcpy(this->str,other.str);
        this->size = other.size;
        cout << "拷贝赋值" << endl;
        return *this;
    }
    char *operator+(const My_string other);
    bool operator==(const My_string other);
    bool operator>(const My_string other);
    //析构函数
    ~My_string()
    {
        delete []str;
        cout << "析构函数" << endl;
    }
    //at函数
    char &my_at(int num);
};
char *My_string::operator+(const My_string other)
{
    char *buff=new char[strlen(str)+strlen(other.str)+1];
    strcpy(buff,str);
    strcat(buff,other.str);
    buff[strlen(buff)]='\0';
    return buff;
}
bool My_string::operator==(const My_string other)
{
    if(strcmp(this->str,other.str)==0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
bool My_string::operator>(const My_string other)
{
    if(strcmp(this->str,other.str)>0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
char &My_string::my_at(int num)
{
        if(num>=size||num<0)
        {
            cout<< "越界访问" << endl;
            return c;
        }
        else
        {
            return str[num];
        }
}
int main()
{
    My_string s="abcde";
    My_string s1="123";
    My_string s2="abcde";
    cout << s.my_at(0) << endl;
    cout << s.my_at(4) << endl;
    cout << s.my_at(5) << endl;
    cout << endl;
    cout << (s1>s) << endl;
    cout << endl;
    cout << (s==s2) << endl;
    cout << endl;
    cout << (s+s1) << endl;
    cout << endl;
    return 0;
}

Xmind知识点:

相关推荐
GiraKoo24 分钟前
【GiraKoo】C++14的新特性
c++
悠悠小茉莉34 分钟前
Win11 安装 Visual Studio(保姆教程 - 更新至2025.07)
c++·ide·vscode·python·visualstudio·visual studio
坏柠1 小时前
C++ Qt 基础教程:信号与槽机制详解及 QPushButton 实战
c++·qt
泽02021 小时前
C++之红黑树认识与实现
java·c++·rpc
岁忧2 小时前
(LeetCode 每日一题) 1865. 找出和为指定值的下标对 (哈希表)
java·c++·算法·leetcode·go·散列表
whoarethenext2 小时前
使用 C++ 实现 MFCC 特征提取与说话人识别系统
开发语言·c++·语音识别·mfcc
R-G-B2 小时前
【MFC】Combobox下拉框中4个选项,运行后点击下拉框选项不能全部展示出来,只能显示2个选项,需要垂直滚动条滚动显示其余选项
c++·mfc
视觉人机器视觉4 小时前
Visual Studio2022和C++opencv的配置保姆级教程
c++·opencv·visual studio
liulilittle4 小时前
C++ i386/AMD64平台汇编指令对齐长度获取实现
c语言·开发语言·汇编·c++
sun0077005 小时前
std::forward作用
开发语言·c++·算法