Unit-5 Operator Overloading
Unit-5 Operator Overloading
Definition
C++ provides a special function to change the current functionality of some operators within
its class which is often called as operator overloading. Operator Overloading is the method
by which we can change some specific operators‟ functions to do different tasks.
Syntax:
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.
Arithmetic operations: + – * / %
Logical operations: && and ||
Relational operations: == != >= <=
Pointer operators: & and *
Memory management operator: new, delete []
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.
Example:
int a;
float b,sum;
sum = a + b;
Here, variables “a” and “b” are of types “int” and “float”, which are built-in data types. Hence
the addition operator „+‟ can easily add the contents of “a” and “b”. This is because the
addition operator “+” is predefined to add variables of built-in data type only.
Implementation:
In above example, we have 3 variables “a1”, “a2” and “a3” of type “class A”. Here we are
trying to add two objects “a1” and “a2”, which are of user-defined type i.e. of type “class A”
using the “+” operator. This is not allowed, because the addition operator “+” is predefined
to operate only on built-in data types. But here, “class A” is a user-defined type, so the
compiler generates an error. This is where the concept of “Operator overloading” comes in.
Now, if the user wants to make the operator “+” add two class objects, the user has to
redefine the meaning of the “+” operator such that it adds two class objects. This is done by
using the concept of “Operator overloading”. So the main idea behind “Operator
overloading” is to use C++ operators with class variables or class objects. Redefining the
meaning of operators really does not change their original meaning; instead, they have been
given additional meaning along with their existing ones.
Public:
Complex (int r = 0, int i = 0)
{
real = r;
imag = i;
}
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
}
consider overloading (-) unary operator. In the unary operator function, no arguments
should be passed. It works only with one class object. It is the overloading of an operator
operating on a single operand.
Key point
No Arguments: Unary operator functions do not take any arguments.
Member Function: Unary operators are typically implemented as member functions.
Return Type: The return type can be void or any other type, depending on the
operation.
Example: Assume that class Distance takes two member objects i.e. feet and inches, and
creates a function by which the Distance object should decrement the value of feet and
inches by 1 (having a single operand of Distance Type).
#include <iostream>
using namespace std;
class Distance
{
public:
int feet, inch; // Constructor to initialize the object's value
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
Binary operator overloading in C++ involves writing a function that redefines a binary operator
for user-defined types. This process allows objects of a class to interact using traditional operators like
+, -, *, etc. To overload a binary operator, you must specify the operator keyword followed by the
operator you intend to overload.
In the binary operator overloading function, there should be one argument to be passed. It is
the overloading of an operator operating on two operands. Below is the C++ program to show the
overloading of the binary operator (+) using a class Distance with two distant objects.
1. Overloaded operators must have at least one operand that is a user-defined type.
2. Overloaded operators cannot change the precedence and associativity of operators.
3. Certain operators cannot be overloaded. (dot), .* (pointer to member), :: (scope
resolution), ?: (ternary conditional), and sizeof.
4. The arity of the Operator cannot be changed; for instance, you cannot make + unary if
it‟s binary.
5. Overloaded operators maintain their original meaning for built-in types.
6. Some operators are not suitable for overloading. For example, &&, ||, ,, . (member
selection), .* (member selection through a pointer), and:: (scope resolution).
7. The overloaded operators can be member functions or global functions.
8. Unary operators have one operand, binary operators have two, and ternary operators
have three.
1. Conditional [?:], size of, scope(::), Member selector(.), member pointer selector(.*) and
the casting operators.
2. We can only overload the operators that exist and cannot create new operators or
rename existing operators.
3. At least one of the operands in overloaded operators must be user-defined, which
means we cannot overload the minus operator to work with one integer and one
double. However, you could overload the minus operator to work with an integer and a
mystring.
4. It is not possible to change the number of operands of an operator supports.
5. All operators keep their default precedence and associations (what they use for), which
cannot be changed.
6. Only built-in operators can be overloaded.
1. Potential Misuse: With great power comes great responsibility. Overloading operators
can lead to abuse or misuse, resulting in difficult-to-understand or maintain code.
2. Complexity: Overuse or misuse of operator overloading can introduce complexity,
especially when overloaded operators don‟t behave as expected or when there‟s
ambiguity in their usage.
3. Limited Overloading: While many C++ operations can be overloaded, there are some
exceptions, such as the member access operators (. and ->) and the scope resolution
operator (::). This limitation can sometimes restrict the flexibility of operator overloading
Example to Understand Operator Overloading in C++ Using Friend Function
#include <iostream>
using namespace std;
class Complex
{
private:
int real;
int img;
public:
Complex (int r = 0, int i = 0)
{
real = r;
img = i;
}
void Display ()
{
cout << real << "+i" << img;
}
friend Complex operator + (Complex c1, Complex c2);
};
Complex operator + (Complex c1, Complex c2)
{
Complex temp;
temp.real = c1.real + c2.real;
temp.img = c1.img + c2.img;
return temp;
}
int main ()
{
Complex C1(5, 3), C2(10, 5), C3;
C1.Display();
cout << " + ";
C2.Display();
cout << " = ";
C3 = C1 + C2;
C3.Display();
}
EXERCISE
2. Demonstrate by using binary (+) operator to perform the addition of two numbers.
3. Write and implement the code for following output.