Addition operation overload
Call the member variable of the object to send the change tag: <none>
Code snippet
# Pragma once
Class Complex
{
Public:
Complex (void );
~ Complex (void );
Complex (int real, int imag );
Int get_real () const;
Int get_imag () const;
Void Display () const;
Void Addone (const Complex & other );
Complex operator + (const Complex & other );
Private:
Int real _;
Int imag _;
};
Complex: Complex (void ){}
Complex: Complex (int real, int imag): real _ (real), imag _ (imag ){}
Int Complex: get_real () const
{Return real _;
}
Int Complex: get_imag () const
{Return imag _;
}
Void Complex: Display () const
{Cout <real _ <',' <imag _ <endl;
};
Void Complex: Addone (const Complex & other)
{Real _ + = other. get_real ();
Imag _ + = other. get_imag ();
}
Complex: operator + (const Complex & other) // The value of the calling object has changed.
{Real _ + = other. get_real ();
Imag _ + = other. get_imag ();
Return * this;
}
Complex ::~ Complex (void)
{
}
# Include <iostream>
# Include "Complex. h"
Using namespace std;
Complex Add (Complex & A, Complex & B)
{Return Complex (A. get_real () + B. get_real (), A. get_imag () + B. get_imag ());
};
Void main ()
{Complex a (10, 5 );
Complex B (11,0 );
Complex c;
A. Display ();
B. Display ();
C = Add (a, B );
C. Display ();
C. Addone ();
C. Display ();
A + B; // It is equivalent to calling a. operator + (B) with a class member function
A. Display ();
}
Returns a new object with an operator overload.
Code snippet
# Pragma once
Class Complex
{
Public:
Complex (void );
~ Complex (void );
Complex (int real, int imag );
Int get_real () const;
Int get_imag () const;
Void Display () const;
Void Addone (const Complex & other );
Complex operator + (const Complex & other );
Private:
Int real _;
Int imag _;
};
# Include "Complex. h"
# Include <iostream>
Using namespace std;
Complex: Complex (void ){}
Complex: Complex (int real, int imag): real _ (real), imag _ (imag ){}
Int Complex: get_real () const
{Return real _;
}
Int Complex: get_imag () const
{Return imag _;
}
Void Complex: Display () const
{Cout <real _ <',' <imag _ <endl;
};
Void Complex: Addone (const Complex & other)
{Real _ + = other. get_real ();
Imag _ + = other. get_imag ();
}
// Complex: operator + (const Complex & other) // The value of the calling object has changed.
// {Real _ + = other. get_real ();
// Imag _ + = other. get_imag ();
// Return * this;
//}
Complex: operator + (const Complex & other) // The calling object does not change.
{Int I, j;
I = real _ + other. get_real ();
J = imag _ + other. get_imag ();
Return Complex (I, j );
}
Complex ::~ Complex (void)
{
}
# Include <iostream>
# Include "Complex. h"
Using namespace std;
Complex Add (Complex & A, Complex & B)
{Return Complex (A. get_real () + B. get_real (), A. get_imag () + B. get_imag ());
};
Void main ()
{Complex a (10, 5 );
Complex B (11,0 );
Complex c;
A. Display ();
B. Display ();
C = Add (a, B );
C. Display ();
C. Addone ();
C. Display ();
C = a + B; // It is equivalent to calling a. operator + (B) for a class member function.
C. Display ();
}