0% found this document useful (0 votes)
2 views5 pages

Operator overloading

Operator overloading in C++ allows the redefinition of operators for user-defined types, enhancing code readability and intuitiveness. Key points include the syntax for overloading, the types of operators that can be overloaded, and guidelines for maintaining consistency and efficiency. Examples demonstrate how to overload the + and == operators for a Complex number class.

Uploaded by

Muzzamil Rani
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)
2 views5 pages

Operator overloading

Operator overloading in C++ allows the redefinition of operators for user-defined types, enhancing code readability and intuitiveness. Key points include the syntax for overloading, the types of operators that can be overloaded, and guidelines for maintaining consistency and efficiency. Examples demonstrate how to overload the + and == operators for a Complex number class.

Uploaded by

Muzzamil Rani
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/ 5

Operator overloading in C++ allows you to redefine the way operators work for user-defined

types (classes and structs). This can make code involving custom types more intuitive and
readable.

Key Points of Operator Overloading

1. Syntax: Operators are overloaded using the operator keyword followed by the
operator symbol.
2. Member Function or Friend Function: Operators can be overloaded as member
functions or as non-member (often friend) functions.
3. Preserve Operator's Original Meaning: It’s a good practice to keep the semantic
meaning of the operator similar to its original intent.
4. Cannot Overload Certain Operators: Operators like ::, .:, .*, sizeof, typeid, and
? : cannot be overloaded.
5. Custom Behavior: Operator overloading allows you to define custom behavior for
operators when applied to objects of your class.

Types of Operators That Can Be Overloaded

1. Arithmetic Operators: +, -, *, /, %
2. Relational Operators: ==, !=, <, >, <=, >=
3. Logical Operators: &&, ||, !
4. Bitwise Operators: &, |, ^, ~, <<, >>
5. Increment/Decrement Operators: ++, --
6. Assignment Operators: =, +=, -=, *=, /=, %= etc.
7. Subscript Operator: []
8. Function Call Operator: ()
9. Dereference Operator: *
10. Member Access Operators: ->, .

Syntax of Operator Overloading

Member Function:

ReturnType ClassName::operator OpSymbol (ParameterList) {


// Implementation
}

Non-Member Function:
ReturnType operator OpSymbol (const ClassName& obj1, const ClassName&
obj2) {
// Implementation
}

Example: Overloading the + Operator for a Complex Number Class

Let's create a class Complex and overload the + operator to add two complex numbers.

Step-by-Step Example
Define the Class:

#include <iostream>
using namespace std;

class Complex {
private:
double real;
double imag;

public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}

// Overload + operator as a member function


Complex operator + (const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}

// Function to display the complex number


void display() const {
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(3.0, 4.0);
Complex c2(1.0, 2.0);
Complex c3 = c1 + c2; // Using the overloaded + operator

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


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

return 0;
}

1.

Explanation:

● The Complex class represents complex numbers with real and imaginary parts.
● The operator+ function is defined as a member function to overload the + operator. It
takes a const Complex& as a parameter and returns a new Complex object that
represents the sum of the two complex numbers.
● The display function is used to print the complex number.

Example: Overloading the == Operator for a Complex Number Class

Let's extend the Complex class to overload the == operator.

#include <iostream>
using namespace std;

class Complex {
private:
double real;
double imag;

public:
Complex(double r = 0, double i = 0) : real(r), imag(i) {}

// Overload + operator as a member function


Complex operator + (const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
// Overload == operator as a member function
bool operator == (const Complex& other) const {
return (real == other.real && imag == other.imag);
}

// Function to display the complex number


void display() const {
cout << real << " + " << imag << "i" << endl;
}
};

int main() {
Complex c1(3.0, 4.0);
Complex c2(1.0, 2.0);
Complex c3 = c1 + c2; // Using the overloaded + operator
Complex c4(4.0, 6.0);

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


cout << "c2: "; c2.display();
cout << "c3: "; c3.display();
cout << "c4: "; c4.display();

if (c3 == c4) {
cout << "c3 is equal to c4" << endl;
} else {
cout << "c3 is not equal to c4" << endl;
}

return 0;
}

Explanation:

● The operator== function is defined as a member function to overload the == operator.


It takes a const Complex& as a parameter and returns a boolean indicating whether
the two complex numbers are equal.
● The main function demonstrates the usage of both the + and == operators.
Guidelines for Operator Overloading

1. Consistency: The overloaded operator should behave in a way consistent with its usual
meaning. For example, + should perform addition, == should compare for equality, etc.
2. Symmetry: Ensure that the operations are commutative when appropriate (e.g., a + b
should be the same as b + a if it makes sense for your data type).
3. Efficiency: Overloaded operators should be implemented efficiently to avoid
unnecessary performance overhead.
4. Error Handling: Ensure that the overloaded operators handle errors and edge cases
gracefully.

Commonly Overloaded Operators

● Arithmetic Operators (+, -, *, /, %)


● Relational Operators (==, !=, <, >, <=, >=)
● Logical Operators (&&, ||, !)
● Increment/Decrement Operators (++, --)
● Stream Insertion and Extraction Operators (<<, >>)
● Subscript Operator ([])
● Function Call Operator (())
● Dereference Operator (*)
● Member Access Operators (->, .)

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