Click here to Skip to main content
15,885,366 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.
C++
// Operator_Overloading_C++_2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
using namespace std;
class overload
{
private:
	int real,imgnary;
public:
	overload(int r=0,int i=0)
{
	real=r;
	imgnary=i;
}
	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)
{
	user<<'endl';
	return user;
}
istream & operator>>(istream & input,overload & a)
{
	cout<<"Real Number: ";
	input>>a.real;
	cout<<"Imaginary Number:";
	input>>a.imgnary;
	return input;
}
ostream & operator<<(ostream & output,overload & b)
{
	output<<"Addition:"<<b.real+b.imgnary<<endl;;
	output<<"Subtraction:"<<b.real-b.imgnary<<endl;;
	output<<"Multiplication:"<<b.real*b.imgnary<<endl;;
		return output;
}
void overload::input()
{
	cout<<"Enter Number:";
	cin>>real;
	cout<<"Enter Again:";
	cin>>imgnary;
}

overload& overload::operator+(overload c1)
{
	overload temp;
	temp=real+c1.real;
	temp=imgnary+c1.imgnary;
	return temp;
}
void overload::display()
{
	cout<<real<<"+"<<imgnary<<endl;
}
int _tmain(int argc, _TCHAR* argv[])
{
	overload over,over_1,all;
	cin>>over;
	cout<<over<<endl;
//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.
	all=over.input();
	all.display();
	system("pause");
	return 0;
}
Posted
Updated 15-Aug-14 10:24am
v4
Comments
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

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

You are returning a reference to a temporary object which is really bad.
C++
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:
C++
overload &operator+=(const overload &other)
{
  r += other.r;
  i += other.i;
  return *this;
}

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

or alternatively
C++
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.
C++
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.
 
Share this answer
 
v2
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
-1.5i
3.34
(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:
http://www.cplusplus.com/reference/complex/complex[^].

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.

—SA
 
Share this answer
 

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