Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] operator overloading: #201

Closed
1 of 2 tasks
nishant4500 opened this issue Oct 7, 2024 · 1 comment · Fixed by #321
Closed
1 of 2 tasks

[Feature Request] operator overloading: #201

nishant4500 opened this issue Oct 7, 2024 · 1 comment · Fixed by #321
Assignees
Labels
enhancement New feature or request gssoc-ext Contributions made as part of GirlScript Summer of Code Extended Edition. hacktoberfest-accepted PRs accepted for Hacktoberfest 2024. Ensures contributions are counted towards the official Hackt... level2 GirlScript Summer of Code | Contributor's Levels

Comments

@nishant4500
Copy link
Contributor

nishant4500 commented Oct 7, 2024

🚀 Feature Request

content for operator overloading:

Added fundamental theory
Examples for better understanding
Rules and pointers for the concept
Reviewed the content added

4. Operator Overloading

Introduction

Operator overloading allows you to redefine the way operators work for user-defined types (classes and structs). It enables you to specify more intuitive ways to perform operations on objects of your classes.

Overloading an operator does not change:

  • the operator precedence,
  • the associativity of the operator,
  • the arity of the operator, or
  • the meaning of how the operator works on objects of
    built-in types.

Syntax

An overloaded operator is implemented as a special member function with the keyword operator followed by the symbol of the operator being overloaded.

class ClassName {
public:
    ReturnType operatorOpSymbol (ParameterList) {
        // Function body
    }
};

Example

class Complex {
public:
    double real, imag;
    Complex(double r = 0, double i = 0) : real(r), imag(i) {}
    // Overload the + operator
    Complex operator+ (Complex& obj) {
        return Complex(real + obj.real, imag + obj.imag);
    }
};

Types of Operators that Can Be Overloaded

  • Arithmetic operators: +, -, *, /, %
  • Relational operators: ==, !=, <, >, <=, >=
  • Logical operators: &&, ||, !
  • Bitwise operators: &, |, ^, ~, <<, >>
  • Increment and decrement operators: ++, --
  • Assignment operators: =, +=, -=, *=, /=, %=
  • Subscript operator: []
  • Function call operator: ()
  • Member access operators: ->, . (only for pointers to members)
  • Input and output operators: >>, <<

Operators that cannot be overloaded include: ::, .*, ., ? :, sizeof

Example:

#include <iostream>

class Complex {
public:
    double real, imag;

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

    // Overload the == operator
    bool operator== (const Complex& obj) const {
        return (real == obj.real && imag == obj.imag);
    }
};

int main() {
    Complex c1(3.0, 4.0), c2(3.0, 4.0);
    if (c1 == c2) {
        std::cout << "c1 and c2 are equal" << std::endl;
    } else {
        std::cout << "c1 and c2 are not equal" << std::endl;
    }
    return 0;
}

Operator Overloading Rules

When overloading operators, there are several rules to keep in mind:

  1. Preserve the Operator's Original Meaning: The overloaded operator should make sense in the context of the operation it performs.
  2. Return Types: The return type should be appropriate for the operation. For example, operator+ should return a new object, while operator+= should return a reference to *this.
  3. Symmetry: Ensure symmetric behavior where applicable. For example, a == b should return the same result as b == a.
  4. Do Not Overload Operators Irrelevantly: Only overload operators that make sense for your class. For example, overloading the arithmetic operators for a class that represents a complex number makes sense, but overloading them for a class that represents a database connection does not.

Would you like to work on this feature?

  • Yes
  • No
@nishant4500 nishant4500 added the enhancement New feature or request label Oct 7, 2024
@nishant4500 nishant4500 changed the title [Feature Request] [Feature Request] operator overloading: Oct 7, 2024
@ajay-dhangar ajay-dhangar added gssoc-ext Contributions made as part of GirlScript Summer of Code Extended Edition. hacktoberfest-accepted PRs accepted for Hacktoberfest 2024. Ensures contributions are counted towards the official Hackt... level2 GirlScript Summer of Code | Contributor's Levels labels Oct 8, 2024
@nishant4500
Copy link
Contributor Author

@ajay-dhangar can you please look into this..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request gssoc-ext Contributions made as part of GirlScript Summer of Code Extended Edition. hacktoberfest-accepted PRs accepted for Hacktoberfest 2024. Ensures contributions are counted towards the official Hackt... level2 GirlScript Summer of Code | Contributor's Levels
Projects
None yet
2 participants