3

My textbook says we can add two objects of same class. V3=V2+V1 // All are of same class.

But when I test that in Turbo c++ I get error: illegal structure operation pointing to the same line, V3=V1+V2.

So my question is whether it is possible to add two objects of same class using + operator , if answer is yes then why am I getting an error message?

Nithin Jose
  • 1,029
  • 4
  • 16
  • 31

4 Answers4

6

Your class must have overloaded the + operator. Without it, the compiler would not know how to "add" the two classes given. Define how the + operator should work by adding a operator overloading function.

The following is an example for a class 'V':

V V::operator+(const V&  other){
    //Define how should the classes be added here

    //Example addition of private fields within V
    int field1 = this.field1 + other.field1;

    //Return the 'added' object as a new instance of class V
    return V(field1);
}

A more complete reference on operator overloading can be viewed here.

initramfs
  • 8,275
  • 2
  • 36
  • 58
1

yes of course you can add two object of same class but before doing that you have to do operator overloading , by defining the '+' operator and how the objects are going to add when u simply put a '+' operator between the object. u can not only add, u can implement any operator like '-' ,'*','/' but first u have to overload them.

here is an example of operator overloading

class Cents
{
 private:
 int m_nCents;

 public:
   Cents(int nCents) { m_nCents = nCents; }

// Add Cents + Cents
   friend Cents operator+(const Cents &c1, const Cents &c2);

   int GetCents() { return m_nCents; }
};

// note: this function is not a member function!
Cents operator+(const Cents &c1, const Cents &c2)
{
 // use the Cents constructor and operator+(int, int)
   return Cents(c1.m_nCents + c2.m_nCents);
}

int main()
{
  Cents cCents1(6);
  Cents cCents2(8);
  Cents cCentsSum = cCents1 + cCents2;
  std::cout << "I have " << cCentsSum .GetCents() << " cents." << std::endl;

  return 0;
}
vipul
  • 118
  • 1
  • 1
  • 6
0

You can not only add two user defined data types but also perform various operations by using operator overloading.

The general syntax for operator overloading is as follows:

If you want to add two objects such as:

eg: objres=obj1+obj2;//belong to class s

The operation obj1+obj2 should return an object of the same user defined data type.

So, the return type of your overloaded function should be s.

s operator+(s obj)
{
s temp;
temp.datamember=datamember+obj.datamember;//perform operation on datamembers
return temp;//return object to objres
}

This will return an object with specific operation performed on it.

It should be noted that operator is a keyword in C++ and it's general ethics to not to change the meaning if the operator being overloaded.Also if an operator is unary it remains unary and if an operator is binary it remains binary and requires one and two operands respectively.

Similarly, operators like *,>,<,==,/,++,-- can be overloaded as well.

vvv
  • 53
  • 1
  • 7
0
#include<iostream>
using namespace std;
/*class count
{private:
    int n;
    
        public:
        count()
        {
        n=0;        }
        void show()
        {cout<<n<<endl;
        }
        void operator ++()
        {count temp;
        n=n+1;
        temp.n=n;
        return temp;
        }
        void operator ++(int)
        {count temp;
        n=n+1;
        temp.n=n;
        return temp;
        }
        
};
int main()
{
count a,b;

    a.show(); 
  
++a;
a++;
    a.show();

    
    
}*/
class add
{
    private  :
        int a,b;
        public:
            void get()
            {cout<<"enter a";
                cin>>a;
            //  cout<<"enter b";
            //  cin>>b;
            }
            void show()
            {
            cout<<"sum of a"<<a<<endl;  
            }
            add operator +(add x)
            {
            //  add y;
                y.a=x.a+a;
            //  return y;
                
            }
        
};
int main()
{
    add obj1,obj2,obj3;
    obj1.get();
    obj2.get();
    obj3=obj1+obj2;
    obj3.show();
}
  • 1
    While this may answer the question, it's better to provide a brief explanation of the solution you provided, so it may benefit the OP and future readers. – Makdous Jun 27 '20 at 17:05