OPERATOR OVERLOADING
OPERATOR OVERLOADING
INTRODUCTION:
C++, Operator overloading is a compile-time polymorphism. It is an idea of giving special meaning to an existing operator in C++ without changing its original meaning.
In this article, we will further discuss about operator overloading in C++ with examples and see which operators we can or cannot overload in C++.
C++ 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.
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:
// C++ Program to Demonstrate the
// working/Logic behind Operator
// Overloading
class A {
statements;
};
int main()
{
A a1, a2, a3;
a3 = a1 + a2;
return 0;
}
In this 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 existingines
DIFFERENCE BETWEEN OPERATOR:
Functions and Normal Functions
Operator functions are the same as normal functions. The only differences are, that the name of an operator function is always the operator keyword followed by the symbol of the operator, and operator functions are called when the corresponding operator is used.
CAN WE OVERLOAD ALL OPERATORS?
Almost all operators can be overloaded except a few. Following is the list of operators that cannot be overloaded.
sizeof
typeid
Scope resolution (::)
Class member access
operators (.(dot),
.* (pointer to member operator))
Ternary or conditional (?:)
Operators that can be Overloaded in C++
We can overload
Unary operators
Binary operators
Special operators ( [ ], (), etc)
But, among them, there are some operators that cannot be overloaded. They are
Scope resolution operator (::)
Member selection operator
Member selection through *
Pointer to a member variable
Conditional operator (?:)
Sizeof operator sizeof()
Operators that can be overloaded Examples
Binary Arithmetic (+, -, *, /, %)
Unary Arithmetic (+, -, ++, —)
Assignment( =, +=,*=, /=,-=, %=)
Bitwise( & , | , << , >> , ~ , ^)
De-referencing (->)
Dynamic memory allocation,De-allocation( New, delete )
Subscript [ ]
Function call ()
Logical &, | |, !
Relational( >
Overloading unary operator:
Let us 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.
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).
Ex program:
// C++ program to show unary
// operator overloading
#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;
}
// Overloading(-) operator to
// perform decrement operation
// of Distance object
void operator-()
{
feet--;
inch--;
cout << "\nFeet & Inches(Decrement): " <<
feet << "'" << inch;
}
};
// Driver Code
int main()
{
Distance d1(8, 9);
// Use (-) unary operator by
// single operand
-d1;
return 0;
}
Output:
Feet & Inches(Decrement): 7'8
Explanation: In the above program
Overloading Binary operator:
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.
Ex program:
// C++ program to show binary
// operator overloading
#include <iostream>
using namespace std;
class Distance {
public:
int feet, inch;
Distance()
{
this->feet = 0;
this->inch = 0;
}
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
// Overloading (+) operator to
// perform addition of two distance
// object
// Call by reference
Distance operator+(Distance& d2)
{
// Create an object to return
Distance d3;
d3.feet = this->feet + d2.feet;
d3.inch = this->inch + d2.inch;
// Return the resulting object
return d3;
}
};
// Driver Code
int main()
{
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;
// Use overloaded operator
d3 = d1 + d2;
cout << "\nTotal Feet & Inches: " <<
d3.feet << "'" << d3.inch;
return 0;
}
Output:
Total Feet & Inches: 18'11
PROGRAM:
// C++ Program to Demonstrate
// Operator Overloading
#include <iostream>
using namespace std;
class Complex {
private:
int real, imag;
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
// This is automatically called when '+' is used with
// between two Complex objects
Complex operator+(Complex const& obj)
{
Complex res;
res.real = real + obj.real;
res.imag = imag + obj.imag;
return res;
}
void print() { cout << real << " + i" << imag << '\n'; }
};
int main()
{
Complex c1(10, 5), c2(2, 4);
Complex c3 = c1 + c2;
c3.print();
OUTPUT:
12 + i9
CREATED BY:
C.DHARANIDHARAN ( 24USC005)
S. MADHAVAN (24USC036)
1ST B. SC COMPUTER SCIENCE.
SRI RAMAKRISHNA MISSION VIDYALAYA COLLEGE OF ARTS AND SCIENCE ,( UN ADIED) ,COIMBATORE ,TAMIL NADU.
Super 👌
ReplyDeleteSuper guys
ReplyDeleteSuper
ReplyDeleteSuper Madhav👌🏼
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteSuper Dharani👌❤️🩹
ReplyDelete