0

I want to know how the operator+ member function and operator= member will be written for below statements in main.

I do not want to add friend functions.

int main(){
  A obj1, obj2, obj3;

  obj2 = obj1 + 10;

  obj3 = 20 + obj1;

  return 0;

}

//Below is my class

//Please add necessary assignment and additions operator+ functions

class A{

   int i;

 public :

      A(){i = 0;}

     A& operator=(const A &obj){

        i = obj.i;
        return *this;
    }
};
Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
ghani
  • 19
  • 2
  • 2
    A free function, operator+(int i, const A& a) { return a+i; } solves your issue. – erenon Aug 19 '14 at 11:27
  • You can't have a *member* `operator+` function for the second addition, for that you need a global non-member operator function. – Some programmer dude Aug 19 '14 at 11:29
  • "I do not want to add friend functions." Is there a reason? – Neil Kirk Aug 19 '14 at 11:31
  • @erenon I disagree. It works around the design flaw. – Neil Kirk Aug 19 '14 at 11:35
  • @NeilKirk: I don't see why your answer any way better than mine. It might be expensive to create a temporary `A` instead of applying operator+ directly. – erenon Aug 19 '14 at 11:42
  • @erenon Not for the class definition provided. – Neil Kirk Aug 19 '14 at 11:51
  • @NeilKirk: Since we are speaking about desing, it's obvious the example is just an example. However, I'm really iterested your opinion against the free function. Maybe I'm missing something. – erenon Aug 19 '14 at 12:17
  • if you do not want friendly guys then you just use your assignment operator as addition – oknsnl Aug 19 '14 at 12:19
  • @erenon He should give an example that reflects his use. It takes more effort to code all the combinations of operators to allow int first, what if he wants float too? My approach involves less code and so less effort and less change to introduce bugs. If A is huge, it may become inefficient, true. Correct code first, then fast code. – Neil Kirk Aug 19 '14 at 12:29
  • @erenon Thinking about this more, the operator could be made a template. – Neil Kirk Aug 19 '14 at 13:20

5 Answers5

1

You say you don't want to use a friend function, but tough, that is the correct way. You have no need for a custom assignment operator. The implicit constructor will automatically turn the integer into an instance of A. This will work with your code in main.

class A
{
public :
    A(int i = 0) : i(i) {}

    friend A operator + (const A& left, const A& right)
    {
        return A(left.i + right.i);
    }

private:
    int i;
};
Neil Kirk
  • 20,002
  • 5
  • 48
  • 79
1

obj = obj1 + 10; can be solved with the following defined operator:

A operator+( int rhs ){
  return A( i + rhs );
}

the other way around is a problem, due to int being a non class type. IMO you cannot solve this without friend operators, because member operators imply that the left hand system is a class type, you will need for overloading.

Here is a link to a very good answer to a similar question

Community
  • 1
  • 1
lifeOfPI
  • 308
  • 1
  • 8
0

Slightly different solution:

class A
{
public :
    A(int i = 0) : i(i) {}

    A operator+(int addition)
    {
        return A(i + addition);
    }

private:
    int i;
};

A operator+(int addition, const A& a)
{
    return a + addition;
}
erenon
  • 17,952
  • 2
  • 56
  • 83
0

Operator= will work as you have written in your code snippet. For addition, member function will not work, one alternative is...

//Define following members functions and attributes in class
class A
{
private:
  int i;

public:
  //1) implicit constructor for conversion from int
  A (int i_ = 0) : i (i_) {}

  //2) public function for addition
  A Add (A const& copy)
  { return A (i + copy.i); }
};

//You can call A::Add from global operator+ function without it being friend
A operator+ (A const& left, A const& right)
{ return left.Add (right); }
Aneel
  • 71
  • 4
0
#include<iostream>
using namespace std;

class A{

     int i;

 public :

     A(){i = 0;}
     A& operator=(int obj){

        i = obj;
        return *this;
    }
    int operator+(){
        return this->i;
    }
    int operator-(){
        return -1*this->i;
    }
    A operator+(int a){
        this->i=a+this->i;
        return *this;
    }
};


int main(){
  A obj1, obj2, obj3;

  obj2 = obj1 + 10;

  obj3 = 20 + +obj1;

  return 0;

}

Is that solve your problem?I hope this will help.

oknsnl
  • 351
  • 1
  • 9