OOPS DA
OOPS DA
ORIENTED PROGRAMMING
DIGITAL ASSIGNMENT -1
OPERATOR OVERLOADING
NAME – KHUSHI SRIVASTAVA
REG NO. - 24BKT0040
OPERATOR OVERLOADING IN C++
Introduction
Operator overloading is a powerful feature in C++ that allows developers to redefine the behavior of
operators for user-defined data types. By overloading operators, objects of custom classes can be
manipulated using intuitive syntax, similar to built-in data types. This enhances code readability and
maintainability.
1. Arithmetic Operators
• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
• % (Modulus)
These operators compare two values and return true or false. Overloading them allows
object comparisons.
• == (Equal to)
• != (Not equal to)
• < (Less than)
• > (Greater than)
• <= (Less than or equal to)
• >= (Greater than or equal to)
3. Assignment Operators
These operators assign values to variables. Overloading them allows custom behavior when
objects are assigned.
• = (Assignment)
• += (Add and assign)
• -= (Subtract and assign)
• *= (Multiply and assign)
• /= (Divide and assign)
4. Unary Operators
Unary operators operate on a single operand. They can be overloaded to modify object
behavior.
• ! (Logical NOT)
• & (Address-of)
• * (Dereference)
• ++ (Increment - pre and post)
• -- (Decrement - pre and post)
5. Bitwise Operators
These operators perform bitwise operations on operands and can be overloaded for objects
representing binary data.
6. Logical Operators
These operators are used for logical operations and can be overloaded for custom logical
behavior in objects.
7. Other Operators
These are special-purpose operators that can be overloaded for advanced functionality.
2. Comparison Operators
These operators compare two objects and return a boolean result (true or false).
• == (Equal to): Checks if the values of two Number objects are equal.
• != (Not equal to): Checks if the values of two Number objects are not equal.
3. Assignment Operators
• += (Add and assign): Adds and assigns the value of one object to another.
• -= (Subtract and assign): Subtracts and assigns the value of one object from another.
4. Unary Operators
• ++ (Post-Increment): Increments the value of the object (used after the object).
• -- (Post-Decrement): Decrements the value of the object (used after the object).
5. Other Operators
• [] (Array Subscript Operator): Returns the value plus an index. Simulates array-like
behavior.
• () (Function Call Operator): Allows the object to be called like a function. Prints the
current value.
1. ARITHMETIC OPERATORS
Question 1: Write a program to overload the * operator to multiply two objects of a class Number.
Question 2: Write a program to overload the / operator to divide two objects of a class Number.
2. COMPARISON OPERATORS
Question 1: Write a program to overload the == operator to compare two objects of a class
Compare.
Question 2: Write a program to overload the != operator to check inequality between two objects
of a class Compare.
3. ASSIGNMENT OPERATORS
Question 1: Write a program to overload the += operator to add values to an object.
Question 2: Write a program to overload the -= operator to subtract values from an object.
4. UNARY OPERATORS
Question 1: Write a program to overload the unary ++ operator to increment the value
of an object.
Question 2: Write a program to overload the unary -- operator to decrement the value of an
object.
5. OTHER OPERATORS
Question 1: Write a program to overload the [] operator to access array elements.
Question 2: Write a program to overload the () operator to simulate a function call.
Question: Using all operators listed above in a program.
Objective: Write a C++ program to overload different operators such as arithmetic,
comparison, assignment, unary, and other operators.
// 24BKT0040
#include<iostream>
using namespace std;
class Number {
int value;
public:
Number(int v = 0) : value(v) {}
// Arithmetic Operators
Number operator*(const Number& obj)
{
return Number(value * obj.value);
}
Number operator/(const Number& obj)
{
return Number(value / obj.value);
}
// Comparison Operators
// Assignment Operators
void operator+=(const Number& obj)
{
value += obj.value;
}
void operator-=(const Number& obj)
{
value -= obj.value;
}
// Unary Operators
Number operator++(int)
{
Number temp = *this; value++;
return temp;
}
Number operator--(int)
{
Number temp = *this;
value--;
return temp;
}
// Other Operators
int operator[](int index)
{
return value + index;
}
void operator()()
{
cout << "Function call operator invoked. Value: " << value <<
endl;
}
void display()
{
cout << value << endl;
}
};
int main() {
Number a(10), b(5);
// Arithmetic Operators
Number c = a * b;
cout << "Multiplication: "; c.display();
Number d = a / b;
cout << "Division: "; d.display();
// Comparison Operators
cout << "Equal: " << (a == b) << endl;
cout << "Not Equal: " << (a != b) << endl;
// Assignment Operators
a += b;
cout << "After += : "; a.display();
a -= b;
cout << "After -= : "; a.display();
// Unary Operators
a++;
cout << "After Post-Increment: "; a.display();
a--;
cout << "After Post-Decrement: "; a.display();
// Other Operators
cout << "Array index operator: " << a[2] << endl;
a();
return 0;
}