diff --git a/README.md b/README.md index 2bd673209..2dc179f6d 100644 --- a/README.md +++ b/README.md @@ -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 @@ -89,6 +90,13 @@ We are grateful to all the contributors who have helped improve this project. Yo Pavitraa G + + + T-Rahul-prabhu-38 +
+ t rahul prabhu +
+ Ankitha2130 @@ -104,12 +112,14 @@ We are grateful to all the contributors who have helped improve this project. Yo - - T-Rahul-prabhu-38 + + Shariq2003
- t rahul prabhu + Shariq
+ + ananyag309 @@ -117,34 +127,32 @@ We are grateful to all the contributors who have helped improve this project. Yo Ananya Gupta - - - - narendra-dhangar + + kRajoria121
- Narendra Dhangar + Kundan Rajoria
- - IRFANSARI + + yashksaini-coder
- Irfan Ansari + Yash Kumar Saini
- - yashksaini-coder + + IRFANSARI
- Yash Kumar Saini + Irfan Ansari
- - Shariq2003 + + tanushrigoel
- Shariq + tanushrigoel
@@ -153,6 +161,15 @@ We are grateful to all the contributors who have helped improve this project. Yo
Soumyadeep Paul + + + + + + narendra-dhangar +
+ Narendra Dhangar +
@@ -161,8 +178,6 @@ We are grateful to all the contributors who have helped improve this project. Yo Chekka Yogeswari - - Hamza1821 @@ -170,12 +185,35 @@ We are grateful to all the contributors who have helped improve this project. Yo Hamza Mubin + + + shalini-bhandari +
+ Shalini Bhandari +
+ + + + monishkumardvs +
+ monishkumardvs +
+ vedhcet-07
Vishwas M D
+ + + + + + PRASHANTSWAROOP001 +
+ PRASHANT SWAROOP +
@@ -191,6 +229,13 @@ We are grateful to all the contributors who have helped improve this project. Yo Mugundh J B + + + Himanshi-m +
+ Himanshi Maheshwari +
+ Bhum-ika @@ -199,19 +244,19 @@ We are grateful to all the contributors who have helped improve this project. Yo - - Akki-58 + + Anandha-Vihari
- AJ + Anandha-Vihari
- - Kratik1093 + + sriraghavi22
- Kratik Mandloi + sriraghavi22
@@ -222,10 +267,24 @@ We are grateful to all the contributors who have helped improve this project. Yo - - shalini-bhandari + + Kratik1093
- Shalini Bhandari + Kratik Mandloi +
+ + + + Akki-58 +
+ AJ +
+ + + + 4F24L +
+ Md Afzal Mir
@@ -235,6 +294,8 @@ We are grateful to all the contributors who have helped improve this project. Yo Harsh + + IRFANSARI2 @@ -249,8 +310,13 @@ We are grateful to all the contributors who have helped improve this project. Yo Imgbot - - + + + jvkousthub +
+ Kousthub J V +
+ shubhagarwal1 @@ -265,12 +331,35 @@ We are grateful to all the contributors who have helped improve this project. Yo Oebelus + + + Rahul7raj +
+ Rahul7raj +
+ + + rajatsinghal02
Rajat singhal
+ + + + RchtDshr +
+ Rachita Dashore +
+ + + + nishant4500 +
+ nishant4500 +
diff --git a/docs/programming-fundamentals/OperatorOverloading.md b/docs/programming-fundamentals/OperatorOverloading.md new file mode 100644 index 000000000..02589e32d --- /dev/null +++ b/docs/programming-fundamentals/OperatorOverloading.md @@ -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 + +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. diff --git a/docs/programming-fundamentals/pointers.md b/docs/programming-fundamentals/pointers.md new file mode 100644 index 000000000..b46f55f45 --- /dev/null +++ b/docs/programming-fundamentals/pointers.md @@ -0,0 +1,197 @@ +--- +id: programming-fundamentals +title: References and Pointers in C++ +sidebar_label: Introduction to References and Pointers +sidebar_position: 2 +description: 'Both references and pointers are powerful features in C++ that allow you to manipulate memory and create more efficient programs. Understanding the differences between them and knowing when to use each is crucial for effective C++ programming' +tags: [dsa, data-structures, Pointers] +--- + +## 1. What are References and Pointers in C++? + +In C++, both references and pointers are used to refer to memory locations, but they have different properties and uses. + +- **References**: An alias for another variable. +- **Pointers**: A variable that holds the memory address of another variable. + + + +## 2. Declaring References and Pointers + +### Declaring References + +A reference is declared using the `&` operator. + +**Syntax:** + +```cpp +datatype &referenceName = variableName; +``` + +**Example:** + +```cpp +#include +using namespace std; + +int main() { + int a = 10; + int &ref = a; // ref is a reference to a + cout << "a: " << a << endl; // Output: 10 + cout << "ref: " << ref << endl; // Output: 10 + return 0; +} +``` + +### Declaring Pointers + +A pointer is declared using the `*` operator. + +**Syntax:** + +```cpp +datatype *pointerName; +``` + +**Example:** + +```cpp +#include +using namespace std; + +int main() { + int a = 10; + int *ptr = &a; // ptr holds the address of a + cout << "a: " << a << endl; // Output: 10 + cout << "ptr: " << ptr << endl; // Output: address of a + cout << "*ptr: " << *ptr << endl; // Output: 10 + return 0; +} +``` + +## 3. Differences Between References and Pointers + +### 1. Syntax and Usage + +- **References**: Use the `&` operator for declaration and do not require dereferencing. +- **Pointers**: Use the `*` operator for declaration and require dereferencing with `*`. + +**Example:** + +```cpp +int a = 5; +int &ref = a; // Reference +int *ptr = &a; // Pointer +``` + +### 2. Initialization + +- **References**: Must be initialized when declared. +- **Pointers**: Can be declared without initialization and assigned later. + +**Example:** + +```cpp +int a = 5; +int &ref = a; // Must be initialized +int *ptr; // Can be declared without initialization +ptr = &a; // Assigned later +``` + +### 3. Reassignment + +- **References**: Cannot be reassigned to refer to another variable. +- **Pointers**: Can be reassigned to point to different variables. + +**Example:** + +```cpp +int a = 5, b = 10; +int &ref = a; // Reference to a +// ref = b; // Error: Cannot reassign a reference + +int *ptr = &a; // Pointer to a +ptr = &b; // Pointer reassigned to b +``` + +### 4. Null Values + +- **References**: Cannot be null. +- **Pointers**: Can be null (useful to indicate that they are not pointing to any valid memory). + +**Example:** + +```cpp +int a = 5; +int &ref = a; // Reference cannot be null + +int *ptr = nullptr; // Pointer can be null +ptr = &a; // Pointer assigned to a valid address +``` + +### 5. Memory Address + +- **References**: Implicitly provide the memory address of the referenced variable. +- **Pointers**: Explicitly hold and can be used to manipulate memory addresses. + +**Example:** + +```cpp +int a = 5; +int &ref = a; // Reference to a +int *ptr = &a; // Pointer to a + +cout << "Address of a: " << &a << endl; +cout << "Address using ref: " << &ref << endl; +cout << "Address using ptr: " << ptr << endl; +``` + +## 4. When to Use References and Pointers + +### Use References: +- When you need an alias for another variable. +- When you want to avoid null checks and pointer arithmetic. +- For pass-by-reference in function arguments to avoid copying large objects. + +### Use Pointers: +- When you need to allocate memory dynamically. +- When you need to implement data structures like linked lists, trees, etc. +- When you need to use null values to indicate "no object". + +## 5. Examples + +### Example 1: Using References + +```cpp +#include +using namespace std; + +void increment(int &ref) { + ref++; +} + +int main() { + int a = 10; + increment(a); + cout << "a: " << a << endl; // Output: 11 + return 0; +} +``` +### Example 2: Using Pointers +```cpp +#include +using namespace std; +void increment(int *ptr) { + (*ptr)++; +} +int main() { + int a = 10; + increment(&a); + cout << "a: " << a << endl; // Output: 11 + return 0; +} +``` + +## 6. Conclusion + +Both references and pointers are powerful features in C++ that allow you to manipulate memory and create more efficient programs. Understanding the differences between them and knowing when to use each is crucial for effective C++ programming