c++运算符重载

/*运算符重载:
* 1.
* 全局函数 完成 +操作符 重载(又叫友元函数重载)
* Complex operator+(Complex &c1, Complex &c2)
* 类成员函数 完成 -操作符 重载
* Complex operator-(Complex &c2)
*
* 区别:成员函数中有this指针。。友元函数中没有this指针
*2.二元运算符重载
* 成员函数重载:
* ObjectA op ObjectB
* 重载成员函数,解释为:
* ObjectA.operator op(ObjectB)
* 左操作数由ObjectA通过this指针传递,右操作数由参数ObjectB传递
* 重载为友元函数,解释为:
* operator op(ObjectA, ObjectB)
* 左右操作数都由参数传值
*3.一元运算符重载
* 成员函数重载:
* ObjectA op 或 op ObjectA
* 重载成员函数,解释为:
* ObjectA.operator op()
* 操作数由对象通过this指针隐式传递
* 重载为友元函数,解释为:
* operator op(ObjectA)
* 操作数由参数表的参数ObjectA提供
*4.前置和后置运算符总结
* c++通过一个占位符来区分前置运算和后置运算的区别
* 前置
* Cal & Cal::operator++() //重载成员函数 Object.operator ++()
* Cal & Cal::operator--() //重载成员函数 Object.operator --()
* friend Cal & operator++(Cal &cal) //重载友元函数 operator ++(Object)
* friend Cal & operator--(Cal &cal) //重载友元函数 operator --(Object)
* 后置
* Cal Cal::operator++(int) //重载成员函数 Object.operator ++(0)
* Cal Cal::operator--(int) //重载成员函数 Object.operator --(0)
* friend Cal operator++(Cal &cal,int) //重载友元函数 operator ++(Object,0)
* friend Cal operator--(Cal &cal,int) //重载友元函数 operator --(Object,0)
*5.一般的运算符重载常用重载成员函数, 重载友元函数常用于运算符的左右操作数类型不同的情况。
*6.operator= 必须重载为成员函数
*  重载函数原型为:类型 & 类名 :: operator= ( const 类名 & ) ;
*
* */

#include <iostream>
using namespace std;
class Cal {
public:
Cal(int a, int b) :
a(a), b(b) {
}
//二元运算符重载
Cal operator +(int x) const;
Cal operator -(const Cal &b) const;
friend Cal operator *(const Cal &a, const Cal &b);
//一元运算符重载
Cal& operator --(); //前置--
friend Cal& operator ++(Cal &cal); //前置++
//后置
Cal operator --(int); //后置--
friend Cal operator ++(Cal &cal, int);//后置++
void print() {
cout << "a=" << a << " b=" << b << endl;
}
private:
int a;
int b;
};
Cal Cal::operator +(int x) const {
Cal res(0, 0);
res.a = a + x;
return res;
}
Cal Cal::operator -(const Cal &b) const {
Cal res(0, 0);
res.b = this->b - b.b;
return res;
}
Cal operator *(const Cal &a, const Cal &b) {
Cal res(0, 0);
res.a = a.a * b.a;
return res;
}
Cal& Cal::operator --() {
a--;
b--;
return *this;
}
Cal& operator ++(Cal &cal) {
cal.a++;
cal.b++;
return cal;
}
Cal Cal::operator --(int) {
/*因为后置--,先返回值,再--
* return *this
* this.a--
* this.b--
* 所以按下面写
*/
Cal tmp(0,0);
tmp = *this;
this->a--;
this->b--;
return tmp;
}
Cal operator ++(Cal &cal, int){
//原理同后置--
Cal tmp = cal;
cal.a++;
cal.b++;
return tmp;
}
class friendClass{
private :
int x;
public:
friendClass(int x){
this->x = x;
}
//friend void operator <<(ostream &os, friendClass &fc);
friend ostream& operator <<(ostream &os,friendClass &fc);
};
//void operator <<(ostream &os, friendClass &fc){
//    cout<<"hello world"<<endl;
//    cout<<"x="<<fc.x<<endl;
//}
ostream& operator <<(ostream &os,friendClass &fc){
//ostream out = os;
cout<<"hello world1111111"<<endl;
cout<<"x="<<fc.x<<endl;
return os;
}
int main() {
Cal a(1, 9);
Cal b(3, 1);
Cal c(0, 0);
c = a + 8; //等价于 c = a.operator+(8)
c.print();
c = a - b; //等价于 c = a.operator-(b)//重载为成员函数
c.print();
c = a * b; //等价于 c = operator*(a,b) 重载为友元函数
c.print();
cout<<"+++++++++++++++++"<<endl;
//前置运算符
c = --a; //等价于 c = a.operator--()
c.print();
c = ++b; //等价于c = operator++(b)
c.print();
a--;
a.print();
cout<<"+++++++++++++++++"<<endl;
//后置++和后置--测试
Cal tmp(0,0);
Cal d(1,1);
d.print();
tmp = d--;
tmp.print();
d.print();
Cal f(2,2);
f.print();
tmp = f++;
tmp.print();
f.print();
friendClass fc(5);
//cout<<fc;
cout<<fc<<"2222"<<endl;
return 0;

}
原文地址:https://www.cnblogs.com/w-which/p/7217328.html