Lab7March manual OPerator Overloading
Lab7March manual OPerator Overloading
These operations can include arithmetic operations like addition and subtraction, comparison
operations like equal to and not equal to, logical operations like AND and OR, and more. In essence,
operators enable you to manipulate data and control the flow of your code.
• Function overloading.
• Operator overloading.
What is function overloading?
The process of having two or more functions with the same name but with different parameters
(arguments) is called function overloading. The function is redefined by either using different
types of arguments or a different number of arguments. It is only through these differences that
a compiler can differentiate between functions
Operator overloading in C++ program can add special features to the functionality and behavior
of already existing operators, such as athematics and other operations.
The mechanism of giving special meaning to an operator is known as operator overloading. For
example, we can overload an operator ‘+’ in a class-like string to concatenate two strings by just
using +.
Syntax:
ReturnType operator<operator>(Parameters);
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
// C++ Program to Demonstrate the
// 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 existing ones.
#include <iostream>
using namespace std;
// Define class A
class A {
private:
int value; // Example data member
public:
// Constructor to initialize value
A(int val = 0) : value(val) {}
int main()
{
A a1(10), a2(20), a3; // Create three objects with initial values
return 0;
}
1. Simplified Syntax: Operator overloading allows programmers to use notation closer to the
target domain, making code more intuitive and expressive.
2. Consistency: It provides similar support to built-in types for user-defined types, enhancing
consistency and ease of use.