操做符重載
- 自定義類型須要操做符重載
- 運算符重載入門技術推演
- 友元函數和成員函數實現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;
}