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

Operator Overloading

C++ allows operators to be overloaded for user-defined types. Operator overloading allows operators to have special meanings for certain data types. For example, the + operator can be overloaded to concatenate strings. Operators can be overloaded as member or non-member functions, with the correct number of operands as required by each operator. Operator overloading must follow specific syntax and rules regarding return types, precedence, associativity, and accessing private members.

Uploaded by

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

Operator Overloading

C++ allows operators to be overloaded for user-defined types. Operator overloading allows operators to have special meanings for certain data types. For example, the + operator can be overloaded to concatenate strings. Operators can be overloaded as member or non-member functions, with the correct number of operands as required by each operator. Operator overloading must follow specific syntax and rules regarding return types, precedence, associativity, and accessing private members.

Uploaded by

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

Operator overloading

C++ has the ability to provide the operators with a special meaning for a data type, this ability is known
as operator overloading. Operator overloading is a compile-time polymorphism. For example, we can
overload an operator ‘+’ in a class like String so that we can concatenate two strings by just using +.
Other example classes where arithmetic operators may be overloaded are Complex Numbers, Fractional
Numbers, Big integers, etc.

Syntax:

Return_Type classname :: operator op(Argument list)


{
Function Body
}// This can be done by declaring the function

Here,

 Return_Type is the value type to be returned to another object.


 operator op is the function where the operator is a keyword.
 op is the operator to be overloaded.

Operator Overloading can be done by using three approaches, i.e.

1. Overloading unary operator.


2. Overloading binary operator.

Following are the general rules of the operator overloading.

1. Syntax/Declaration Rule: While overloading a function we need to use the keyword


operator , proceeded by class name and followed by operator symbol such as ‘+’.

2. Member and Non-member functions: We can overload operators in two ways:- first is
as a member functions , second is as a non-member functions. When overloading as a
member function, the left operand represents the object on which the function is called
and when overloading as a non-member function, we can specify any type for the left
operand.

3. Number of operands Required: Most operators can be overloaded with one or two
operands. Example- Unary operators like ++ require one operand, while binary operators
like + require two operands.
4. Precedence and associativity: Operator precedence and associativity cannot be changed
when overloading operators. Example- It’s not allowed to change the precedence of (*) or
the associativity of (/).

5. Return type: When we overload operators than we must define the return type of the
overloaded operator function according to our need.

6. Friend function: If overloaded operators needs to access private member


variables/functions of a class than they must be declared as a friend function.

7. Special Syntax: For operators like (), [], or -> we must use a special syntax when
overloading. Example, in order to overload the [] operator and to make objects callable
like functions, we need to define a function named operator().

You might also like