B
B. Williams
I have an assignment for school to Overload the operators << and >> and I
have written the code, but I have a problem with the insertion string
function. I can't get it to recognize the second of number that are input so
it selects the values from the default constructor. Can someone assist me
with the insertion function. The code is below.
// Definition of class Complex
#ifndef COMPLEX1_H
#define COMPLEX1_H
#include <iostream>
using std:stream;
using std::istream;
class Complex
{
friend ostream &operator<<(ostream &, const Complex & ); //stream extractor
friend istream &operator>>(istream &, Complex & ); //stream inserter
public:
Complex( double = 0.0, double = 0.0 ); // constructor
Complex operator+( const Complex & ) const ; // addition
Complex operator-( const Complex & ) const ; // subtraction
const Complex &operator=( const Complex & ); // assignment
private:
double real; // real part
double imaginary; // imaginary part
};
#endif
// Member function definitions for class Complex
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include "complex1.h"
// Constructor
Complex::Complex( double r, double i )
: real( r ), imaginary( i ) { }
// Overloaded addition operator
Complex Complex:perator+( const Complex &operand2 ) const
{
return Complex( real + operand2.real,
imaginary + operand2.imaginary );
}
// Overloaded subtraction operator
Complex Complex:perator-( const Complex &operand2 ) const
{
return Complex( real - operand2.real,
imaginary - operand2.imaginary );
}
// Overloaded stream extraction operator
ostream &operator<<( ostream &output, const Complex &operand2 )
{
output << '(' << operand2.real << ", " << operand2.imaginary << ')';
return output;
}
// Overloaded stream insertion operator
istream &operator>>( istream &input, Complex &operand2 )
{
input.ignore();
input >> setw(7) >> operand2.real;
input.ignore();
input >> setw(7) >>operand2.imaginary;
return input;
}
// Overloaded = operator
const Complex& Complex:perator=( const Complex &right )
{
real = right.real;
imaginary = right.imaginary;
return * this ; // enables cascading
}
// Driver for class Complex
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include "complex1.h"
int main()
{
Complex x, y, z;
cout << "Input complex number y in the form of (2.2,2.2)" << endl;
cin >> y;
cout << "Input complex number z in the form of (2.2,2.2)" << endl;
cin >> z;
cout << "x: ";
cout << x;
cout << "\ny: ";
cout << y;
cout << "\nz: ";
cout << z;
x = y + z;
cout << "\n\nx = y + z:\n";
cout << x;
cout << " = ";
cout << y;
cout << " + ";
cout << z;
x = y - z;
cout << "\n\nx = y - z:\n";
cout << x;
cout << " = ";
cout << y;
cout << " - ";
cout << z;
cout << endl;
return 0;
}
have written the code, but I have a problem with the insertion string
function. I can't get it to recognize the second of number that are input so
it selects the values from the default constructor. Can someone assist me
with the insertion function. The code is below.
// Definition of class Complex
#ifndef COMPLEX1_H
#define COMPLEX1_H
#include <iostream>
using std:stream;
using std::istream;
class Complex
{
friend ostream &operator<<(ostream &, const Complex & ); //stream extractor
friend istream &operator>>(istream &, Complex & ); //stream inserter
public:
Complex( double = 0.0, double = 0.0 ); // constructor
Complex operator+( const Complex & ) const ; // addition
Complex operator-( const Complex & ) const ; // subtraction
const Complex &operator=( const Complex & ); // assignment
private:
double real; // real part
double imaginary; // imaginary part
};
#endif
// Member function definitions for class Complex
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include <iomanip>
using std::setw;
#include "complex1.h"
// Constructor
Complex::Complex( double r, double i )
: real( r ), imaginary( i ) { }
// Overloaded addition operator
Complex Complex:perator+( const Complex &operand2 ) const
{
return Complex( real + operand2.real,
imaginary + operand2.imaginary );
}
// Overloaded subtraction operator
Complex Complex:perator-( const Complex &operand2 ) const
{
return Complex( real - operand2.real,
imaginary - operand2.imaginary );
}
// Overloaded stream extraction operator
ostream &operator<<( ostream &output, const Complex &operand2 )
{
output << '(' << operand2.real << ", " << operand2.imaginary << ')';
return output;
}
// Overloaded stream insertion operator
istream &operator>>( istream &input, Complex &operand2 )
{
input.ignore();
input >> setw(7) >> operand2.real;
input.ignore();
input >> setw(7) >>operand2.imaginary;
return input;
}
// Overloaded = operator
const Complex& Complex:perator=( const Complex &right )
{
real = right.real;
imaginary = right.imaginary;
return * this ; // enables cascading
}
// Driver for class Complex
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
#include "complex1.h"
int main()
{
Complex x, y, z;
cout << "Input complex number y in the form of (2.2,2.2)" << endl;
cin >> y;
cout << "Input complex number z in the form of (2.2,2.2)" << endl;
cin >> z;
cout << "x: ";
cout << x;
cout << "\ny: ";
cout << y;
cout << "\nz: ";
cout << z;
x = y + z;
cout << "\n\nx = y + z:\n";
cout << x;
cout << " = ";
cout << y;
cout << " + ";
cout << z;
x = y - z;
cout << "\n\nx = y - z:\n";
cout << x;
cout << " = ";
cout << y;
cout << " - ";
cout << z;
cout << endl;
return 0;
}