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

References and Pointers in C++ && Operator Overloading #243

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 119 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ We aim to:
- **Multi-language Support**: Algorithms in multiple programming languages
- **Beginner-Friendly**: Well-structured, easy-to-understand explanations
- **Open Source Contributions**: Welcoming developers at all levels to contribute
- **Community Forum**: A new interactive platform where developers can ask questions, share ideas, and collaborate in real-time. This forum will help bridge the gap between beginners and experts, allowing for deeper engagement and problem-solving.

## Website

Expand Down Expand Up @@ -89,6 +90,13 @@ We are grateful to all the contributors who have helped improve this project. Yo
<sub><b>Pavitraa G</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/T-Rahul-prabhu-38">
<img src="https://avatars.githubusercontent.com/u/167653990?v=4" width="100;" alt="T-Rahul-prabhu-38"/>
<br />
<sub><b>t rahul prabhu</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Ankitha2130">
<img src="https://avatars.githubusercontent.com/u/149484435?v=4" width="100;" alt="Ankitha2130"/>
Expand All @@ -104,47 +112,47 @@ We are grateful to all the contributors who have helped improve this project. Yo
</a>
</td>
<td align="center">
<a href="https://github.com/T-Rahul-prabhu-38">
<img src="https://avatars.githubusercontent.com/u/167653990?v=4" width="100;" alt="T-Rahul-prabhu-38"/>
<a href="https://github.com/Shariq2003">
<img src="https://avatars.githubusercontent.com/u/109452033?v=4" width="100;" alt="Shariq2003"/>
<br />
<sub><b>t rahul prabhu</b></sub>
<sub><b>Shariq</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/ananyag309">
<img src="https://avatars.githubusercontent.com/u/145869907?v=4" width="100;" alt="ananyag309"/>
<br />
<sub><b>Ananya Gupta</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/narendra-dhangar">
<img src="https://avatars.githubusercontent.com/u/161828345?v=4" width="100;" alt="narendra-dhangar"/>
<a href="https://github.com/kRajoria121">
<img src="https://avatars.githubusercontent.com/u/138133004?v=4" width="100;" alt="kRajoria121"/>
<br />
<sub><b>Narendra Dhangar </b></sub>
<sub><b>Kundan Rajoria</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/IRFANSARI">
<img src="https://avatars.githubusercontent.com/u/71096605?v=4" width="100;" alt="IRFANSARI"/>
<a href="https://github.com/yashksaini-coder">
<img src="https://avatars.githubusercontent.com/u/115717039?v=4" width="100;" alt="yashksaini-coder"/>
<br />
<sub><b>Irfan Ansari</b></sub>
<sub><b>Yash Kumar Saini</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/yashksaini-coder">
<img src="https://avatars.githubusercontent.com/u/115717039?v=4" width="100;" alt="yashksaini-coder"/>
<a href="https://github.com/IRFANSARI">
<img src="https://avatars.githubusercontent.com/u/71096605?v=4" width="100;" alt="IRFANSARI"/>
<br />
<sub><b>Yash Kumar Saini</b></sub>
<sub><b>Irfan Ansari</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Shariq2003">
<img src="https://avatars.githubusercontent.com/u/109452033?v=4" width="100;" alt="Shariq2003"/>
<a href="https://github.com/tanushrigoel">
<img src="https://avatars.githubusercontent.com/u/115028578?v=4" width="100;" alt="tanushrigoel"/>
<br />
<sub><b>Shariq</b></sub>
<sub><b>tanushrigoel</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -153,6 +161,15 @@ We are grateful to all the contributors who have helped improve this project. Yo
<br />
<sub><b>Soumyadeep Paul</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/narendra-dhangar">
<img src="https://avatars.githubusercontent.com/u/161828345?v=4" width="100;" alt="narendra-dhangar"/>
<br />
<sub><b>Narendra Dhangar </b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/yogeswari05">
Expand All @@ -161,21 +178,42 @@ We are grateful to all the contributors who have helped improve this project. Yo
<sub><b>Chekka Yogeswari</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Hamza1821">
<img src="https://avatars.githubusercontent.com/u/118290407?v=4" width="100;" alt="Hamza1821"/>
<br />
<sub><b>Hamza Mubin</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/shalini-bhandari">
<img src="https://avatars.githubusercontent.com/u/76897566?v=4" width="100;" alt="shalini-bhandari"/>
<br />
<sub><b>Shalini Bhandari</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/monishkumardvs">
<img src="https://avatars.githubusercontent.com/u/125813798?v=4" width="100;" alt="monishkumardvs"/>
<br />
<sub><b>monishkumardvs</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/vedhcet-07">
<img src="https://avatars.githubusercontent.com/u/176995332?v=4" width="100;" alt="vedhcet-07"/>
<br />
<sub><b>Vishwas M D</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/PRASHANTSWAROOP001">
<img src="https://avatars.githubusercontent.com/u/56585080?v=4" width="100;" alt="PRASHANTSWAROOP001"/>
<br />
<sub><b>PRASHANT SWAROOP</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/nishakp3005">
Expand All @@ -191,6 +229,13 @@ We are grateful to all the contributors who have helped improve this project. Yo
<sub><b>Mugundh J B</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Himanshi-m">
<img src="https://avatars.githubusercontent.com/u/140889204?v=4" width="100;" alt="Himanshi-m"/>
<br />
<sub><b>Himanshi Maheshwari</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Bhum-ika">
<img src="https://avatars.githubusercontent.com/u/91523494?v=4" width="100;" alt="Bhum-ika"/>
Expand All @@ -199,19 +244,19 @@ We are grateful to all the contributors who have helped improve this project. Yo
</a>
</td>
<td align="center">
<a href="https://github.com/Akki-58">
<img src="https://avatars.githubusercontent.com/u/154585091?v=4" width="100;" alt="Akki-58"/>
<a href="https://github.com/Anandha-Vihari">
<img src="https://avatars.githubusercontent.com/u/177409005?v=4" width="100;" alt="Anandha-Vihari"/>
<br />
<sub><b>AJ</b></sub>
<sub><b>Anandha-Vihari</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/Kratik1093">
<img src="https://avatars.githubusercontent.com/u/153417068?v=4" width="100;" alt="Kratik1093"/>
<a href="https://github.com/sriraghavi22">
<img src="https://avatars.githubusercontent.com/u/140194099?v=4" width="100;" alt="sriraghavi22"/>
<br />
<sub><b>Kratik Mandloi </b></sub>
<sub><b>sriraghavi22</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -222,10 +267,24 @@ We are grateful to all the contributors who have helped improve this project. Yo
</a>
</td>
<td align="center">
<a href="https://github.com/shalini-bhandari">
<img src="https://avatars.githubusercontent.com/u/76897566?v=4" width="100;" alt="shalini-bhandari"/>
<a href="https://github.com/Kratik1093">
<img src="https://avatars.githubusercontent.com/u/153417068?v=4" width="100;" alt="Kratik1093"/>
<br />
<sub><b>Shalini Bhandari</b></sub>
<sub><b>Kratik Mandloi </b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Akki-58">
<img src="https://avatars.githubusercontent.com/u/154585091?v=4" width="100;" alt="Akki-58"/>
<br />
<sub><b>AJ</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/4F24L">
<img src="https://avatars.githubusercontent.com/u/108697331?v=4" width="100;" alt="4F24L"/>
<br />
<sub><b>Md Afzal Mir</b></sub>
</a>
</td>
<td align="center">
Expand All @@ -235,6 +294,8 @@ We are grateful to all the contributors who have helped improve this project. Yo
<sub><b>Harsh</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/IRFANSARI2">
<img src="https://avatars.githubusercontent.com/u/171218596?v=4" width="100;" alt="IRFANSARI2"/>
Expand All @@ -249,8 +310,13 @@ We are grateful to all the contributors who have helped improve this project. Yo
<sub><b>Imgbot</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/jvkousthub">
<img src="https://avatars.githubusercontent.com/u/162019723?v=4" width="100;" alt="jvkousthub"/>
<br />
<sub><b>Kousthub J V</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/shubhagarwal1">
<img src="https://avatars.githubusercontent.com/u/105449260?v=4" width="100;" alt="shubhagarwal1"/>
Expand All @@ -265,12 +331,35 @@ We are grateful to all the contributors who have helped improve this project. Yo
<sub><b>Oebelus</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Rahul7raj">
<img src="https://avatars.githubusercontent.com/u/69787135?v=4" width="100;" alt="Rahul7raj"/>
<br />
<sub><b>Rahul7raj</b></sub>
</a>
</td>
</tr>
<tr>
<td align="center">
<a href="https://github.com/rajatsinghal02">
<img src="https://avatars.githubusercontent.com/u/112543741?v=4" width="100;" alt="rajatsinghal02"/>
<br />
<sub><b>Rajat singhal</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/RchtDshr">
<img src="https://avatars.githubusercontent.com/u/58704284?v=4" width="100;" alt="RchtDshr"/>
<br />
<sub><b>Rachita Dashore</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/nishant4500">
<img src="https://avatars.githubusercontent.com/u/135944619?v=4" width="100;" alt="nishant4500"/>
<br />
<sub><b>nishant4500</b></sub>
</a>
</td>
</tr>
<tbody>
Expand Down
96 changes: 96 additions & 0 deletions docs/programming-fundamentals/OperatorOverloading.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
id: programming-fundamentals
title: Operator Overloading
sidebar_label: Introduction to Operator Overloading
sidebar_position: 2
description: '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.'
tags: [dsa, data-structures, Operator Overloading]
---

## 2 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.

```cpp
class ClassName {
public:
ReturnType operatorOpSymbol (ParameterList) {
// Function body
}
};
```
### Example
```cpp
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:
```cpp
#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.
Loading
Loading