C ++ operator overloading the study notes (eyes, avoiding objects and numbers operator overloading)

Binary operator:

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  class Complex {
 . 4  public :
 . 5      Complex ( Double R & lt = 0.0 , Double I = 0.0 ): Real (R & lt), imag (I) {} // default constructor is though default, but if given the parameters can also be called, is a good habit
 6      
7      Complex operator + ( const Complex & c2) const ; // + 
8  
9      Complex operator + ( int & a) const ; //Complex and real number 
10      
. 11      Complex operator - ( const Complex C2 &) const ; // - 
12 is  
13 is      void the display () const ;    // output of the complex 
14  Private :
 15      Double Real;     // complex real part 
16      Double imag;     // plurality of virtual portion 
. 17  };
 18 is  
. 19 complex complex :: operator + ( const complex & C2) const { // complex + 
20 is      return Complex(real + c2.real, imag + c2.imag);
21 }
22 
23 Complex Complex::operator +(int& a) const {//复数和实数+
24     a++;
25     return Complex(real + a, imag );
26 }
27 
28 Complex Complex::operator-(const Complex& c2) const {//复数-
29     return Complex(real - c2.real, imag - c2.imag);
30 }
31 
32 void Complex::display() const {
33     cout << "(" << real << ", " << imag << ")" << endl;
34 }
35 
36 int main() {
37     Complex c1(5, 4), c2(2, 10), c3;
38     int b = 5;
39     cout << "c1 = "; c1.display();
40     cout << "c2 = " ; C2.display ();
 41 is      C3 = C1 - C2;    // use overloaded operators to complete the complex subtractor 
42 is      COUT << " C3 = C1 - C2 = " ; c3.display ();
 43 is      C3 = C1 + C2 ;    // use overloaded operators to complete the complex adder 
44 is      COUT << " C3 = C2 = C1 + " ; c3.display ();
 45      C3 = C1 + B; // this is a complex and real sum, attention (complex int variables and addition) and (complex directly addend c3 = c1 + 5) distinction, which the compiler will generate a plurality of objects 5 as a complex class again, but only one parameter, i.e. the real part, and then adding a complex class 
, because writing the default constructor (above yellow), I suspect the problem is the order of addition function, but tried it and found not, after all, the problem constructor, the following will be given later in this code means
46 is COUT << " C3 = C2 = C1 + " ; c3.display (); 47 return 0 ; 48 }

Want to make c3 = c1 + 5 + function call is the real number of complex code (yellow is not the place):

. 1 #include <the iostream>
 2  the using  namespace STD;
 . 3  class Complex {
 . 4  public :
 . 5      Complex () {}; // default constructor, assignment is not initialized 
. 6      Complex ( Double R & lt, Double I): Real (R & lt), imag (I) {} 
 . 7      
. 8      complex operator + ( const complex C2 &) const ; // + 
. 9  
10      complex operator + ( int A) const ; // complex and real
 11     
12 is      Complex operator - ( const Complex C2 &) const ; // - 
13 is  
14      void the display () const ;    // output of the complex 
15  Private :
 16      Double Real;     // plurality of real part 
. 17      Double imag;     // plural imaginary portion 
18  };
 . 19  
20 is complex complex :: operator + ( const complex & C2) const { // complex + 
21 is      return Complex(real + c2.real, imag + c2.imag);
22 }
23 
24 Complex Complex::operator +(int a) const {//复数和实数+
25     a++;
26     return Complex(real + a, imag );
27 }
28 
29 Complex Complex::operator-(const Complex& c2) const {//复数-
30     return Complex(real - c2.real, imag - c2.imag);
31 }
32 
33 voidThe display :: Complex () const {
 34 is      COUT << " ( " << Real << " , " << << imag " ) " << endl;
 35  }
 36  
37 [  int main () {
 38 is      Complex C1 ( . 5 , . 4 ), C2 ( 2 , 10 ), C3; C3 // default constructor call, garbage data
 39      int B = . 5 ;
 40      COUT << " C1 = " ; c1.display ();
41     cout << "= C2 " ; c2.display ();
 42 is      C3 = C1 - C2;    // use overloaded operators to complete the complex subtractor 
43 is      COUT << " C3 = C1 - C2 = " ; c3.display ();
 44 is      C3 = C1 C2 +;    // use overloaded operators to complete the complex adder 
45      COUT << " C3 = C2 = C1 + " ; c3.display ();
 46 is      C3 = C1 + . 5 ;
 47      COUT << " C3 = C2 = C1 + " ; c3.display ();
 48      return  0 ;
 49 }

Although later solved this problem, but I also found that the object is directly calculated by the operator overloading loopholes, so I strongly recommend operation between objects do not directly through the numbers, but by manipulating objects and object of making it possible to follow their own ideas to run, although through digital directly, but its constructor is obviously unreasonable (because the default constructor did not give the default parameters, but garbage data, C3 is an example)

Guess you like

Origin www.cnblogs.com/working-in-heart/p/12131285.html