The document discusses operator overloading in C++, outlining how to associate user-defined types with operators to enhance usability. It explains the difference between member and global functions for overloading unary and binary operators, along with specific rules and limitations for various operators. Additionally, it highlights that certain operators cannot be overloaded and provides the syntax for overloading the stream insertion and extraction operators.
Most fundamentaldata types have pre-defined
operations associated with them.
To make a user-defined data type as natural as a
fundamental data type, the appropriate set of operators
must be associated with it.
C++ allows to define additional meaning to an existing
operator without changing its basic meaning.
It is known as operator overloading.
Programming with Sikander 2
3.
To addtwo objects of the same class , a member function needs
to be invoked.
- obj3 = obj1.add(obj2)
User have to remember functions names for all operations
Understanding of operations will be more easier when we use
operators. Ie
Obj3 = obj1 + obj2;
The user can understand the operation more easily as compared to
the function call because it is closer to a real-life implementation.
Thus by associating a set of meaningful operators, manipulation of
an ADT can be done in a conventional and simple form. Associating
operators with ADTs involves overloading the operators.
Programming with Sikander 3
4.
Only anexpression containing a user defined type can have
overloaded operator.
Only the predefined set of C++ operators can be overloaded, no
new operator can be introduced.
By overloading, it is not possible to change the number of
arguments the operator take.
By overloading, it is not possible to change the precedence of the
operators.
Programming with Sikander 4
5.
An operatorcan be overloaded by defining an operator function for it.
Syntax of operator function
keyword symbol of the operator
Return_type operator # (function arguments)
{
//definition of the function
}
In most cases, the operator function can be either a member function
or a friend function
However in a few cases, the operator function can be only a member
or only a friend as mandated
In operator overloading, a single interface is again used for multiple
implementation and the binding is taking place during compile time.
So operator overloading is considered as compile time polymorphism.
Programming with Sikander 5
6.
When abinary operator is overloaded as a member function, LHS
object acts as the invoking object and RHS object acts as the
argument. ie) the operator member function will take only a single
argument.
When a binary operator is overloaded as global function, since
global function need not be invoked using any object, both the LHS
and RHS object will acts as arguments to the function. ie) the
operator global function will take two arguments.
Programming with Sikander 6
7.
Operator function Unaryoperator Binary operator
As member function No Arguments One Argument
As global function One Argument Two Arguments
Programming with Sikander 7
A unaryoperator is one which takes only a single operand.
While overloading a unary operator as a member function, the
single operand will take up the responsibility of invoking the
function.
i.e., the operator member function will take no arguments.
While overloading a unary operator as a global function, since the
function need not be invoked by an object, the single operand will
now act as an argument to this function.
i.e., the operator global function will take one argument.
Programming with Sikander 12
When youtry to write the operator function of prefix and postfix operators
(++ / --), you can see two functions with the same name which is not
differing in terms of arguments. This violates the function overloading
principles.
In-order to solve this problem, the operator function for postfix operations
should have a integer dummy argument.
While overloading postfix operator as global function, the dummy
argument should always be the second argument.
The compiler invokes the operator function without dummy argument for
the prefix application of the operator.
The compiler invokes the operator function with int argument for the
postfix application of the operator.
Programming with Sikander 16
Operators whichcan be overloaded only as member functions are
=
[ ]
( )
->
Programming with Sikander 18
19.
The <<(Insertion / put to) operator and >>(extraction / get from)
operator is already overloaded to work with fundamental data
types.
Put to operator / insertion operator (<< ) always works with “cout”
which is a predefined object of class “ostream” and get from /
extraction operator (>>) always works with the “cin” which is a
predefined object of class “istream”.
Since cin and cout are objects of istream and ostream respectively,
it cannot be used to invoke member functions of user’s class. i.e.,
overloading of these two operators are not possible with member
function.
The only other possibility here is writing the operator function as a
global / friend function.
Programming with Sikander 19
20.
Syntax foroverloading << and >>
The arguments of ostream and istream should be taken by reference and
returned by reference. This is because, the copy constructor of istream
and ostream class is present in the non public section of the class.
Programming with Sikander 20