【C++笔记5】运算符重载

文章详细介绍了C++中的运算符重载概念,包括不能重载的运算符、加法运算符、左移运算符、自增运算符、赋值运算符、关系运算符以及函数调用运算符的重载示例。通过成员函数和全局函数两种方式实现,并强调了重载运算符时的注意事项和应用。
摘要由CSDN通过智能技术生成

运算符重载

运算符重载的概念

运算符重载是指允许用户自定义类型的对象使用内置的运算符,或者允许用户自定义的运算符作用于内置类型的对象。运算符重载是通过函数重载来实现的。

不能重载的运算符:

..*运算符

::运算符

?:运算符

sizeof运算符

typeid运算符

运算符重载的基本语法

运算符重载的基本语法如下:

返回类型 operator 运算符(参数列表)
{
    //函数体
}

加法运算符重载

//运算符重载-1
#include<iostream>
using namespace std;
class example
{
    public:
        int a;
        int b;
        example(int x,int y):a(x),b(y)
        {
            cout<<"初始化赋值操作"<<endl;
        }
        example operator+ (example x)
        {
            cout<<"重载操作"<<endl;
            example temp(a+x.a,b+x.b);
            return temp;
        }
};
int main()
{
    example e1(1,2),e2(3,4);
    e1=e1+e1+e2+e2;
    example e3 = e1+e2;
    cout<<"e3的值";
    cout<<e3.a<<","<<e3.b<<endl;
    cout<<"e1的值";
    cout<<e1.a<<","<<e1.b<<endl;
    return 0;
}

运行结果:

初始化赋值操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
e3的值11,16
e1的值8,12

不但可以成员函数实现 + 号运算符重载,也可以通过全局函数实现 + 号运算符重载。

//运算符重载-2
#include<iostream>
using namespace std;
class example
{
    public:
        int a;
        int b;
        example(int x,int y):a(x),b(y)
        {
            cout<<"初始化赋值操作"<<endl;
        }
};
example operator+ (example x,example y)
{
    cout<<"重载操作"<<endl;
    example temp(x.a+y.a,x.b+y.b);
    return temp;
}
int main()
{
    example e1(1,2),e2(3,4);
    e1=e1+e1+e2+e2;
    example e3 = e1+e2;
    cout<<"e3的值";
    cout<<e3.a<<","<<e3.b<<endl;
    cout<<"e1的值";
    cout<<e1.a<<","<<e1.b<<endl;
    return 0;
}
初始化赋值操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
重载操作
初始化赋值操作
e3的值11,16
e1的值8,12

左移(<<)运算符重载

//运算符重载-3
#include<iostream>
using namespace std;
class example
{
    public:
        int a;
        int b;
        example(int x,int y):a(x),b(y)
        {
            cout<<"初始化赋值操作"<<endl;
        }
        //如果需要访问私有成员,需要将重载运算符函数声明为友元函数
        friend ostream& operator<< (ostream &out,example x);
};
ostream& operator<< (ostream &out,example x)
{
    cout<<"重载<<运算符"<<endl;
    out<<x.a<<","<<x.b<<endl;
    return out;
}
int main()
{
    example e1(1,2),e2(3,4);
    cout<<e1<<e2;
    return 0;
}

输出结果:

初始化赋值操作
初始化赋值操作
重载<<运算符
1,2
重载<<运算符
3,4

自增运算符重载

//运算符重载-4(前置++)
#include<iostream>
using namespace std;
class example
{
    public:
        int a;
        int b;
        example(int x,int y):a(x),b(y)
        {
            cout<<"初始化赋值操作"<<endl;
        }
        example operator++ ()
        {
            cout<<"重载++运算符"<<endl;
            a++;
            b++;
            return *this;
        }
};
ostream& operator<<(ostream& out, example ex1) 
{
	out << ex1.a<<ex1.b;
	return out;
}
int main()
{
    example e1(1,2),e2(3,4);
    ++e1;
    ++e2;
    cout<<e1<<","<<e2;
    return 0;
}

输出结果:

初始化赋值操作
初始化赋值操作
重载++运算符
重载++运算符
23,45
//运算符重载-5(后置++)
#include<iostream>
using namespace std;
class example
{
    public:
        int a;
        int b;
        example(int x,int y):a(x),b(y)
        {
            cout<<"初始化赋值操作"<<endl;
        }
        example operator++ (int)
        {
            cout<<"重载++运算符"<<endl;
            example temp(a,b);
            a++;
            b++;
            return temp;
        }
};
ostream& operator<<(ostream& out, example ex1) 
{
	out << ex1.a<<ex1.b;
	return out;
}
int main()
{
    example e1(1,2),e2(3,4);
    e1++;
    e2++;
    cout<<e1<<e2<<endl;
    cout<<e1++<<endl;
    cout<<e1;
    return 0;
}

输出结果:

初始化赋值操作
初始化赋值操作
重载++运算符
初始化赋值操作
重载++运算符
初始化赋值操作
2345
重载++运算符
初始化赋值操作
23
34

所以说:前置递增返回引用,后置递增返回值。

赋值运算符重载

//运算符重载-6
#include<iostream>
using namespace std;
class example
{
    public:
        int a;
        int b;
        example(int x,int y):a(x),b(y)
        {
            cout<<"初始化赋值操作"<<endl;
        }
        example operator= (example x)
        {
            cout<<"重载赋值运算符"<<endl;
            a=x.a;
            b=x.b;
            return *this;
        }
};
ostream& operator<<(ostream& out, example ex1) 
{
    out << ex1.a<<ex1.b;
    return out;
}
int main()
{
    example e1(1,2),e2(3,4);
    e1=e2;
    cout<<e1<<e2<<endl;
    return 0;
}

输出结果:

初始化赋值操作
初始化赋值操作
重载赋值运算符
3434

关系运算符重载

//运算符重载-7
#include<iostream>
using namespace std;
class example
{
    public:
        int a;
        int b;
        example(int x,int y):a(x),b(y)
        {
            cout<<"初始化赋值操作"<<endl;
        }
        bool operator== (example x)
        {
            cout<<"重载==运算符"<<endl;
            if(a==x.a&&b==x.b)
                return true;
            else
                return false;
        }
        bool operator!= (example x)
        {
            cout<<"重载!=运算符"<<endl;
            if(a!=x.a||b!=x.b)
                return true;
            else
                return false;
        }
        bool operator> (example x)
        {
            cout<<"重载>运算符"<<endl;
            if(a>x.a&&b>x.b)
                return true;
            else
                return false;
        }
        bool operator< (example x)
        {
            cout<<"重载<运算符"<<endl;
            if(a<x.a&&b<x.b)
                return true;
            else
                return false;
        }
};
ostream& operator<<(ostream& out, example ex1) 
{
    out << ex1.a<<ex1.b;
    return out;
}
int main()
{
    example e1(1,2),e2(3,4);
    if(e1==e2)
        cout<<"e1==e2"<<endl;
    else
        cout<<"e1!=e2"<<endl;
    if(e1!=e2)
        cout<<"e1!=e2"<<endl;
    else
        cout<<"e1==e2"<<endl;
    if(e1>e2)
        cout<<"e1>e2"<<endl;
    else
        cout<<"e1<=e2"<<endl;
    if(e1<e2)
        cout<<"e1<e2"<<endl;
    else
        cout<<"e1>=e2"<<endl;
    return 0;
}

输出结果:

初始化赋值操作
初始化赋值操作
重载==运算符
e1!=e2
重载!=运算符
e1!=e2
重载>运算符
e1<=e2
重载<运算符
e1<e2

函数调用运算符重载

//运算符重载-8
#include<iostream>
using namespace std;
class example
{
    public:
        int a;
        int b;
        example(int x,int y):a(x),b(y)
        {
            cout<<"初始化赋值操作"<<endl;
        }
        example operator() (example x)
        {
            cout<<"重载()运算符"<<endl;
            a=x.a;
            b=x.b;
            return *this;
        }
};
ostream& operator<<(ostream& out, example ex1) 
{
    out << ex1.a<<ex1.b;
    return out;
}
int main()
{
    example e1(1,2),e2(3,4);
    e1(e2);
    cout<<e1<<","<<e2<<endl;
    return 0;
}

输出结果:

初始化赋值操作
初始化赋值操作
重载()运算符
34,34

其他运算符略

重载运算符的注意事项

  1. 重载运算符不能改变运算符的优先级和结合性
  2. 重载运算符不能创建新的运算符
  3. 重载运算符不能改变运算符的操作数个数
  4. 重载运算符不能改变运算符的操作数类型
  5. 重载运算符不能改变运算符的操作数的含义

重载运算符的应用

  1. 重载运算符可以使得类的对象像内置类型一样使用,如:cin、cout、+、-、、/、==、!=、>、<、++、–、=、()、[]、&、|、~、!、<<、>>、+=、-=、=、/=、<<=、>>=、&=、^=、|=、,、->、->*、new、delete、new[]、delete[]等;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

美丽新科技

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值