用成员函数重载运算符:
1 #include "stdafx.h" 2 #include3 using namespace std; 4 class Complex { 5 private: 6 double real, imag; 7 public: 8 Complex(double r = 0.0, double i = 0.0); 9 Complex operator+(Complex c);10 Complex operator-(Complex c);11 void display();12 };13 14 Complex::Complex(double r, double i)15 {16 real = r;17 imag = i;18 }19 20 Complex Complex::operator+(Complex c)21 {22 Complex temp;23 temp.real = real + c.real;24 temp.imag = imag + c.imag;25 return temp;26 }27 28 Complex Complex::operator-(Complex c)29 {30 Complex temp;31 temp.real = real - c.real;32 temp.imag = imag - c.imag;33 return temp;34 }35 36 void Complex::display()37 {38 char ch;39 ch = (imag < 0) ? ' ' : '+';40 cout << real << ch << imag << "i" << endl;41 }42 43 int main()44 {45 Complex c1(12.4, 13.3);46 Complex c2(14.4, 26.5);47 Complex c;48 cout << "c1=";49 c1.display();50 cout << "c2=";51 c2.display();52 c = c1 + c2;53 cout << "c1+c2=";54 c.display();55 c = c1 - c2;56 cout << "c1-c2=";57 c.display();58 return 0;59 }
用友元函数重载运算符:
1 #include "stdafx.h" 2 #include3 using namespace std; 4 class Complex { 5 private: 6 double real, imag; 7 public: 8 Complex(double r = 0.0, double i = 0.0); 9 friend Complex operator+(Complex c1,Complex c2);10 friend Complex operator-(Complex c1,Complex c2);11 void display();12 };13 14 Complex::Complex(double r, double i)15 {16 real = r;17 imag = i;18 }19 20 Complex operator+(Complex c1,Complex c2)21 {22 Complex temp;23 temp.real = c1.real + c2.real;24 temp.imag = c1.imag + c2.imag;25 return temp;26 }27 28 Complex operator-(Complex c1,Complex c2)29 {30 Complex temp;31 temp.real = c1.real - c2.real;32 temp.imag = c1.imag - c2.imag;33 return temp;34 }35 36 void Complex::display()37 {38 char ch;39 ch = (imag < 0) ? ' ' : '+';40 cout << real << ch << imag << "i" << endl;41 }42 43 int main()44 {45 Complex c1(12.4, 13.3);46 Complex c2(14.4, 26.5);47 Complex c;48 cout << "c1=";49 c1.display();50 cout << "c2=";51 c2.display();52 c = c1 + c2;53 cout << "c1+c2=";54 c.display();55 c = c1 - c2;56 cout << "c1-c2=";57 c.display();58 return 0;59 }
单目运算符重载(自加·自减)
用成员函数重载运算符:
1 #include "stdafx.h" 2 #include3 using namespace std; 4 class Counter { 5 private: 6 unsigned value; //unsigned不加类型名时,默认表示无符号整型 7 public: 8 Counter() { value = 0; } 9 Counter(int i) { value = i; }10 Counter operator++();11 Counter operator++(int);12 Counter operator--();13 Counter operator--(int);14 void display() { cout << value << endl; }15 };16 17 Counter Counter::operator++()18 {19 value++;20 return *this;21 }22 23 Counter Counter::operator++(int)24 {25 Counter temp;26 temp.value = value++;27 return temp;28 }29 30 Counter Counter::operator--()31 {32 value--;33 return *this;34 }35 36 Counter Counter::operator--(int)37 {38 Counter temp;39 temp.value = value--;40 return temp;41 }42 43 int main()44 {45 Counter n(10);46 Counter c;47 c = ++n;48 cout << "前缀++运算符计算结果:" << endl;49 cout << "n=", n.display();50 cout << "c=", c.display();51 c = n++;52 cout << "后缀++运算符计算结果:" << endl;53 cout << "n=", n.display();54 cout << "c=", c.display();55 c = --n;56 cout << "前缀--运算符计算结果:" << endl;57 cout << "n=", n.display();58 cout << "c=", c.display();59 c = n--;60 cout << "后缀--运算符计算结果:" << endl;61 cout << "n=", n.display();62 cout << "c=", c.display();63 return 0;64 }
用友元函数重载运算符:
1 #include "stdafx.h" 2 #include3 using namespace std; 4 class Counter { 5 private: 6 unsigned value; //unsigned不加类型名时,默认表示无符号整型 7 public: 8 Counter() { value = 0; } 9 Counter(int i) { value = i; }10 friend Counter operator++(Counter&);11 friend Counter operator++(Counter& ,int);12 friend Counter operator--(Counter&);13 friend Counter operator--(Counter& ,int);14 void display() { cout << value << endl; }15 };16 17 Counter operator++(Counter& p)18 {19 p.value++;20 return p;21 }22 23 Counter operator++(Counter& p,int)24 {25 Counter temp;26 temp.value = p.value++;27 return temp;28 }29 30 Counter operator--(Counter& p)31 {32 p.value--;33 return p;34 }35 36 Counter operator--(Counter& p ,int)37 {38 Counter temp;39 temp.value = p.value--;40 return temp;41 }42 43 int main()44 {45 Counter n(10);46 Counter c;47 c = ++n;48 cout << "前缀++运算符计算结果:" << endl;49 cout << "n=", n.display();50 cout << "c=", c.display(); //这句话相当于 cout<<"c=";51 c = n++; //c.display();52 cout << "后缀++运算符计算结果:" << endl;53 cout << "n=", n.display();54 cout << "c=", c.display();55 c = --n;56 cout << "前缀--运算符计算结果:" << endl;57 cout << "n=", n.display();58 cout << "c=", c.display();59 c = n--;60 cout << "后缀--运算符计算结果:" << endl;61 cout << "n=", n.display();62 cout << "c=", c.display();63 return 0;64 }
最后来一组运行结果: