Friday, Jan 19th

Last update12:59:40 PM GMT

Operator Overloading Part-II

Write e-mail

Now that we have learnt how Operator Overloading works in C++, its time to move a step ahead and see its application. In the last article we have already seen how to overload + operator which is a binary operator. In this article we will try to learn how to overload unary operators, and then consider few important concepts dealing with operator overloading.

argaiv1077

You will find it surprising to know that even simplest of programs you would have written till now (even a “Hello World” program) involved Operator Overloading. The IO operators (the one that are also used with cout and cin) are overloaded versions of shift operators (<<, >>). For the same class Complex we declared in the last article, if we wish to overload << operator to display the complex class according to our need, we can have an implementation like below:


friend ostream &operator<<(ostream &out, Complex c)
{
 out<<"real part: "<<<"\n";
 out<<"imag part: "<<<"\n";
 return out;
}

Clearly declaring the function as friend is solely to directly access the private member data of class Complex. The function returns the reference to ostream because it is required to use them recursively. Of course the above function lacks error checking. 

Note: Whenever we overload a unary operator, it works with one operand, and hence the object itself becomes the caller and no argument is required. Whereas in case of binary operator overload, we require two operands. So the calling object itself provides the first operand and the supplied object in the argument becomes the second operand. You can relate what we said to the overloaded + operator example we had in last article on Basics of Operator Overloading, the signature of which was:

Complex operator + (Complex c);

Now for the matter of easy understanding we will take example of overloading two unary operators (pre increment and post increment) operators:

Overloading Pre Increment (++x) and Post Increment (x++) operator

class PrePostInc {
 private:
 int count ;
 public:
 PrePostInc() {
 //Default constructor
 count = 0 ;
 }

 PrePostInc(int C) {
 // Constructor with Argument
 count = C ;
 }

 PrePostInc operator ++ () {
 // Operator Function Definition for pre-increment
 return PrePostInc(++count);
 }

 PrePostInc operator ++ (int) {
 // Operator Function Definition for post-increment
 return PrePostInc(count++);
 }

 void display(void) {
 cout << count << endl ;
}
};
 
int main() {
 PrePostInc a, b(10),c,d;

 cout << "Before using the operator ++()\n";
 cout << "a = ";
 a.display();
 cout << "b = ";
 b.display();

 ++a;  //using pre increment operator overload version
 b++;  //using post increment operator overload version

 cout << "After using the operator ++()\n";
 cout << "a = ";
 a.display();  //a.count here is 1
 cout << "b = ";
 b.display(); //b.count here is 11

 c = ++a;
 d = b++;
 cout << "c = ";
 c.display(); //c.count here is 2
 cout << "d = ";
 d.display(); //d.count here is 11

 return 0;
}

As you can see in this case we use int as a dummy argument for post-fix, when we redefine the functions for the unary increment (++) or decrement (--) overloaded operators.
You must remember that int isn't an integer, but just a dummy argument. You can see it as a signal to the compiler to create the post-fix notation of the operator.

Also note its not necessary to have both prefix and postfix versions of increment operator (or decrement) operator overloaded together. However on overloading just one of them, some compilers do flag a warning which can be ignored.

Share this post



Add comment

Please refrain from using slang or abusive language in the comments.
To avoid waiting for your comment to be published after being reviewed, please log in as a registered user.


Security code
Refresh

Web Hosting