0% found this document useful (0 votes)
38 views

Operators Overloading in C++

Operator overloading in C++ allows redefining operators for user-defined types. Only built-in operators can be overloaded, and their basic meanings cannot be changed. Overloaded operators are defined as either member or friend functions, with certain operators like assignment only allowed to be member functions. Unary operators take no or one argument as member or friend functions, and binary operators take one or two arguments.

Uploaded by

Pratap Adimulam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Operators Overloading in C++

Operator overloading in C++ allows redefining operators for user-defined types. Only built-in operators can be overloaded, and their basic meanings cannot be changed. Overloaded operators are defined as either member or friend functions, with certain operators like assignment only allowed to be member functions. Unary operators take no or one argument as member or friend functions, and binary operators take one or two arguments.

Uploaded by

Pratap Adimulam
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Rules of Operator Overloading in C++

Only built-in operators can be overloaded. New operators cannot be


created.
We cannot change the basic meaning of an operator.
An Operator function must be either member function or friend
function.
We cannot use friend functions to overload certain operators.
However, member function can be used to overload them.
Friend Functions cannot be used with assignment operator (=),
function call operator(()), subscripting operator([]), class member
access operator(->) etc.

Assignment (=), subscript ([]), function call (()), and member selection (>) operators must be defined as member functions
Unary operators declared as member functions take no arguments; if
declared as global functions, they take one argument.
Binary operators declared as member functions take one argument; if
declared as global functions, they take two arguments.
If an operator can be used as either a unary or a binary operator ( &,
*, +, and -), you can overload each use separately.
Overloaded operators cannot have default arguments.

Operators overloading in C++:


You can redefine or overload most of the built-in operators available in C+
+. Thus a programmer can use operators with user-defined types as well.

Overloaded operators are functions with special names the keyword


operator followed by the symbol for the operator being defined. Like any
other function, an overloaded operator has a return type and a parameter
list.
Unary operators, overloaded by means of a member function, take no
explicit arguments and return no explicit values, but, those overloaded
by means of a friend function, take one reference argument (the object
of the relevant class).
Binary operators overloaded through a member function take one
explicit argument and those which are overloaded through a friend
function take two explicit arguments.
When using binary operators overloaded through a member function,
the left hand operand must be an object of the relevant class.
Binary arithmetic operators such as +,-,* and / must explicitly return
a value. They must not attempt to change their own arguments.
Overloadable/Non-overloadableOperators:
Following is the list of operators which can be overloaded:
+
&
<
<<
+=
|=
->

|
>
>>
-=
*=
->*

*
~
<=
==
/=
<<=
new

/
!
>=
!=
%=
>>=
new []

%
,
++
&&
^=
[]
delete

^
=
-||
&=
()
delete []

There are some operators that cannot be overloaded like size of


operator (sizeof), membership operator(.), pointer to member
operator(.*), scope resolution operator(::), conditional operators(?:) etc
Following is the list of operators, which can not be overloaded:

::

You might also like