-4

Overload Operator + to implement below operations:

  1. Obj1 + Obj2
  2. 12 + Obj2
  3. Obj1 + 10

Overload Operator - to implement below operations:

  1. Obj1 - Obj2
  2. 12 - Obj2
  3. Obj1 - 10

Mainly looking to overload: Obj1 - 10, 12 - Obj2, 12 + Obj2, Obj1 + 10 cases I wanted to overload operator + and - such that above all operation are handled. How to handle these operation/cases? Here I am facing problem with 2nd case. I am thinking to write only one function each for + and - to handle these case.

#include<iostream>
using namespace std;

class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {real = r;   imag = i;}

     Complex operator + (Complex const &obj) {
     Complex res;
     res.real = this->real + obj.real;
     res.imag = this->imag + obj.imag;
     return res;
}

     Complex operator + (int i) {
     Complex res;
     res.real = this->real + i;
     res.imag = this->imag ;
     return res;
}
    Complex operator - (Complex const &obj) {
      Complex res;
     res.real = this->real - obj.real;
     res.imag = this->imag - obj.imag;
     return res;
    }
  Complex operator - (int i) {
      Complex res;
     res.real = this->real - i;
     res.imag = this->imag ;
     return res;
    }
    void print() { cout << real << " + i" << imag << endl; }
};  

int main()
{
    Complex Obj1(10, 5), Obj2(2, 4);
    Complex Obj3 = Obj1 + Obj2; 
    Complex Obj4 = 10 + Obj3; 
    Complex Obj5 = Obj4 + 15;
    cout<<" + operation:"<<endl;
    Obj3.print();
    Obj4.print();
    Obj5.print();
    Complex Obj6 = Obj1 - Obj2; 
    Complex Obj7 = 10 - Obj3; 
    Complex Obj8 = Obj4 - 15;
    cout<<" - operation:"<<endl;
    Obj6.print();
    Obj7.print();
    Obj8.print();
}

Expected Output:

 + operation:    
 12 + i9     
 22 + i9     
 37 + i9   
 - operation:   
 8 + i    
 2 + i9     
 7 + i9    

Getting below error:

error: no match for 'operator+' (operand types are 'int' and 'Complex')
         Complex Obj4 = 10 + Obj3; 
HarshGiri
  • 408
  • 2
  • 12
  • So.. What is your question? What, exactly, is the problem you are having? – Algirdas Preidžius Jul 11 '18 at 09:08
  • You can overload operators as member functions, or as non-member functions. I suggest you [get a few good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282) to read all about it. – Some programmer dude Jul 11 '18 at 09:09
  • https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading?rq=1 – Mat Jul 11 '18 at 09:10
  • 1
    Whats the reason for so many downvotes for a valid question? –  Jul 11 '18 at 09:16
  • Yes I do not understand the reason of downvotes, I think the did not read whole question, great guys keep it up – HarshGiri Jul 11 '18 at 09:18
  • @AlgirdasPreidžius I have updated the problem that I am facing hope you understand it. – HarshGiri Jul 11 '18 at 09:20
  • 1
    OP, does the link provided by Mat helps? We think it could be a dup' – YSC Jul 11 '18 at 09:22
  • @mfromla No research? Very little effort? A couple of entries on http://idownvotedbecau.se/ are valid. – Some programmer dude Jul 11 '18 at 09:24
  • @Someprogrammerdude please let me know whats your reason to downvote and please help me out with the answer to the question if you can. – HarshGiri Jul 11 '18 at 09:28
  • @Some programmer dude - Instead of downvoting you can guide him to appropriate link where he can learn and find solution. "No research" is valid reason but is unreasonable for downvoting when an answer is needed specifically for your question and probably quickly and if asker is a beginner. –  Jul 11 '18 at 09:33
  • To begin with, how about http://idownvotedbecau.se/noresearch/? And since the code seems to be a template given by a teacher then http://idownvotedbecau.se/noattempt/? – Some programmer dude Jul 11 '18 at 09:39
  • This could help you: https://en.cppreference.com/w/cpp/language/operators There are some examples. – Missu Jul 11 '18 at 09:41
  • @HarshGiri You stated that you are "Having problem with the 2nd case", but didn't state what that problem is. – Algirdas Preidžius Jul 11 '18 at 09:44
  • @AlgirdasPreidžius mentioned please check – HarshGiri Jul 11 '18 at 09:49
  • @Someprogrammerdude I was getting error that I mentioned , please check now – HarshGiri Jul 11 '18 at 09:50
  • Possible duplicate of [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – Tadeusz Kopec for Ukraine Jul 11 '18 at 10:07

1 Answers1

1

It should not be like this Complex Obj4 = 10 + Obj3; Instead it should be like this Complex Obj4 = Obj3 + 10;

As per you have defined function to overload operator the first argument should be of complex type not int type.

Do it like this:-

#include<iostream>
using namespace std;

class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {real = r;   imag = i;}

     Complex operator + (Complex const &obj) {
     Complex res;
     res.real = this->real + obj.real;
     res.imag = this->imag + obj.imag;
     return res;
}

     friend Complex operator + (int i, Complex const &obj) {
     Complex res;
     res.real = obj.real + i;
     res.imag = obj.imag ;
     return res;
}
    Complex operator - (Complex const &obj) {
      Complex res;
     res.real = this->real - obj.real;
     res.imag = this->imag - obj.imag;
     return res;
    }
  friend Complex operator - (int i, Complex const &obj) {
      Complex res;
     res.real = obj.real - i;
     res.imag = obj.imag ;
     return res;
    }
    void print() { cout << real << " + i" << imag << endl; }
};  

int main()
{
    Complex Obj1(10, 5), Obj2(2, 4);
    Complex Obj3 = Obj1 + Obj2; 
    Complex Obj4 = 10 + Obj3; 
    Complex Obj5 = Obj4 + 15;
    cout<<" + operation:"<<endl;
    Obj3.print();
    Obj4.print();
    Obj5.print();
    Complex Obj6 = Obj1 - Obj2; 
    Complex Obj7 = 10 - Obj3; 
    Complex Obj8 = Obj4 - 15;
    cout<<" - operation:"<<endl;
    Obj6.print();
    Obj7.print();
    Obj8.print();
}

If you want to write one function for each(+ and -) then consider using templates.

As I have already told you the first argument should be of complex type, but I think I should explain you why?

Below are two programs try to understand them carefully.

First Program:

#include<iostream>
using namespace std;

class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {real = r;   imag = i;}

     Complex operator +(int i) {
        Complex res;
        res.real = this->real + i;
        res.imag = this->imag;
        return res;
    }
    void printComplex(){
        cout<<"Real="<<this->real<<" Imaginary="<<this->imag;
    }
};  

int main()
{
    Complex Obj1(10, 5);
    Complex Obj2;
    Obj2=Obj1 + 5;
    Obj2.printComplex();
    return 0;
}

Second Program:

#include<iostream>
using namespace std;

class Complex {
private:
    int real, imag;
public:
    Complex(int r = 0, int i =0)  {real = r;   imag = i;}

     Complex addInt(int i) {
        Complex res;
        res.real = this->real + i;
        res.imag = this->imag;
        return res;
    }
    void printComplex(){
        cout<<"Real="<<this->real<<" Imaginary="<<this->imag;
    }
};  

int main()
{
    Complex Obj1(10, 5);
    Complex Obj2;
    Obj2=Obj1.addInt(5);
    Obj2.printComplex();
    return 0;
}

Consider statement Obj2=Obj1 + 5; and Obj2=Obj1.addInt(5); in first program and second program respectively.

So from this I want to say that internally operator overloading works just like calling a function using dot(.) operator.So Obj1.addInt(5) will return an object of type Complex and will be assigned in Obj2. Similarly Obj1 + 5 will also return an object of type Complex.

So if you will not provide first argument of Complex type, like you was doing like this Complex Obj4 = 10 + Obj3; which will expand something like Obj4= 10.AddInt(Obj4);. Now 10 is not any object so how we can call a method(member function) using a dot operator.

But the internal working of friend function is different. Now whenever we are defining a friend function, for the same operation, it is taking two arguments. It means that friend function is not calling anything using dot(.) operator, instead it takes two arguments and return a object of type Complex.

Note:- If both arguments are of Complex type then it works fine because Complex Obj3 = Obj1 + Obj2; will expand to Complex Obj3 = Obj1.AddComplex(Obj2); and Complex Obj3 = Obj2 + Obj1; will expand to Complex Obj3 = Obj2.AddComplex(Obj1);.So in both cases first argument is Complex.

Honey Yadav
  • 176
  • 1
  • 12