Help with Overloaded stream insertion operator function

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::eek: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::eek:perator+( const Complex &operand2 ) const

{

return Complex( real + operand2.real,

imaginary + operand2.imaginary );

}


// Overloaded subtraction operator

Complex Complex::eek: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::eek: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;

}
 
B

B. Williams

B. Williams said:
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::eek: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::eek:perator+( const Complex &operand2 ) const

{

return Complex( real + operand2.real,

imaginary + operand2.imaginary );

}


// Overloaded subtraction operator

Complex Complex::eek: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::eek: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;

}
Please disregard this post. I figured it out. I just changed the function to
read
istream &operator>>( istream &input, Complex &operand2 )

{

input >> operand2.real;

input.ignore();

input >> operand2.imaginary;


return input;

}
 
S

Salt_Peter

B. Williams said:
Please disregard this post. I figured it out. I just changed the function to
read
istream &operator>>( istream &input, Complex &operand2 )

{

input >> operand2.real;

input.ignore();

input >> operand2.imaginary;


return input;

}

Ok, but you might consider rearranging the signature of that function
like so:

std::istream& operator>>( std::istream& input, Complex& operand2 )
{
...
}

the return type as well as the two parameters are references.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top