C++运算符重载

单目运算符重载
包括 前置++前置- -后置++后置- -
原型:
成员函数写法:className operator++( int )
友元函数写法:friend className operator++(className& obj ,int )
其中,有无 int 参数是区分前置还是后置的标志,有 int 参数的为后置
成员函数写法少写一个参数是因为成员函数中自带一个指向当前对象的指针this

下面以坐标点自加、自减为例

#include <iostream>
using namespace std;

class Cpoint
{
private:
    int x;
    int y;

public:
    Cpoint(int x, int y)
    {
        this->x = x;
        this->y = y;
    }
    ~Cpoint()
    {
    }

    void print()
    {
        cout << "(" << x << "," << y << ")" << endl;
    }

    Cpoint operator++();
    Cpoint operator--();
    //friend Cpoint operator++(Cpoint& c);
    //friend Cpoint operator--(Cpoint& c);

    //Cpoint operator++(int);
    //Cpoint operator--(int);
    friend Cpoint operator++(Cpoint& c, int);
    friend Cpoint operator==(Cpoint& c, int);
};

Cpoint Cpoint::operator++()
{
    x++; 
    y++;
    return *this;
}
Cpoint Cpoint::operator--()
{
    x--;
    y--;
    return *this;
}
/*
Cpoint operator++(Cpoint& c)
{
    c.x++;
    c.y++;
    return c;
}
Cpoint operator--(Cpoint& c)
{
    c.x--;
    c.y--;
    return c;
}

Cpoint Cpoint::operator++(int)
{
    Cpoint tmp = *this;
    x++;
    y++;
    return tmp;
}
Cpoint Cpoint::operator--(int)
{
    Cpoint tmp = *this;
    x--;
    y--;
    return tmp;
}
*/
Cpoint operator++(Cpoint& c, int)
{
    Cpoint tmp(c);    //Cpoint tmp = c;
    c.x++;
    c.y++;
    return tmp;
}
Cpoint operator--(Cpoint& c, int)
{
    Cpoint tmp(c);
    c.x--;
    c.y--;
    return tmp;
}

int main()
{
    Cpoint c(1,2);
    /*c.print();
    ++c;
    c.print();
    --c;
    c.print();*/
    c--.print();
    //c.print();
    c--.print();
    c.print();

    return 0;
}

双目运算符重载

1)+ - * /(加减乘除):
原型:
成员函数写法:className operatot + (className& obj) (其中obj一般指右操作数
友元函数写法:friend className operator+(className& obj1, className& obj2)

下面以复数的相加、减、乘、除为例()

#include <iostream>
using namespace std;

class Complex
{
private:
    float real;
    float imag;

public:
    Complex()
    {
        real = 0;
        imag = 0;
    }
    Complex(float a, float b)
    {
        real = a;
        imag = b;
    }
    ~Complex()
    {
    }

    void print()
    {
        cout << real << "+" << imag << "i" << endl;
    }

    Complex operator+(Complex& c);
    friend Complex operator-(Complex& c1, Complex& c2);
    Complex operator*(Complex& c1);
    friend Complex operator/(Complex& c1, Complex& c2);

    friend ostream& operator<<(ostream& os, Complex &c);
};

ostream& operator<<(ostream& os, Complex &c)   //此为“<<”操作符重载
{
    cout << c.real << "+" << c.imag << "i";

    return os;
}

Complex Complex::operator+(Complex& c)
{
    Complex tmp;
    tmp.real = this->real + c.real;
    tmp.imag = this->imag + c.imag;
    return tmp;
}

Complex operator-(Complex& c1, Complex& c2)
{
    Complex tmp;
    tmp.real = c1.real - c2.real;
    tmp.imag = c1.imag - c2.imag;
    return tmp;
}

Complex Complex::operator*(Complex& c1)
{
    Complex tmp;
    tmp.real = c1.real * this->real - c1.imag * this->imag;
    tmp.imag = c1.real * this->imag + this->real * c1.imag;
    return tmp;
}

Complex operator/(Complex& c1, Complex& c2)
{
    Complex tmp;
    tmp.real = (c1.real * c2.real) / (c1.real * c1.real + c2.imag * c2.imag);
    tmp.imag = (c1.imag * c2.real - c1.real * c2.imag) / (c1.real * c1.real + c2.imag * c2.imag);
    return tmp;
}

int main()
{
    Complex c1(1, 1);
    Complex c2(1, 1);
    Complex c3;

    c3 = c1 + c2;
    //c3 = c1 - c2;
    //c3 = c1 * c2;
    //c3 = c1 / c2;
    //c3.print();
    
    cout << "c3: " << c3 << endl;

    return 0;
}

2)重载赋值运算符 “ =
重载运算符 “ = ” 可用于解决深拷贝/浅拷贝造成的指针悬挂问题
解决上述问题的方法有两种:
1.手写拷贝构造函数
2.重载运算符“ = ”

重载运算符 “ = ” 的注意事项
1.不可用友元函数写法
2.不能被继承
3.不能被声明为虚函数

原型:className operate=(className& obj) (函数原型有引用符号)

#include <iostream>
using namespace std;

class Student
{
private:
    int age;
    char *name;

public:
    Student(int age, char *name)
    {
        this->age = age;
        this->name = new char [strlen(name) + 1];
        strcpy(this->name, name);
    }
    Student()
    {
        age = 0;
        strcpy(name, "");
    }
    ~Student()
    {
        age = 0;
        if(name != NULL)
        {
            delete []name;
            name = NULL;
        }
    }
    Student(const Student& s)
    {
        age = s.age;
        name = new char [strlen(s.name) + 1];
        strcpy(name, s.name);
    }

    void print()
    {
        cout << "name: " << name << endl;
        cout << "age: " << age << endl;
    }

    Student operator=(Student& s);
};

Student Student::operator=(Student& s)     //函数原型有引用符号
{
	//将旧对象的内存释放
    if(name != NULL)
    {
        delete []name;
        name = NULL;
        age = 0;
    }
    
	//给旧对象分配空间并赋值
	age = s.age;
    name = new char[strlen(s.name) + 1];
    strcpy(name, s.name);

    return *this;
}

int main()
{
    Student s1(20, "xiaoming"); 
    Student s2 = s1;    //初始化语句,调用构造函数
    s2 = s1;    //赋值语句,调用运算符重载

    s2.print();

    return 0;
}

重载数组下标运算符“ [ ]
注意事项:
1.不可用友元函数
2.只适用于一维数组
3.左操作数是当前类的对象

原型:<数组类型>& operator[] (参数)
例:定义一个数组类:int& operator[ ]( int index )

下面以数组类为例

#include <iostream>
using namespace std;

class Arr
{
private:
    int len;
    int *arr;

public:
    Arr(int len)
    {
        this->len = len;
        arr = new int[len];
    }
    ~Arr()
    {
        if(arr != NULL)
        {
            delete []arr;
            arr = NULL;
        }
    }

    int& operator[](int index);
};

int& Arr::operator[](int index)
{
    if(index < 0 || index >= len)
    {
        cout << "error..." << endl;
        exit(1);
    }

    return arr[index];
}

int main()
{
    Arr a(5);

    for(int i = 0; i < 5; i++)
    {
        a[i] = i + 1;
    }

    cout << a[0] << endl;
    cout << a[1] << endl;
    cout << a[5] << endl;   //超出数组大小,打印error并退出
    cout << a[2] << endl;

    return 0;
}

重载左移 << :
注意事项:
1.只能用友元函数写法

原型:friend ostream& operator<<(ostream& os, className& obj)

例:在Complex复数运算的例子里使用过,不再重复

基于STM32F407,使用DFS算法实现最短迷宫路径检索,分为三种模式:1.DEBUG模式,2. 训练模式,3. 主程序模式 ,DEBUG模式主要分析bug,测量必要数据,训练模式用于DFS算法训练最短路径,并将最短路径以链表形式存储Flash, 主程序模式从Flash中….zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值