操作符重载

  • 自定义类型需要操作符重载
  • 运算符重载入门技术推演
  • 友元函数和成员函数实现2元运算符重载
  • 友元函数和成员函数实现1元运算符重载(前置++,前置--,后置++,后置--)
  • 友元函数实现运算符重载应用场景
#define _CRT_SECURE_NO_WARNINGS
#include <iostream> using namespace std; class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
} void printComplex()
{
cout << "( " << this->a << ", " << this->b << "i )" << endl;
} friend Complex complexAdd(Complex &c1, Complex &c2);
//friend Complex operator+(Complex &c1, Complex &c2);
//friend Complex operator-(Complex &c1, Complex &c2); Complex complexAdd(Complex &another)
{
Complex temp(this->a + another.a, this->b + another.b);
return temp;
} Complex operator+(Complex &another)
{
Complex temp(this->a + another.a, this->b + another.b);
return temp;
}
Complex operator-(Complex &another)
{
Complex temp(this->a - another.a, this->b - another.b);
return temp;
} private:
int a;//实数
int b;//虚数
}; Complex complexAdd(Complex &c1, Complex &c2)
{
Complex temp(c1.a + c2.a, c1.b + c2.b);
return temp;
} //操作符重载写在全局
#if 0
Complex operator+(Complex &c1, Complex &c2)
{
Complex temp(c1.a + c2.a, c1.b + c2.b);
return temp;
} Complex operator-(Complex &c1, Complex &c2)
{
Complex temp(c1.a - c2.a, c1.b - c2.b);
return temp;
} #endif int main(void)
{
Complex c1(1, 2);
Complex c2(2, 4); c1.printComplex();
c2.printComplex(); //Complex c3 = complexAdd(c1, c2);
//Complex c3 = c1.complexAdd(c2);
//Complex c3 = c1 + c2; //operator+(c1, c2) 全局的调用方式
//c1.operator+(c2)
//Complex c3 = operator+(c1, c2); Complex c3 = c1.operator+(c2); c3.printComplex(); Complex c4 = c1 + c2; c4.printComplex(); return 0;
}

双目运算符重载(-=,+=)

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> using namespace std; class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
} void printComplex()
{
cout << "( " << this->a << ", " << this->b << "i )" << endl;
} //friend Complex & operator+=(Complex &c1, Complex &c2);
friend Complex &operator-=(Complex &c1, Complex &c2); Complex &operator+=(Complex &another)
{
this->a += another.a;
this->b += another.b; return *this;
}
private:
int a;//实数
int b;//虚数
}; //全局
#if 0
Complex & operator+=(Complex &c1, Complex &c2)
{
c1.a += c2.a;
c1.b += c2.b; return c1;
}
#endif Complex &operator-=(Complex &c1, Complex &c2)
{
c1.a -= c2.a;
c1.b -= c2.b; return c1;
} int main(void)
{
Complex c1(1, 2);
Complex c2(2, 4); (c1 += c2)+=c2;//c1.operator+=(c2) .operator(c2) c1.printComplex();
c2.printComplex(); c1 -= c2;
c1.printComplex(); return 0;
}

单目运算符

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> using namespace std; class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
} void printComplex()
{
cout << "( " << this->a << ", " << this->b << "i )" << endl;
} //friend Complex & operator++(Complex &c);
//friend const Complex operator++(Complex &c1, int); Complex &operator++()
{
this->a++;
this->b++; return *this;
} const Complex operator++(int)//亚元
{
Complex temp(this->a, this->b);
this->a++;
this->b++;
return temp;
} private:
int a;//实数
int b;//虚数
}; #if 0
//重载的是前++运算符
Complex & operator++(Complex &c)
{
c.a++;
c.b++;
return c;
}
#endif //重载的是后++运算符
#if 0
const Complex operator++(Complex &c1, int)
{
Complex temp(c1.a, c1.b); c1.a++;
c1.b++; return temp;
}
#endif int main(void)
{ Complex c1(1, 2); //++++c1; c1++; c1.printComplex(); //++++c1; return 0;
}

左移右移操作符重载

#define _CRT_SECURE_NO_WARNINGS
#include <iostream> using namespace std; class Complex
{
public:
Complex(int a, int b)
{
this->a = a;
this->b = b;
} void printComplex()
{
cout << "( " << this->a << ", " << this->b << "i )" << endl;
} friend ostream& operator<<(ostream & os, Complex &c);
friend istream & operator>>(istream &is, Complex &c); //<<操作符只能写在全局,不能够写在成员方法中。否则调用的顺序会变饭,c1<<cout;
#if 0
ostream& operator<<(ostream &os) //c1.operator<<(cout)
{
os << "( " << this->a << ", " << this->b << "i )";
return os;
}
#endif private:
int a;//实数
int b;//虚数
}; #if 1
ostream& operator<<(ostream & os, Complex &c)
{
os << "( " << c.a << ", " << c.b << "i )"; return os;
} istream & operator>>(istream &is, Complex &c)
{
cout << "a:";
is >> c.a;
cout << "b:";
is >> c.b; return is;
}
#endif int main(void)
{
Complex c1(1, 2); cin >> c1;//operaotr>>(cin, c1) cout << c1;
//c1 << cout;
//cout.operator<<(c1); //cout << c1 << " " <<c1<< endl;//operator<<(cout, c1); return 0;
}
05-20 13:09