-2

can i add three objects using operator overloading in c++??

#include <iostream>
#include <conio.h>
using namespace std;

class complex{
private:
    int a;
public:
    void setdata(int x){
        a=x;
    }
    void showdata(){
        cout<<"\n"<<a;
    }
    int operator +(complex c,complex d){
        complex temp;
        temp.a=a+c.a+c+d.a;
        return (temp);
    }
};

int main(){
complex c1,c2,c3,c4;
c1.setdata(3);
c2.setdata(5);
c4.setdata(2);
c4=c1+c2+c3;
c4.showdata();
}

i am using this approach but it is not working please help.

Usama Ali
  • 47
  • 1
  • 7
  • 3
    Yes you can. You will need to explain what's not working though. – Tas Oct 24 '17 at 05:52
  • Your operator has the wrong return type. – George Oct 24 '17 at 05:56
  • 1
    Your `complex::operator+()` returns `int`. That's why `c1+c2` is complex x complex -> int and hence `(c1+c2)+c3` is int x complex -> ???. Either you change the return of `complex::operator+()` to `complex` or you need a second `operator+(int, complex)`. Both options should work. – Scheff's Cat Oct 24 '17 at 05:57
  • 1
    I'm not quite sure about the syntax. AFAIK, you may define `operator+` as _member_ but in this case `this` is the first argument and you have to define only the 2nd argument as parameter. Or you define `operator+` as _non-member_ with the signature like in your sample. In the latter case, you may make it a `friend` of `complex` to allow its access to `private` member variables. – Scheff's Cat Oct 24 '17 at 06:00
  • see https://stackoverflow.com/q/46843462/3754223 and my answer to it. Seems like you guys are in the same course. :D – MABVT Oct 24 '17 at 06:13

2 Answers2

1

you have to change a little the operator and you have a mistake in the inizialization of the variables (C3 is not initialized). You have not to define an operator that works on three terms at the same time. The sum will be split in two parts (c1 + c2) + c3; the first sum returns a 'complex' item that is added to c3. The result of this last sum is assigned to c4. See below the code.

#include <iostream>
#include <conio.h>
using namespace std;

class complex{
private:
    int a;
public:
    void setdata(int x){
        a = x;
    }
    void showdata(){
        cout << "\n" << a;
    }
    complex operator +(complex c){
        complex temp;
        int i = 0;
        i = a + c.a;
        temp.setdata(i);
        return temp;
    }
};

int main(){
    complex c1, c2, c3, c4;
    c1.setdata(3);
    c2.setdata(5);
    c3.setdata(2);
    c4 = c1 + c2 + c3;
    c4.showdata();
}
tost
  • 84
  • 2
  • thank u so much bro it is my first post on stack over flow :-) – Usama Ali Oct 24 '17 at 12:29
  • `operator+` should take `c` by const reference to avoid a copy. And it should be declared `const` itself since it does not modify `*this`: `complex operator+(const complex &c) const` – Remy Lebeau Oct 24 '17 at 15:20
0

In my comments, I suggested two alternative solutions but while fiddling around with the sample code I even found a third one.

The sample code:

#include <iostream>

// Version 1: (return type fixed)

class Complex1 {
  friend std::ostream& operator << (std::ostream &out, const Complex1 &c);
  private:
    int a;
  public:
    explicit Complex1(int a): a(a) { }
    // operator + as member
    Complex1 operator + (const Complex1 &c) const
    {
      return Complex1(a + c.a);
    }
};

std::ostream& operator << (std::ostream &out, const Complex1 &c)
{
  return out << c.a;
}

// Version 2: (two overloaded operators)

class Complex2 {
  friend std::ostream& operator << (std::ostream &out, const Complex2 &c);
  friend int operator+(const Complex2 &c, const Complex2 &d);
  friend int operator+(int c, const Complex2 &d);
  private:
    int a;
  public:
    explicit Complex2(int a): a(a) { }

};

std::ostream& operator << (std::ostream &out, const Complex2 &c)
{
  return out << c.a;
}

int operator+(const Complex2 &c, const Complex2 &d)
{
  return c.a + d.a;
}

int operator+(int c, const Complex2 &d)
{
  return c + d.a;
}

// Version 3: (implicit conversion with constructor)

class Complex3 {
  friend std::ostream& operator << (std::ostream &out, const Complex3 &c);
  private:
    int a;
  public:
    Complex3(int a): a(a) { }
    // operator + as member
    int operator+(const Complex3 &c) const
    {
      return a + c.a;
    }
};

std::ostream& operator << (std::ostream &out, const Complex3 &c)
{
  return out << c.a;
}

// Check everything out:

using namespace std;

int main()
{
  cout << "Version 1:" << endl;
  { Complex1 c1(3), c2(5), c3(2);
    Complex1 c4 = c1 + c2 + c3;
    cout << "c4: " << c4 << endl;
  }
  cout << "Version 2:" << endl;
  { Complex2 c1(3), c2(5), c3(2);
    Complex2 c4 = Complex2(c1 + c2 + c3);
    cout << "c4: " << c4 << endl;
  }
  cout << "Version 3:" << endl;
  { Complex1 c1(3), c2(5), c3(2);
    Complex1 c4 = c1 + c2 + c3;
    cout << "c4: " << c4 << endl;
  }
  cout << "done." << endl;
  return 0;
}

You may compile and run the code on ideone.


Complex1 provides a fixed operator as member

Complex1 Complex1::operator + (const Complex1 &c) const;

Hence, (c1 + c2) + c3 is

(Complex1 × Complex1 → Complex1) &times Complex1 → Complex1


Complex2 provides two overloaded operators as non-members

  int operator+(const Complex2 &c, const Complex2 &d);
  int operator+(int c, const Complex2 &d);

Hence, c1 + c2 is

Complex2 × Complex2 → int

and (c1 + c2) + c3 is

int × Complex2 → int


Complex3 is very similar like the original sample with the essential difference that I provided a non-explicit constructor which accepts an int. This means the compiler will use it as conversion operator when necessary.

(With a proper constructor, the operator issue probably hadn't been noticed soon.)

Thus, c1 + c2 is

Complex3 × Complex3 → int

and (c1 + c2) + c3 is

(int → Complex3) × Complex3 → int

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56