重载符号的总结

//运算符重载的相关知识点

//重载:一名多用  自定义规则

//不可重载的运算符:    .     ::     .*       ?:      sizeof

//重载后的优先级不变 不改变结合性和所需操作数  不去创建新的运算符

 

//<< >>只能用全局函数重载

 

//= , 【】 , () , -> ,只能用成员函数重载

 

//c++通过一个站位参数    区分前置后置

 

/*

//我们试着重载一下运算符 + 号

#include <iostream>

#include <windows.h>

 

using namespace std;

 

class Complex

{

friend Complex operator +(Complex &c1, Complex &c2);

private:

int m_a;

int m_b;

public:

Complex();

Complex(int a, int b);

void print();

};

 

Complex::Complex()

{

m_a = 0;

m_b = 0;

}

 

Complex::Complex(int a, int b)

{

m_a = a;

m_b = b;

}

 

Complex operator +(Complex &c1, Complex &c2)

{

Complex tmp;

tmp.m_a = c1.m_a + c2.m_a;

tmp.m_b = c1.m_b + c2.m_b;

return tmp;

}

 

void Complex::print()

{

cout << "m_a = " << m_a << "  m_b = " << m_b << endl;

}

 

int main()

{

Complex c1(1, 2);

Complex c2(3, 4);

Complex c3;

 

c3 = c1 + c2;

c3.print();

 

system("pause");

return 0;

}

*/

 

 

/*

//重载<<

 

#include <iostream>

#include <windows.h>

 

using namespace std;

 

class Complex

{

friend ostream & operator <<(ostream &out, Complex &c);

private:

int m_a;

int m_b;

public:

Complex();

Complex(int a, int b);

void print();

};

 

Complex::Complex()

{

m_a = 0;

m_b = 0;

}

 

Complex::Complex(int a, int b)

{

m_a = a;

m_b = b;

}

 

void Complex::print()

{

cout << "m_a = " << m_a << "  m_b = " << m_b << endl;

}

 

ostream & operator << (ostream &out, Complex & c)//重载<<

{

out << "c.m_a = " << c.m_a << "  c.m_b = " << c.m_b << endl;

return out;

}

 

int main()

{

Complex c2(3, 4);

cout << c2 << endl;

 

system("pause");

return 0;

}

 

 

*/

 

/**/

//我们试着重载一下运算符 ++ 前置和后置

#include <iostream>

#include <windows.h>

 

using namespace std;

 

class Complex

{

friend ostream & operator <<(ostream &out, Complex &c);

private:

int m_a;

int m_b;

public:

Complex();

Complex(int a, int b);

void print();

Complex operator + (Complex& c2);

Complex operator ++(int);//后置++

Complex &operator ++();//前置++

};

 

Complex::Complex()

{

m_a = 0;

m_b = 0;

}

 

Complex::Complex(int a, int b)

{

m_a = a;

m_b = b;

}

 

void Complex::print()

{

cout << "m_a = " << m_a << "  m_b = " << m_b << endl;

}

 

ostream & operator << (ostream &out, Complex & c)//重载 <<

{

out << "c.m_a =" << c.m_a << "  c.m_b =" << c.m_b << endl;

return out;

}

 

Complex Complex::operator ++(int)//后置++

{

Complex c7;

c7.m_a = m_a;

c7.m_b = m_b;

 

m_a++;

m_b++;

return c7;

}

 

Complex& Complex::operator ++()//前置++

{

m_a++;

m_b++;

return *this;

}

 

int main()

{

Complex c1(1, 2);

cout << ++c1 << endl;

cout << c1++ << endl;

system("pause");

return 0;

}

/**/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值