Click here to Skip to main content
15,351,229 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello Everyone!

I Am doing Operator overloading in C++. but at one point i got the Problem,when i take input TWO Times from The User for operator overloading then Program Run Perfect But i want to Take Input only one time From the User and then SHOW Output.

Please See the Code Below.
// Operator_Overloading_C++_2.cpp : Defines the entry point for the console application.

#include "stdafx.h"
using namespace std;
class overload
	int real,imgnary;
	overload(int r=0,int i=0)
	friend ostream & operator<<(ostream &,overload &);
	friend istream & operator>>(istream &,overload &);
	ostream & newline(ostream &);
	overload& operator+(overload c1);
	void input();
	void display();
ostream & newline(ostream & user)
	return user;
istream & operator>>(istream & input,overload & a)
	cout<<"Real Number: ";
	cout<<"Imaginary Number:";
	return input;
ostream & operator<<(ostream & output,overload & b)
		return output;
void overload::input()
	cout<<"Enter Number:";
	cout<<"Enter Again:";

overload& overload::operator+(overload c1)
	overload temp;
	return temp;
void overload::display()
int _tmain(int argc, _TCHAR* argv[])
	overload over,over_1,all;
//Here i Got the Problem.//if i take input two time//call input Function two Time Then Program run Perfect but i want to Call Input Function Only For One Time.
	return 0;
Updated 15-Aug-14 10:24am
Sergey Alexandrovich Kryukov 15-Aug-14 16:27pm
You screwed up HTML formatting of C++ code pretty well, by escaping some angular brackets and not others. Also, you need to sandwich the code in "pre" tag. Please, click "Improve question" and:
1) see how I formatted it using the "pre" tag and escaped angular brackets to fix your formatting, do the same next time;
2) review the source code to make sure I did not break it by my edition.

Now, your comment "got a problem" is between lines. I am confused. Is it next line? Please put this comment on right of the right line

Sergey Alexandrovich Kryukov 15-Aug-14 16:28pm
What is 'endl'?

You are returning a reference to a temporary object which is really bad.
overload& overload::operator+(overload c1)

You should return a object and not a reference.

By the way, you should almost always overload operator += instead which would return a reference to this and then implement binary operator + using that code.

By the way, it make no sense to store integer in a complex number class (and it make no sense to reinvent the well instead of using an existing comple number class). That way, you avoid all those problems. Usually you would write such class only for educationnal purpose.

Anyway, the implementation should look like this:
overload &operator+=(const overload &other)
  r += other.r;
  i += other.i;
  return *this;

And outside the class definition, you would have:
overload operator+(const overload &lhs, const overload &rhs)
  overload result(lhs);
  result += rhs;
  return result;

or alternatively
overload operator+(const overload &lhs, const overload &rhs)
  return overload(lhs) += rhs;

As an added benefit, you don't need friend declaration and you gain symmetry if you have mixed type expression.
overload a(1, 2);
overload b = a + 1;
overlaod c = 2 + a;

Finally since operator+= is defined, then you can write more efficient code when you don't need a new variable for the result.

There are a few other things that might be improved in your code. It could be a good think to read books on how to write Professional grade C++ code. Book like "Exceptionnal C++" are really good.

Also, I don't really understand exactly your problem. it could have beeen a good idea to reduce the code to only the code necessary to show the problem and explain it better.
Please see my comment. Your "got a problem" comment is between lines. It could not be about the line above, because there is no input in it. Then in should be about all=over.input();. But how can you complain about it, if you explicitly put two separate input operations in it? So, what else would you expect.

I think your problem is simple. You probably need to make user input one complex number at once, if a forms like:
3.22 + 17.1i
22i + 11.34343
(each line above is the example of one single input; the last case is also a complex number with zero imaginary part, don't miss such cases).

Then, you need to parse this expression into real and imaginary part and initialize the complex number. Also, don't use your real, imgnary (correct: imaginary!) fields: always use available complex template type:[^].

And finally, this your absolutely the worse problem: immediate constants "Real Number: ", "Imaginary Number:" and the like. Very, very bad.
(By the way, you are requesting imaginary part of complex number, not "Imaginary number". Do you really understand complex numbers? I'm not sure.)

Never ever using those strings in operator definitions. And never ever use input output/operators in definitions of input/output operators. Those points, especially the first one, are so obvious that I don't even have hard to explain further. Just use you logic. And just don't.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900