I'm trying to write a program that uses overloading of operators to perform operations (add, multiply, etc.). I've got the program done, but I'm having trouble modifying the driver I have to fit the program. The original program used a print method, and the new program uses overloading input and output operators. I'm having trouble using the overloaded output and input operators. I know that I have to use them instead of using the print() function, but I'm not sure how to do that.
This is the complex.cpp file:
#include "complex.h"
#include <iostream>
using namespace std;
//using std::ostream;
//using std::istream;
// Constructor
Complex::Complex( double realPart, double imaginaryPart )
{
real = realPart;
imaginary = imaginaryPart;
} // end class 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-
// multiplication operator
Complex Complex::operator*( const Complex &operand2 ) const
{
return Complex( real * operand2.real,
imaginary * operand2.imaginary );
} // end function operator*
bool Complex::operator==( const Complex &operand2 ) const
{
if((real == operand2.real) && (imaginary == operand2.imaginary))
{
return true;
}
else
{
return false;
}
} // end function operator==
bool Complex::operator!=( const Complex &operand2 ) const
{
if((real != operand2.real) && (imaginary != operand2.imaginary))
{
return true;
}
else
{
return false;
}
} // end function operator!=
// Overloaded << operator
ostream& operator<<( ostream &output, const Complex &complex )
{
output << complex.real << " + " << complex.imaginary << 'i';
return output;
} // end function operator<<
// Overloaded >> operator
istream& operator>>( istream &input, Complex &complex )
{
input >> complex.real;
input.ignore( 3 ); // skip spaces and +
input >> complex.imaginary;
input.ignore( 2 );
return input;
} // end function operator>>
This is the complex.h 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 ); // 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_H
This is the original driver:
#include <iostream>
using std::cout;
using std::endl;
#include "Complex.h"
int main()
{
Complex x;
Complex y( 4.3, 8.2 );
Complex z( 3.3, 1.1 );
cout << "x: ";
x.print();
cout << "\ny: ";
y.print();
cout << "\nz: ";
z.print();
x = y + z;
cout << "\n\nx = y + z:" << endl;
x.print();
cout << " = ";
y.print();
cout << " + ";
z.print();
x = y - z;
cout << "\n\nx = y - z:" << endl;
x.print();
cout << " = ";
y.print();
cout << " - ";
z.print();
cout << endl;
return 0;
} // end main
Thanks in advance for your help!