Usage of loading operations in Js

Source: Internet
Author: User

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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.