Operator overloading

Assignment operator overloading:

Syntax:

StringBuilder & operator=(const StringBuilder &);

Implementation:

StringBuilder & StringBuilder::operator=(const StringBuilder & s) { if(this == &s)

//The operator function should protect against assigning an object to itself

return *this;

delete [] str;

 

//Since the target object may already refer to previously allocated data, the overload operator should use delete [] to free the memory.

int len = std::strlen(s.str);

str = new char[len + 1];

std::strcpy(str,s.str); // deep copy

return *this; //The operator function returns a reference to the invoking object. 

}

 2. Overload addition operator:

 const MyComplex MyComplex::operator+(const MyComplex& c) { MyComplex temp;

temp.real = this->real + c.real;

temp.imag = this->imag + c.imag;

return temp;

Note that this doesn’t return Complex &, but instead returns a const Complex class variable. As you can see, the implementation is a little bit different from the previous example. Here, we’re not returning *this. Instead, we’re creating a temporary variable and assigning the results of the addition to temp. This explains why the function returns const Complex and not Complex &.

In other words, the function creates a new MyComplex object temp that represents the sum of the other two MyComplex objects. Returning the object creates a copy of the object that the calling function can use. If the return type were MyComplex &, however, the reference would be the temp object. But the temp object is a local variable and is destroyed when the function terminates, so the reference would be a reference to a nonexisting object. Using a MyComplex return type, however, means the program constructs a copy of MyComplex object before destroying it, and the calling function gets the copy.

The problem with return type as reference:

MyComplex & MyComplex::operator+(const MyComplex& c) {

real += c.real;

imag += c.imag;

return *this;

}

c5 = c3 + c4; // it changes values of both after the addition operator (c3)and the equal operator(c5).

c3 = opetator+(c1,c2); // nonmember function

c3 = c1.operator+(c2); // member function

Output of the code above is:

c3= 10+100i

c4= 20+200i
adding c3 and c4
c3= 30+300i
c4= 20+200i
c5= 30+300i

We got the right result at least for c5. But the value of c3 has been changed.

//pre-increment operator overloading

MyComplex & MyComplex::operator++() {

real++; imag++;

return *this;

}

//post-increment operator overloading

MyComplex MyComplex::operator++(int) {

MyComplex temp = *this;

real++; imag++;

return temp; }

There is a problem with defining both the prefix and postfix opetators: They each take the same number and type of parameters. Normal overloading cannot distinguish between whether the operator we’re defining is the prefix version or the postfix.

To solve this problem, the postfix operator function take an extra parameter of the int. When we use the postfix operator, the compiler supplies 0 as the argument for this parameter. Although our postfix function could use this extra parameter, it usually should not. That parameter is not needed for the work normally performed by a postfix operator. Its sole purpose is to distinguish the definition of the postfix function from the prefix version.

Note that the post-increment(++) is returning an old value, and requires local temp variable. This is why we prefer prefix in STL iterators.
The increment(++) and decrement(–) operators are most often implemented for class, such as iterators, that provide pointers like behavior on the elements of a sequence.

Operators that cannot be overloaded

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.