I am new in C++ programming , I want to return object which was passed by default in operator overloading. Please help me on that.....
eg.
adi operator+(adi& s2){return s1;}
main()
{
s3=s1+s2;
}
I am new in C++ programming , I want to return object which was passed by default in operator overloading. Please help me on that.....
eg.
adi operator+(adi& s2){return s1;}
main()
{
s3=s1+s2;
}
You either implement operator+ as a class member
class A {
A operator+(const A& rhs) const {
A result; // do something with a
// Pick one
return result; // returns new object
return rhs; // returns rhs parameter
return *this; // returns current object
}
};
Or as a non-member
A operator+(const A& a, const A& b) {
A result; // do something with a
return result; // returns new object
}
It will look the following way
adi operator+( const adi &s2) const
{
adi temp;
/* some calculations with temp*/
return temp;
}
The symantic is the following: you create a new object that will be the sum of the object that calls the operator that is this
ans the object passed to the operator as an argument. Neither the original object nor the arument should be changed.
Also the operator can be define as a non-member function. For example
adi operator+( const adi &s1, const adi &s2)
{
adi temp;
/* some calculations with temp*/
return temp;
}