Hi,everyone
I have to write a calculator which can + , - , * , / , + = ,- = , * = , /= ,++,-- do this operations with operator overloading

#include<iostream>

using namespace std;

class complexNumber
{
private:
    double realPart;
    double imaginaryPart;
public:
    complexNumber(){realPart=0.0; imaginaryPart=0.0;}
    ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}

    double get_real(){return realPart;}
    double get_imaginary(){return imaginaryPart;}

    void set_real(double r){realPart=r;}
    void set_imaginary(double i){imaginaryPart=i;}

    complexNumber operator+(const complexNumber& c);
    complexNumber operator-(const complexNumber& c);
    
};
void main()
{
    complexNumber c1,c2,c3;
    c1.set_real(3.4);
    c1.set_imaginary(4.5);
    c2.set_real(5.6);
    c2.set_imaginary(6.2);
    c3= c1 + c2;
    cout<< c1 <<endl<< c2 <<endl<< c3 <<endl;

}

i wrote that code according to some topics in this forum but it gives me a error

error C2679: binary '<<' : no operator defined which takes a right-hand operand of type 'class complexNumber' (or there is no acceptable conversion)

How can i fix this?
Also i have to write a display function to print complex numbers according to my code i can not do it please also help me in this problem...

Have a good day...
P.S. i have just registered yet sorry if i did sth wrong...:rolleyes:

Member Avatar for iamthwee

Hi,everyone
I have to write a calculator which can + , - , * , / , + = ,- = , * = , /= ,++,-- do this operations with operator overloading

#include<iostream>

using namespace std;

class complexNumber
{
private:
    double realPart;
    double imaginaryPart;
public:
    complexNumber(){realPart=0.0; imaginaryPart=0.0;}
    ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}

    double get_real(){return realPart;}
    double get_imaginary(){return imaginaryPart;}

    void set_real(double r){realPart=r;}
    void set_imaginary(double i){imaginaryPart=i;}

    complexNumber operator+(const complexNumber& c);
    complexNumber operator-(const complexNumber& c);
    
};
void main()
{
    complexNumber c1,c2,c3;
    c1.set_real(3.4);
    c1.set_imaginary(4.5);
    c2.set_real(5.6);
    c2.set_imaginary(6.2);
    c3= c1 + c2;
    cout<< c1 <<endl<< c2 <<endl<< c3 <<endl;

}

i wrote that code according to some topics in this forum but it gives me a error
How can i fix this?
Also i have to write a display function to print complex numbers according to my code i can not do it please also help me in this problem...

Have a good day...
P.S. i have just registered yet sorry if i did sth wrong...:rolleyes:

You haven't defined the methods for operator overloading so what do you expect. Miracles?

The method for overloading the << operator is slightly different. You have to be careful when doing that.

Also this:

c1.set_real(3.4);

should be:

c1.set_real(3,4); <-- separated with a comma.

And exchange void main with int main. Carry on...

The method for overloading the << operator is slightly different. You have to be careful when doing that.

How can i do that?Please give me some code examples?

Member Avatar for iamthwee

Well without actually handing out the code for a complex number class the following thread should make things clearer.

It's a fraction class but you should get an idea of what you need to do. It can probably simplified as well.

http://www.daniweb.com/techtalkforums/thread44082.html

i have looked that topic but i couldn't get the main idea of overloading "<<" operator...
Can you give me a little code for cout complexNumbers from my code???
This code is working but can not print my complex numbers...
Can you edit this code's "<<" operator for me???or give me an idea please...

#include<iostream>

using namespace std;

class complexNumber
{
private:
    double realPart;
    double imaginaryPart;
public:
    complexNumber(){realPart=0.0; imaginaryPart=0.0;}
    ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}

    double get_real(){return realPart;}
    double get_imaginary(){return imaginaryPart;}

    void set_real(double r){realPart=r;}
    void set_imaginary(double i){imaginaryPart=i;}

    friend ostream &operator<<( ostream &output, complexNumber &c )
{
   
    return output <<  c.realPart << " / " << c.imaginaryPart;
    
}
    complexNumber operator+(complexNumber c)
    {
    complexNumber temp;
    int temp_realPart,temp_imaginaryPart;
    
    temp_realPart= realPart * c.get_imaginary() + c.get_real()* imaginaryPart;
    temp_imaginaryPart = imaginaryPart * c.get_imaginary();
    
    temp.set_real(temp_realPart);
    temp.set_imaginary(temp_imaginaryPart);
    

    return temp;
}
//    complexNumber operator-(const complexNumber& c);
    
};
int main()
{
    complexNumber c1,c2,c3;
    c1.set_real(3.4);
    c1.set_imaginary(4.5);
    c2.set_real(5.6);
    c2.set_imaginary(6.2);
    c3= c1 + c2;
    cout<<c3<<endl;
    
return 0;
}
Member Avatar for iamthwee

You've more or less got it. I've just highlighted the stuff I've changed. Now you have to finish the methods for overloading the other operators.

Carry on...

#include<iostream>

using namespace std;

class complexNumber
{
private:
    double realPart;
    double imaginaryPart;
public:
    complexNumber(){realPart=0.0; imaginaryPart=0.0;}
    ~complexNumber(){cout<<"Destructor do it's work..!"<<endl;}

    double get_real(){return realPart;}
    double get_imaginary(){return imaginaryPart;}

    void set_real(double r){realPart=r;}
    void set_imaginary(double i){imaginaryPart=i;}

    friend ostream &operator<<( ostream &output, complexNumber &c )
    {
   
       return output <<  c.realPart << " + " << c.imaginaryPart <<"i";
    
    }
    complexNumber operator+(complexNumber c)
    {
       complexNumber temp;
       double temp_realPart,temp_imaginaryPart;
    
       temp_realPart= realPart  + c.get_real();
       temp_imaginaryPart = imaginaryPart + c.get_imaginary();
    
       temp.set_real(temp_realPart);
       temp.set_imaginary(temp_imaginaryPart);
    
       return temp;
    }
//    complexNumber operator-(const complexNumber& c);
    
};
int main()
{
    complexNumber c1,c2,c3;
    c1.set_real(3.4);
    c1.set_imaginary(4.5);
    c2.set_real(5.6);
    c2.set_imaginary(6.2);
    c3= c1 + c2;
    cout<< c3<<endl;
    
    cin.get();
    
return 0;
}

Thanx a lot..!I'm understanding my mistakes now,i just start to write operators if i'll get in trouble,maybe i'll ask sth also...

Thanx for your patience..!

Member Avatar for iamthwee

I'm just learning this myself. He he.

I hope I not mis-leading you. :lol:

I've finished writing Basic Complex Numbers Calculator thank you very much...!
have a nice day...

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.