java operator 重载 ==_运算符重载

1、赋值运算符=

2、等于运算符==

3、加法运算符+

4、前向自加运算符++

5、后向自加运算符++

6、下标运算符[ ]

7、输入输出运算符<< 和 >>

8、转换运算符( )

9、实例代码

#include

#include

#include

using namespace std;

class A;

ostream& operator<

istream& operator>>(istream &in,A &a);

class A{

private:

int b;

public:

A(){b=10;}

~A(){}

// 加法

A operator+(A&a)

{

A temp;

temp.b=b+a.b;

return temp;

}

// 加等于

A& operator+=(A&a)

{

b+=a.b;

return *this;

}

// 赋值

A& operator=(A&a)

{

if (a!=*this)

{

b=a.b;

}

return *this;

}

//相等

bool operator==(A&a)

{

return b==a.b;

}

// 前向++

A operator++()

{

b++;

return *this;

}

// 后向++ 区别前向++,加了个int形参

A operator++(int o)

{

A temp=*this;

b++;

return temp;

}

// 转换函数-- 形参列表为空,并且无返回类型

operator int()

{

return b;

}

//利用有元函数(可以访问类的私有成员)重载输入输出流

friend ostream& operator<

friend istream& operator>>(istream &in,A &a);

};

//有元函数重载输出流

ostream& operator<

{

out<

return out;

}

//有元函数重载输入流

istream& operator>>(istream &in,A &a)

{

in>>a.b;

return in;

}

int main()

{

A a1;

cout<

A a2=a1++;

cout<

a2=++a1;

cout<

a2+=a1;

cout<

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值