0% found this document useful (0 votes)
11 views

Operator Overloading in CPP

Operator Overloading in CPP

Uploaded by

murshadbawag11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Operator Overloading in CPP

Operator Overloading in CPP

Uploaded by

murshadbawag11
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Operator Overloading in C++

Understanding the Need and Usage


of Overloaded Operators
Need for Operator Overloading
• Enhance code readability by using intuitive
symbols for object operations.
• Allow operators to work with user-defined
types (like classes).
• Implement custom behaviors for built-in
operators.
• Example: Adding two complex numbers using
'+' instead of a function.
Implicitly Overloaded Operators
• Certain operators are implicitly overloaded by
default for primitive types.
• Example: '+' works for both integers and
floating-point numbers.
• Custom behavior requires explicit operator
overloading.
Binary Arithmetic Operators
• Operators: +, -, *, /, %
• Used to perform arithmetic operations on
objects.
• Example: Overloading '+' to add two complex
numbers.
Example: Binary Operator Overloading
class Complex {
int real, imag;
public:
Complex(int r, int i) : real(r), imag(i) {}
Complex operator+(const Complex &obj) {
return Complex(real + obj.real, imag + obj.imag);
Complex operator-(const Complex &obj) {
return Complex(real - obj.real, imag - obj.imag); }
}
};
Example
Complex operator*(const Complex &obj) { return
Complex(real * obj.real - imag * obj.imag, real *
obj.imag + imag * obj.real);
}
Complex operator/(const Complex &obj) {
int denom = obj.real * obj.real + obj.imag * obj.imag;
int r = (real * obj.real + imag * obj.imag) / denom;
int i = (imag * obj.real - real * obj.imag) / denom;
return Complex(r, i); }
Example
• // Overload % operator (assuming modulus
returns an integer, not a complex number)
int operator%(const Complex &obj) {
return (real * obj.real + imag * obj.imag) %
obj.real; }
// Display function
void display() const { cout << real << " + " << imag
<< "i" << endl; }
};
Example
int main() {
Complex c1(4, 5), c2(2, 3);

Complex c3 = c1 + c2; // Addition


Complex c4 = c1 - c2; // Subtraction
Complex c5 = c1 * c2; // Multiplication
Complex c6 = c1 / c2; // Division
int mod = c1 % c2; // Modulus

cout << "c1: ";


c1.display();
cout << "c2: ";
c2.display();

cout << "c1 + c2: ";


c3.display();

cout << "c1 - c2: ";


c4.display();

cout << "c1 * c2: ";


c5.display();

cout << "c1 / c2: ";


c6.display();

cout << "c1 % c2: " << mod << endl;

return 0;
}
Relational Operators
• Operators: ==, >=, <=, !=
• Used to compare objects.
• Example: Overloading '==' to compare two
objects.
Example
#include <iostream>
using namespace std;
int main() {
Complex c1(4, 5), c2(4, 5), c3(2, 3);
class Complex {
int real, imag;
cout << "c1: ";
public:
// Constructor c1.display();
Complex(int r = 0, int i = 0) : real(r), imag(i) {}
cout << "c2: ";
// Overload == operator c2.display();
bool operator==(const Complex &obj) const {
return (this->real == obj.real && this->imag == obj.imag); cout << "c3: ";
}
c3.display();
// Overload != operator
bool operator!=(const Complex &obj) const {
return (this->real != obj.real || this->imag != obj.imag); // Comparison examples
}
cout << "\nIs c1 == c2? " << (c1 == c2 ? "Yes" :
// Overload >= operator (compares magnitude) "No") << endl;
bool operator>=(const Complex &obj) const {
return (real >= obj.real );
cout << "Is c1 != c3? " << (c1 != c3 ? "Yes" : "No")
} << endl;
// Overload <= operator cout << "Is c1 >= c3? " << (c1 >= c3 ? "Yes" : "No")
bool operator<=(const Complex &obj) const { << endl;
return (real <= obj.real);
} cout << "Is c3 <= c1? " << (c3 <= c1 ? "Yes" : "No")
<< endl;
// Display function
void display() const {

}
cout << real << " + " << imag << "i" << endl;
return 0;
}; }
Unary Operators
• Operators: ++, --
• Overloaded to modify object states or return
computed values.
• Example: Overloading '++' to increment an
object's value.
Example
#include <iostream> int main() {
using namespace std;
Complex c1(5, 6), c2(3, 4), c3;
class Complex {
int real, imag; cout << "Initial c1: ";
c1.display();
public: cout << "Initial c2: ";
// Constructor
c2.display();
Complex(int r = 0, int i = 0) : real(r), imag(i) {}

// Overload pre-increment ++ // Overloaded + and - operators


Complex& operator++() { c3 = c1 + c2;
++real; cout << "\nc3 (c1 + c2): ";
++imag;
c3.display();
return *this;
}
c3 = c1 - c2;
// Overload post-increment ++ cout << "c3 (c1 - c2): ";
Complex operator++(int) { c3.display();
Complex temp = *this;
real++;
imag++; // Overloaded ++ operators
return temp; cout << "\nPre-increment c1: ";
} ++c1;
c1.display();
// Overload pre-decrement --
Complex& operator--() {
--real; cout << "Post-increment c1: ";
--imag; c1++;
return *this; c1.display();
}

// Overload post-decrement --
// Overloaded -- operators
Complex operator--(int) { cout << "\nPre-decrement c2: ";
Complex temp = *this; --c2;
real--; c2.display();
imag--;
return temp;
} cout << "Post-decrement c2: ";
c2--;
// Display function c2.display();
void display() const { system("pause");
cout << real << " + " << imag << "i" << endl;
return 0;
}
}; }
Stream Insertion and Extraction
• Operators: <<, >>
• Used to input and output user-defined
objects.
• Example: Overloading '<<' to display object
details.
Example: Stream Operators
#include<iostream>
using namespace std; int main()
{
class Complex { Complex c1, c2(2, 'i');
int real; cin >> c1;
char imag;
cout << c1 <<endl;
public:
Complex() {
real = 0; cout << c2;
imag = 'i';
} system("pause");
Complex(int r, char i) : real(r), imag(i) {} return 0;
// Overload >> (stream extraction operator) for input }
friend istream& operator>>(istream& in, Complex& c) {
cout << "Enter real part: ";
in >> c.real;
cout << "Enter imaginary part: ";
in >> c.imag;
return in; // Allow chaining
}

// Overload << (stream insertion operator) for output


friend ostream& operator<<(ostream& out, const Complex& c) {
out << c.real << c.imag;
return out; // Allow chaining
}
};

You might also like