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.
Linda1 -6 Newbie Poster
#include<iostream>
#include "complex0.h"
using namespace std;
ostream& operator<<(ostream& osObject, const complex0& complex)
{
osObject<< "("; //output left parenthesis
osObject<< complex.realPart; //output real part
osObject<< ", "; //output comma and space
osObject<<complex.imaginaryPart; //output imaginary part
osObject<< "i)"; //output right parenthesis and i
return osObject; //return ostream object
}
istream& operator>>(istream& isObject, complex0& complex)
{
// char ch;
//isObject >> ch; //read and discard left parenthesis
cout << "real: ";
isObject >> complex.realPart; // read and store real part
cout << "imaginary: ";
isObject >> complex.imaginaryPart; //read and store imaginary part
//isObject >> ch;
return isObject; // return istream object
}
//constructor
complex0::complex0(double real, double imag)
{
realPart = real;
imaginaryPart = imag;
}
void complex0::setComplex(const double& real, const double& imag)
{
realPart = real;
imaginaryPart = imag;
}
//overload operator +
complex0 complex0::operator+ (const complex0& otherComplex) const
{
complex0 temp;
temp.realPart = realPart + otherComplex.realPart;
temp.imaginaryPart = imaginaryPart + otherComplex.imaginaryPart;
return temp;
}
//overload operator -
complex0 complex0::operator- (const complex0& otherComplex) const
{
complex0 temp;
temp.realPart = realPart - otherComplex.realPart;
temp.imaginaryPart = imaginaryPart - otherComplex.imaginaryPart;
return temp;
}
//overload operator*
complex0 complex0::operator* (const complex0& otherComplex) const
{
complex0 temp;
temp.realPart = (realPart * otherComplex.realPart) - (imaginaryPart * otherComplex.imaginaryPart);
temp.imaginaryPart = (realPart * otherComplex.imaginaryPart)
+ (imaginaryPart * otherComplex.realPart);
return temp;
}
//overload operator~
complex0 complex0::operator~ ()
{
complex0 temp;
temp.realPart = (realPart);
temp.imaginaryPart = -(imaginaryPart);
return temp;
}
#include<iostream>
using namespace std;
#include "complex0.h"
int main()
{
complex0 num1(3.0, 4.0); //INITIALIZE to (3, 4i)
complex0 real;
complex0 imaginary;
complex0 c;
cout << "Enter a complex number in form (a, b)(q to quit):\n";
cin >> real;
cout << endl;
cout << "c is " << real << " " << endl;
cout << "a is " << num1 << '\n';
cout << "Complex conjugate is " << ~real << endl;
/*{
// cout << "complex conjugate is " << ~c << '\n';
cout << "a is " << a <<'\n';
cout << "a + c is " << a + c << '\n';
cout << "a - c is " << a - c << '\n';
cout << "a * c is " << a * c << '\n';
// cout << "2 * c is " << 2 * c << '\n';
cout << "Enter a complex number (q to quit);\n";
}
cout << "Done!\n"; */
return 0;
}
#ifndef H_complexNumber
#define H_complexNumber
#include<iostream>
using namespace std;
class complex0
{
//overload the stream insertion and extraction operators
friend ostream& operator<<(ostream&, const complex0&);
friend istream& operator>>(istream&, complex0&);
public:
void setComplex(const double& real, const double& imag);
//funtion to set complex numbers according to parameters. realPart = real; imaginaryPart = imag;
void getComplex(double& real, double& imag) const;
//Function to retrieve the complex number
//post condition: real = realPart; imag = imaginaryPart;
complex0(double real = 0, double imag = 0);
//constructor; initializes according to paramneters
complex0 operator+ (const complex0& otherComplex) const; //overload operator +
complex0 operator- (const complex0& otherComplex) const; //overload operator -
complex0 operator* (const complex0& otherComplex) const; //overload operator *
private:
double realPart; //variable to store real part
double imaginaryPart; //variable to store imaginary part
};
#endif
selvaganapathy 31 Posting Pro
Hi,
Why dont use friend function for overloading *
Syntax
friend complex0 operator*(int, complex0&);
friend complex0 operator*(complex0&, int);
Linda1 -6 Newbie Poster
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?
selvaganapathy 31 Posting Pro
//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;
}
Linda1 -6 Newbie Poster
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;
}
Linda1 -6 Newbie Poster
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.