Write a console program that implements this class. In main, write a “driver pro
ID: 3620231 • Letter: W
Question
Write a console program that implements this class. In main, write a “driver program” that fully tests the functionality of the class. In main, you will need to create some objects, add and subtract them, compare them, and output the results.class Complex {
friend ostream & operator<<( ostream &, const Complex & );
friend istream & operator>>( istream &, Complex & );
public:
// Constructor
Complex( double = 0.0, double = 0.0 );
// Arithmetic Operators
Complex operator+( const Complex & ) const; // addition
Complex operator-( const Complex & ) const; // subtraction
//Complex operator*( const Complex & ) const; // multiplication OPTIONAL
// Comparison Operators
bool operator==( const Complex & ) const;
bool operator!=( const Complex & ) const;
// Assignment Operator
// will work fine without this because there
// is no pointers to memory outside the class,
// but do anyway for practice
Complex& operator=( const Complex & );
// Handy access function
void setValue(double, double );
private:
double real;
double imaginary;
};
the code i have works, however all of the required output is not covered. this is what i have:
#include <iostream>
using namespace std;
class Complex
{
//cout, usually
friend ostream & operator<<( ostream &, const Complex & );
friend istream & operator>>( istream &, Complex & );
public:
// Constructor
Complex( double = 0.0, double = 0.0 );
// Arithmetic Operators
Complex operator+( const Complex & ) const; // addition
Complex operator-( const Complex & ) const; // subtraction
//Complex operator*( const Complex & ) const; // multiplication OPTIONAL
// Comparison Operators
bool operator==( const Complex & ) const;
bool operator!=( const Complex & ) const;
// Assignment Operator
// will work fine without this because there
// is no pointers to memory outside the class,
// but do anyway for practice
Complex & operator=( const Complex & );
// Handy access function
void setValue(double, double );
private:
double real;
double imaginary;
};
Complex & Complex::operator= ( const Complex & Bob)
{
//assign values to the imp (left arg)
real = Bob.real;
imaginary = Bob.imaginary;
//return something for chaining
return *this;//
}
Complex::Complex( double r, double i )
{
real = r; imaginary =i;
}
Complex Complex::operator+( const Complex & Bob) const // addition
{
Complex temp;//new object
temp.real = real + Bob.real;
temp.imaginary = imaginary + Bob.imaginary;
return temp;
}
Complex Complex::operator-( const Complex & Bob) const // addition
{
Complex temp;//new object
temp.real = real - Bob.real;
temp.imaginary = imaginary - Bob.imaginary;
return temp;
}
bool Complex::operator==( const Complex & Bob) const
{
if (real == Bob.real && imaginary == Bob.imaginary)
return true;
else
return false;
}
bool Complex::operator!=( const Complex & Bob) const
{
return !(*this != Bob);
}
ostream & operator<<( ostream & stream, const Complex & Bob)
{
stream << Bob.real << " + "<< Bob.imaginary << "i";
return stream;// for chaining on the calling side
}
istream & operator>>( istream & stream, Complex & Bob)
{
stream >> Bob.real >> Bob.imaginary;
return stream;//chaining
}
int main ()
{
Complex C1(1.1, 2.2);
Complex C2(3.3, 4.3);
Complex C3;
C3 = C1 + C2;
cout<<C3<<endl;
if (C1 == C1)
cout << "Equal ";
else (C1 = C2);
cout << "!= ";
cout<< C1<<C1<<C1;
Complex C4;
cin >> C4;
cout <<" "<< C4;
C4 = C1 - C2;
return 0;
}
Explanation / Answer
please rate - thanks I think everything is covered I made a change in your input so that it provides prompts#include <iostream>
using namespace std;
class Complex
{
//cout, usually
friend ostream & operator<<( ostream &, const Complex & );
friend istream & operator>>( istream &, Complex & );
public:
// Constructor
Complex( double = 0.0, double = 0.0 );
// Arithmetic Operators
Complex operator+( const Complex & ) const; // addition
Complex operator-( const Complex & ) const; // subtraction
//Complex operator*( const Complex & ) const; // multiplication OPTIONAL
// Comparison Operators
bool operator==( const Complex & ) const;
bool operator!=( const Complex & ) const;
// Assignment Operator
// will work fine without this because there
// is no pointers to memory outside the class,
// but do anyway for practice
Complex & operator=( const Complex & );
// Handy access function
void setValue(double, double );
private:
double real;
double imaginary;
};
Complex & Complex::operator= ( const Complex & Bob)
{
//assign values to the imp (left arg)
real = Bob.real;
imaginary = Bob.imaginary;
//return something for chaining
return *this;//
}
Complex::Complex( double r, double i )
{
real = r; imaginary =i;
}
Complex Complex::operator+( const Complex & Bob) const // addition
{
Complex temp;//new object
temp.real = real + Bob.real;
temp.imaginary = imaginary + Bob.imaginary;
return temp;
}
Complex Complex::operator-( const Complex & Bob) const // addition
{
Complex temp;//new object
temp.real = real - Bob.real;
temp.imaginary = imaginary - Bob.imaginary;
return temp;
}
bool Complex::operator==( const Complex & Bob) const
{
if (real == Bob.real && imaginary == Bob.imaginary)
return true;
else
return false;
}
bool Complex::operator!=( const Complex & Bob) const
{
if (real == Bob.real && imaginary == Bob.imaginary)
return false;
else
return true;
}
ostream & operator<<( ostream & stream, const Complex & Bob)
{
stream << Bob.real << " + "<< Bob.imaginary << "i";
return stream;// for chaining on the calling side
}
istream & operator>>( istream & stream, Complex & Bob)
{
cout<<"Enter real part: ";
stream >> Bob.real;
cout<<"Enter imaginary part: ";
stream >> Bob.imaginary;
return stream;//chaining
}
int main ()
{
Complex C1(1.1, 2.2);
Complex C2(3.3, 4.3);
Complex C3;
cout<<"C1="<<C1<<endl;
cout<<"C2="<<C2<<endl;
C3 = C1 + C2;
cout<<"C1+C2=C3="<<C3<<endl;
if (C1 == C2)
cout << " C1 and C2 are Equal ";
else
cout << " C1 and C2 are != ";
C2=C1;
cout<<"C1="<<C1<<endl;
cout<<"C2="<<C2<<endl;
if (C1 != C2)
cout << " C1 and C2 are not Equal ";
else
cout << " C1 and C2 are equal ";
Complex C4;
cin >> C4;
cout <<" ";
cout<<C4;
cout<<endl;
C4 = C1 - C2;
cout<<"C1="<<C1<<endl;
cout<<"C2="<<C2<<endl;
cout<<"C1-C2=C4="<<C4<<endl;
system("pause");
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.