Create a class complex for performing arithmetic with complex numbers. Write a program to test your class. Complex numbers have the form real and imaginary part. Use double variable to represent private data of class. Add and Subtract 2 complex numbers. Print complex numbers in form (a,b).
1
Expert's answer
2013-04-16T08:54:00-0400
//complex class
#include <iostream> using namespace std;
//declaration class Complex { public: Complex(doubleRe = 0, double Im = 0);
//overloadingof operators of +,- and << for //adding,subtracting and output data, respectively //friendclass is needed for access private members of class friendComplex operator+ (const Complex& c1, const Complex& c2); friendComplex operator- (const Complex& c1, const Complex& c2);
//outputing //you can use this with simply ostream streams for output//i.e cout << Complex(2.545, 2.313); //or in file in the same way //you can also make another operator to output in usual way: Re + i*Im; ostream& operator<< (ostream& out, const Complex& c) { out<< "(" << c.m_Re << "," << c.m_Im << ")"; returnout; }
Comments