0% found this document useful (0 votes)
5 views17 pages

OOPS DA

Uploaded by

khushibarbie04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views17 pages

OOPS DA

Uploaded by

khushibarbie04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

STRUCTURED AND OBJECT

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.

Rules for Operator Overloading

1. Only existing operators can be overloaded. New operators cannot be created.


2. Some operators like (scope resolution) ::, .(dot),*, ., and sizeof cannot be
overloaded.
3. At least one operand in the overloaded operator must be a user-defined type (class or
structure).
4. Overloaded operators follow the original precedence and associativity.

Types of Operators and Their Overloading


C++ supports different types of operators that can be overloaded. The main categories
include:

1. Arithmetic Operators

These operators perform mathematical operations on operands. They can be overloaded to


perform arithmetic operations on objects of a class.

• + (Addition)
• - (Subtraction)
• * (Multiplication)
• / (Division)
• % (Modulus)

2. Comparison (Relational) Operators

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.

• & (Bitwise AND)


• | (Bitwise OR)
• ^ (Bitwise XOR)
• ~ (Bitwise Complement)
• << (Left shift)
• >> (Right shift)

6. Logical Operators

These operators are used for logical operations and can be overloaded for custom logical
behavior in objects.

• && (Logical AND)


• || (Logical OR)
• ! (Logical NOT)

7. Other Operators

These are special-purpose operators that can be overloaded for advanced functionality.

• [] (Array subscript) – Allows objects to be accessed like an array.


• () (Function call) – Allows objects to be called like functions.
• -> (Member access) – Used to access members of a class via pointers.
• , (Comma) – Defines behavior for comma-separated expressions.
• new and delete – Custom memory allocation and deallocation.
List of Operators Used with Descriptions
1. Arithmetic Operators

These operators perform mathematical operations.

• * (Multiplication): Multiplies two Number objects.


• / (Division): Divides one Number object by another.

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

These operators assign a value to a variable with an operation.

• += (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

These operate on a single operand and return a new value.

• ++ (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

These are special-purpose operators that add versatility to the class.

• [] (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

bool operator==(const Number& obj)


{
return value == obj.value;
}
bool operator!=(const Number& obj)
{
return value != obj.value;
}

// 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;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy