complex - STL C++

operator+()

Declaration

complex<X> operator+(const complex<X>& kqrZ1, const complex<X>& kqrZ2);

Description

This friend function is the overloaded addition operator for the complex class. This operator returns the sum of kqrZ1 and kqrZ2. Two overloaded versions allow a complex number to be added to the real number parameter type. The third overloaded version, the unary operator, is a just a sign function that returns the complex number that is passed in; it is like the negation operator, except that it does not affect the value of the number.

Header Include

#include <complex>

Overloads

complex<X> operator+(const complex<X>& kqrZ, const X& kxrX);

complex<X> operator+(const X& kxrX, const complex<X>& kqrZ);

complex<X> operator+(const complex<X>& kqrZ);

Example

#include <complex>
#include <iostream>

int main()
{
	using namespace std;

	// Create two complex numbers
	complex<double> qZ1(2.0, 4.0);
	complex<double> qZ2(1.3, 5.1);
	cout << "Complex Number 1 : "<< qZ1 << endl;
	cout << "Complex Number 2 : "<< qZ2 << endl;

	// Try each version of the operator+
	double dReal = 3.14;
	cout << "Sum 1 = " << (qZ1 + qZ2) << endl;
	cout << "Sum 2 = " << (qZ1 + dReal) << endl;
	cout << "Sum 3 = " << (dReal + qZ2) << endl;
	cout << "Sign = " << +qZ1 << endl;

	// Keep the window open
	cin.get();
	return 0;
}

Output

operator+() Output
 

© 2007–2024 XoaX.net LLC. All rights reserved.