Please, help with the question below--relatiing to Complex numbers. The Driver p
ID: 3550301 • Letter: P
Question
Please, help with the question below--relatiing to Complex numbers. The Driver program and Class definition are shown hereunder
----------------------------------------------------------------------------------------------------------------------------
a) Modify the class to enable input and output of complex numbers via overloaded and operators respectively
b) Overload the multiplication operator t enable mltiplication of two complex nubers are in algebra
c) Overload the == and ! operation to allow comparison of complex numbers.
Complex numbers are numbers of the form realPart + imaginaryPart = i, where i has a value of square root of -1
__________________________________________________________________________________________
// driverComplex.cpp
// Driver for class Complex
#include <iostream>
using std::cout;
using std::endl;
#include "Complex.h"
int main()
{
Complex x, y( 4.3, 8.2 ), z( 3.3, 1.1 );
// REPLACE code initializing y and z with code
// that inputs y and z from the user
// Test creation of Complex objects
cout << "x: ";
x.print(); / REPLACE all print() calls with output (<<) statements
cout << " y: ";
y.print();
cout << " z: ";
z.print();
cout << endl;
// Test overloaded addition operator
x = y + z;
cout << " x = y + z: (";
x.print();
cout << ") = (";
y.print();
cout << ") + (";
z.print();
cout << ")" << endl;
// Test overloaded subtraction operator
x = y - z;
cout << " x = y - z: (";
________________________________________________________________________________
// Complex class definition.
#ifndef COMPLEX_H
#define COMPLEX_H
#include <iostream>
using namespace std;
class Complex
{
friend ostream &operator<<( ostream &, const Complex & );
friend istream &operator>>( istream &, Complex & );
public:
Complex( double = 0.0, double = 0.0 ); // constructor
Complex operator+( const Complex& ) const; // addition
Complex operator-( const Complex& ) const; // subtraction
Complex operator*( const Complex& ) const; // multiplication
Complex& operator=( const Complex& ); // assignment
bool operator==( const Complex& ) const;
bool operator!=( const Complex& ) const;
private:
double real; // real part
double imaginary; // imaginary part
}; // end class Complex
#endif
// Complex class member-function definitions.
#include <iostream>
#include "Complex.h"
using namespace std;
// Constructor
Complex::Complex( double realPart, double imaginaryPart )
: real( realPart ),
imaginary( imaginaryPart )
{
// empty body
} // end Complex constructor
// addition operator
Complex Complex::operator+( const Complex &operand2 ) const
{
return Complex( real + operand2.real,
imaginary + operand2.imaginary );
} // end function operator+
// subtraction operator
Complex Complex::operator-( const Complex &operand2 ) const
{
return Complex( real - operand2.real,
imaginary - operand2.imaginary );
} // end function operator-
// Overloaded multiplication operator
Explanation / Answer
// Member function definition for class Complex
#include<iostream>
using std::cout;
# include "Complex.h"<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
using namespace std;
// Complex constructor
Complex::Complex(double realPart, double imaginaryPart)
: real (realPart), imaginary (imaginaryPart)
{
// empty body
} // end constructor Complex
// Addition operator
Complex Complex::operator+(const Complex &operand2) const
{
return Complex(real + operand2.real, imaginary + operand2.imaginary);
} // end function operator+
// Subtraction operator
Complex Complex::operator-(const Complex &operand2) const
{
return Complex(real - operand2.real, imaginary - operand2.imaginary);
} // end function operator-
// Display the Complex Object in the form (a, b)
void Complex::print() const
{ cout << "(" << real << ", " << imaginary << ")";
} // end function print
// overloading the operator <<
ostream &operator<< (ostream &out, const Complex &myComplex)
{
return out << "(" << myComplex.real << ", " << myComplex.imaginary << ")";
} // end function operator<<
// overloading the operator >>
istream &operator>> (istream &in, Complex &myComplex)
{ in >> myComplex.real >> myComplex.imaginary;
return in;
} // end function operator>>
// Multiplication operator
Complex Complex::operator*(const Complex &operand2) const
{return Complex(real * operand2.real - imaginary * operand2.imaginary,
real * operand2.imaginary + imaginary * operand2.real);
} // end function operator*
// Equality Comparision operator
bool Complex::operator==(const Complex &operand2) const
{ if ( (real == operand2.real) && ( imaginary == operand2.imaginary) )
return true;
else
return false;
} // end function operator==
// Unequality Comparision operator
bool Complex::operator!=(const Complex &operand2) const
{ if ( (real != operand2.real) || ( imaginary != operand2.imaginary) )
return true;
else
return false;
} // end function operator!=
-----------------------------------------------------------------------------
//Complex.h
// prevent multiple inclusions of header file
# ifndef COMPLEX_H
# define COMPLEX_H
#include<iostream>
using std::ostream;
using std::istream;
// Class Complex definition
class Complex
{ friend ostream &operator<<( ostream &, const Complex &);
friend istream &operator>>( istream &, Complex &);
public :
Complex( double = 0.0, double = 0.0);
Complex operator+( const Complex & ) const;
Complex operator-( const Complex & ) const;
Complex operator*( const Complex & ) const;
bool operator==( const Complex & ) const;
bool operator!=( const Complex & ) const;
void print() const;
private :
double real;
double imaginary;
};
# endif
------------------------------------------------------------------------------------
Note: Code contains more than 65000 characters, but this
Post cannot accept longer than 65000 characters. So, remain code will post next post.