- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming
The Concept of Operator Overloading in C++
C++ programming language focuses on object-oriented programming that has several advantages over the conventional form. The freedom to create user-defined data type has extended the functionality of coding and has simplified long and complicated programs. Objects may have several attributes relevant to the class to which they belong and they are dealt with member or friend functions as the case may be.
Operator overloading concept gives a special meaning to an operator such as + or -. Addition or subtraction cannot be directly performed with objects as we do with basic data types. Like overloading functions, operators also can be overloaded for doing this task. This is achieved with the help of member functions and friend functions. The general form of an overloaded operator function is
return type operator op(argument list);
where op stands for the operator sign.
Operator overloading by means of member functions
While overloading operators using member functions, only objects belonging to that class can access it. So the object used to invoke it would be the first operand and the objects or variables specified in its argument list would be the remaining operands.
A unary operator overloaded by means of a member function will have no arguments and a binary operator overloaded function will have only one argument.
For example, the unary minus operator overloaded function should be
return type operator-()
and a binary operator overloaded function should be
return type operator+(argument 1);
An example for operator overloading by means of member function can be read here.
int operator-() add A; //add is the class to which object A is declared -A; return(A);
Operator overloading by means of friend functions
There can be situations when all the operands need not be objects (user-defined data types) of a class. When the left-hand operand of a binary operator is not of user-defined data type, using a member function will not work. Such cases are handled by using friend functions that take both the operands as parameters.
Friend functions are treated as normal functions that have no access to the private members of a class. They can be called like a normal function and operators can be overloaded through them. All the operands are passed as arguments explicitly here. The general form of an operator overloaded friend function is
friend return type operator op(argument list);
where op stands for the operator sign.
For example, a unary operator overloaded friend function should be
friend return type operator-(argument) and
a binary operator overloaded friend function should be
friend return type operator+(argument1, argument2);
Even if friend functions can’t overload operators such as assignment operator (=), sub-scripting operator ([]), function call operator (()), and member access operator (->) they come handy in some situations where member functions won’t help. In scalar multiplication of a vector, an integer (built-in data type) and an object (user defined data type) are the operands. When the object becomes the left-hand operand, operator overloading through member function would work. But when the integer becomes the first operand, this won’t work as an object of the relevant class is required as the left-hand operand in order to invoke the member function. Friend functions can solve this problem by taking both the operands as the arguments.
Some rules for overloading operators
- Only existing operators can be overloaded. We cannot create new operators for overloading.
- We cannot change the basic meaning of an operator. We cannot redefine ‘+’ operator to subtract one value from the other.
- Overloaded operators follow the syntax rules of the original operator. They cannot be overridden.
- There are some operators that cannot be overloaded.
- Binary operators such as +, -, * and /must explicitly return a value.
Operators that cannot be overloaded
Name
| syntax
|
---|---|
sizeof
| sizeof()
|
membership
| .*
|
conditional
| ?:
|
scope resolution
| ::
|
Please vote!
Does your program get simplified through operator overloading?
- C++ program code for polynomial addition using binar...
Even if the logic involved for polynomial addition in C++ is same as that of C, the main advantage with C++ is that both the polynomials can be added directly with the concept of operator overloading. - About the object-oriented programming language C++
If the volume of data involved in a problem is more, it becomes difficult to program using conventional programming. In such case, data is organized to form a class (like structure in C) of user-defined type and programming is performed on that data - How to learn programming; 5 best resources for a beg...
Learning programming is accompanied by the resources books, programming languages, source codes, thinking power and desire. These resources have been discussed in detail in this hub.
© 2013 Radhika Sreekanth