complex - STL C++

operator-()

Declaration

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

Description

This friend function is the overloaded subtraction operator for the complex class. This operator returns the difference of kqrZ1 and kqrZ2. Two overloaded versions allow for calculating differences between a complex number the real number parameter type. The third overloaded version, the unary operator, is a just a sign function that returns the negation of the complex number that is passed in.

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 << "Difference 1 = " << (qZ1 - qZ2) << endl;
	cout << "Difference 2 = " << (qZ1 - dReal) << endl;
	cout << "Difference 3 = " << (dReal - qZ2) << endl;
	cout << "Negation = " << -qZ1 << endl;

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

Output

operator-() Output
 

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