I am practically done with this project but got stumped in one area
I am trying to use operator overloading and multiplying 2 to the complex number.
Since I used operator* already, how do I call the operator with 2* complex no?
How do I write the code for it? Just one tiny issue. Others are compiling fine.

Recommended Answers

All 5 Replies

Why dont use friend function for overloading *

friend complex0 operator*(int, complex0&);
 friend complex0 operator*(complex0&, int);

Just learning C++.
I know we use that friend in inheritance

so, how do I code this;
friend complex0 operator*(int, complex0&)
num3 = 2 * realPart;
num3 = imaginaryPart * 2;
How do i call it at main?

//overload operator*
it is similar to overloading << and >>
Just give declaration inside complex0 class as friend. friend complex0 operator* (int no, complex0& com);

complex0 operator* (int no, complex0& com)
	complex0 temp;
	temp.realPart = no*com.realPart;
	temp.imaginaryPart =  no*com.imaginaryPart;
	return temp;

I got the issue resolved using friend function
Here's the code:
Under .h file header
friend complex0 square(const complex0 x);

Then under cpp file
complex0 square(const complex0& x)
double realPart, imaginaryPart;
complex0 sq;
sq.realPart = realPart * 2;
sq.imaginaryPart = 2.0 * realPart * imaginaryPart;
return sq;

selvagonopathy's code does not work. I tried that before coming into the forum. The friend function idea was good, but I did not know how to code. After bugging quite a few programmers, managed to get the right code. Thanks for your inputs!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.