473,766 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<<(ost ream &, const Complex & ); //stream extractor

friend istream &operator>>(ist ream &, 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::Comple x( double r, double i )

: real( r ), imaginary( i ) { }
// Overloaded addition operator

Complex Complex::operat or+( const Complex &operand2 ) const

{

return Complex( real + operand2.real,

imaginary + operand2.imagin ary );

}
// Overloaded subtraction operator

Complex Complex::operat or-( const Complex &operand2 ) const

{

return Complex( real - operand2.real,

imaginary - operand2.imagin ary );

}

// Overloaded stream extraction operator

ostream &operator<<( ostream &output, const Complex &operand2 )

{

output << '(' << operand2.real << ", " << operand2.imagin ary << ')';

return output;

}

// Overloaded stream insertion operator

istream &operator>>( istream &input, Complex &operand2 )

{

input.ignore();

input >setw(7) >operand2.rea l;

input.ignore();

input >setw(7) >>operand2.imag inary;
return input;

}
// Overloaded = operator

const Complex& Complex::operat or=( 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 7375

"B. Williams" <wi*******@hotm ail.comwrote in message
news:UoqMg.1018 6$JR5.4607@duke read11...
>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<<(ost ream &, const Complex & ); //stream
extractor

friend istream &operator>>(ist ream &, 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::Comple x( double r, double i )

: real( r ), imaginary( i ) { }
// Overloaded addition operator

Complex Complex::operat or+( const Complex &operand2 ) const

{

return Complex( real + operand2.real,

imaginary + operand2.imagin ary );

}
// Overloaded subtraction operator

Complex Complex::operat or-( const Complex &operand2 ) const

{

return Complex( real - operand2.real,

imaginary - operand2.imagin ary );

}

// Overloaded stream extraction operator

ostream &operator<<( ostream &output, const Complex &operand2 )

{

output << '(' << operand2.real << ", " << operand2.imagin ary << ')';

return output;

}

// Overloaded stream insertion operator

istream &operator>>( istream &input, Complex &operand2 )

{

input.ignore();

input >setw(7) >operand2.rea l;

input.ignore();

input >setw(7) >>operand2.imag inary;
return input;

}
// Overloaded = operator

const Complex& Complex::operat or=( 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.rea l;

input.ignore();

input >operand2.imagi nary;
return input;

}
Sep 9 '06 #2

B. Williams wrote:
"B. Williams" <wi*******@hotm ail.comwrote in message
news:UoqMg.1018 6$JR5.4607@duke read11...
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<<(ost ream &, const Complex & ); //stream
extractor

friend istream &operator>>(ist ream &, 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::Comple x( double r, double i )

: real( r ), imaginary( i ) { }
// Overloaded addition operator

Complex Complex::operat or+( const Complex &operand2 ) const

{

return Complex( real + operand2.real,

imaginary + operand2.imagin ary );

}
// Overloaded subtraction operator

Complex Complex::operat or-( const Complex &operand2 ) const

{

return Complex( real - operand2.real,

imaginary - operand2.imagin ary );

}

// Overloaded stream extraction operator

ostream &operator<<( ostream &output, const Complex &operand2 )

{

output << '(' << operand2.real << ", " << operand2.imagin ary << ')';

return output;

}

// Overloaded stream insertion operator

istream &operator>>( istream &input, Complex &operand2 )

{

input.ignore();

input >setw(7) >operand2.rea l;

input.ignore();

input >setw(7) >>operand2.imag inary;
return input;

}
// Overloaded = operator

const Complex& Complex::operat or=( 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.rea l;

input.ignore();

input >operand2.imagi nary;
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
354
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 "daytime.h" #include <string> #include <fstream>
3
1500
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 of specification, as friends in the class that will use them? Thanks, TJ
2
2041
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" facility. I am to get rid of them and replace them with exceptions. I need to create a "linkedListException" class that's declared and implemented in my "linkedListType" class. This class needs to inherit from the base "exception" class and return...
7
6761
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 program with a bunch of classes which have the output stream operators << of their own. The overloaded operators work fine when inserted into the std:cout stream but I don't know how to make the logging class accept them. If I try to compile the...
5
2101
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& std::operator>>( std::istream& is, jme::Name& obj ); ---name.cpp std::ostream& std::operator<<( std::ostream& os, const jme::Name& obj ) { return os << obj.str;
2
1725
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; m_result = os.str(); }
3
3034
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 buffer etc, but nothing worked. Ideas? Also might use another internal construct, only API is needed and requirement to reuse existing std::ostream inserters.
5
10993
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 objects have constructors that allow implicit conversions from simple types. All this has been defined as follows: <code> class Int
1
1373
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. My teacher told me that all functions in it should be cascadable. plz plz point out my mistakes.I have to submit it on monday. #include<iostream.h> #include<conio.h>
0
9568
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9959
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.