468,251 Members | 1,464 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,251 developers. It's quick & easy.

Help with Overloaded stream insertion operator function

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::ostream;

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

{

return Complex( real + operand2.real,

imaginary + operand2.imaginary );

}
// Overloaded subtraction operator

Complex Complex::operator-( 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::operator=( 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;

}
Sep 9 '06 #1
2 7059

"B. Williams" <wi*******@hotmail.comwrote in message
news:UoqMg.10186$JR5.4607@dukeread11...
>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::ostream;

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

{

return Complex( real + operand2.real,

imaginary + operand2.imaginary );

}
// Overloaded subtraction operator

Complex Complex::operator-( 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::operator=( 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;

}
Sep 9 '06 #2

B. Williams wrote:
"B. Williams" <wi*******@hotmail.comwrote in message
news:UoqMg.10186$JR5.4607@dukeread11...
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::ostream;

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

{

return Complex( real + operand2.real,

imaginary + operand2.imaginary );

}
// Overloaded subtraction operator

Complex Complex::operator-( 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::operator=( 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;

}
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.

Sep 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Revman | last post: by
3 posts views Thread by TJ | last post: by
2 posts views Thread by dinks | last post: by
7 posts views Thread by Riku Jarvinen | last post: by
5 posts views Thread by jalkadir | last post: by
2 posts views Thread by shaun roe | last post: by
3 posts views Thread by sven.suursoho | last post: by
1 post views Thread by anumliaqat | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.