473,241 Members | 1,534 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

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 7330

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Revman | last post by:
I'm having problems opening and reading from a file to test a Class. My diver main.cpp is fairly short but with a longer file open function // Project #4 -- Main/driver program #include...
3
by: TJ | last post by:
Hi, I've been referring to many codes and books and always see that the stream insertion operators are overloaded as friends. Why is that? Are there any other overloading that need the same type...
2
by: dinks | last post by:
Hi, I'm new to C++ and have been assigned a task which i dont completely understand. Any help would be greately appreciated. Here is the problem: The class "linkedListType" use the "assert"...
7
by: Riku Jarvinen | last post by:
Hello everyone, I have a logging class which writes program outputs to the logfile. The class works fine as long as only C++ native data types are considered. The problem is that I have a...
5
by: jalkadir | last post by:
I my program I have overloaded the inserters and extractor operator. ---name.hpp friend std::ostream& std::operator<<( std::ostream& os, const jme::Name& obj ); friend std::istream&...
2
by: shaun roe | last post by:
Following up from my earlier post, where I pursued the line I outlined. Here is the MYFunc class implementation: MyFunc::MyFunc(int i):m_i(i){ ostringstream os; os<<"This number is "<<m_i;...
3
by: sven.suursoho | last post by:
Hello, In main(), the first output API is what I try to achieve. Unfortunately it fails, printing first string as pointer instead of human readable message. Tried to initialize str(""), set new...
5
by: rolandz | last post by:
Hi, Maybe somebody has been fighting with the problem that I do, currently. I have a class that has method f(). The two versions of the f() method accept different objects: Int and Short. These...
1
by: anumliaqat | last post by:
hello!!! I am new to this site.can anyone help me in my program.It is of rational number class in c++ having operator overloading.It is not executing and i am not able to find mistakes. ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.