Tutorial: Overloading Unary Operator in C Plus Plus

Harsh Gupta - Tech Writer
C plus plus is an object oriented programming language. Object oriented programming language include the features of objects, classes, encapsulation, abstraction, polymorphism, inheritance. Polymorphism is the combination of two words: poly (which is a Greek word) means "many" + "morph" means form. So polymorphism means one thing which has many forms. Polymorphism includes:

Operator overloading

Operator overriding

Function overloading

Here I m discussing operator overloading:

Usually an operator can be operated on built in data types like int, float. But if you want to operate the operators available in C++ on user defined data type like object, then it can be done by operator overloading. By operator overloading we can make an operator to operate on user defined variables. Operator overloading includes following features:

Only existing operator can be overloaded

It will not change the basic meaning of operator

Overloaded operator will follow the same syntax rule of the original operator

There are some operators that can't be overloaded

Unary operators, overloaded b y means of a member function, take no explicit arguments and return no explicit argument. The syntax for member function for overloading unary operator is:

operator op ()

{

Statement........

}

Here return type is the data type of the value we want to return from this member function. Operator is the keyword and op is the operator to be overloaded (like in this case it will be ++) and like other member functions we can have parameter list also. ++ Operator will simply increment the value of the variable. It can be either prefix or postfix. This can be called through variables like: "a++" or "++a". for a object obj1 of class length we can use this operator like:

Obj1++;

And the member function for it will be like:

Void operator ++ (........)

{......... ............ ................ }

We don't have to pass the variables explicitly it will be automatically passes implicitly. The object will be passed implicitly and we will simply increment all the variables under object "obj1" and the control will be returned back to the main function. So by this you can easily use increment operator on user defined object. Hence you can easily implement operator overloading on unary operator.

Problems on operator overloading of unary "++" operator:

This overloading can be done on pre increment operator but, it will generate a warning when we implement it on post increment operator. But it will perform the pre and post increment operator. We can't distinguish between pre and post increment operator as it will do the same. We can't make it differ to perform pre and post increment.

Published by Harsh Gupta - Tech Writer

I am a part time freelancer and writing is my hobby Some of my websites: http://www.GenericArticles.com http://www.JailBreakingiPhone.com  View profile

  • By operator overloading we can make an operator to operate on user defined variables.
Usually an operator can be operated on built in data types like int, float. But if you want to operate the operators available in C++ on user defined data type like object, then it can be done by operator overloading.

To comment, please sign in to your Yahoo! account, or sign up for a new account.