|
| 1 | +<h1 align='center'>Overload - Operators</h1> |
| 2 | + |
| 3 | +## Problem Statement |
| 4 | + |
| 5 | +**Problem URL :** [Overload Operators](https://www.hackerrank.com/challenges/overload-operators/problem?isFullScreen=true) |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | + |
| 10 | +## Problem Solution |
| 11 | +```cpp |
| 12 | +//Operator Overloading |
| 13 | + |
| 14 | +#include<iostream> |
| 15 | + |
| 16 | +using namespace std; |
| 17 | + |
| 18 | +class Complex |
| 19 | +{ |
| 20 | +public: |
| 21 | + int a,b; |
| 22 | + void input(string s) |
| 23 | + { |
| 24 | + int v1=0; |
| 25 | + int i=0; |
| 26 | + while(s[i]!='+') |
| 27 | + { |
| 28 | + v1=v1*10+s[i]-'0'; |
| 29 | + i++; |
| 30 | + } |
| 31 | + while(s[i]==' ' || s[i]=='+'||s[i]=='i') |
| 32 | + { |
| 33 | + i++; |
| 34 | + } |
| 35 | + int v2=0; |
| 36 | + while(i<s.length()) |
| 37 | + { |
| 38 | + v2=v2*10+s[i]-'0'; |
| 39 | + i++; |
| 40 | + } |
| 41 | + a=v1; |
| 42 | + b=v2; |
| 43 | + } |
| 44 | +}; |
| 45 | + |
| 46 | +Complex operator+(const Complex& x, const Complex& y){ |
| 47 | + Complex result; |
| 48 | + |
| 49 | + result.a = x.a + y.a; |
| 50 | + result.b = x.b + y.b; |
| 51 | + return result; |
| 52 | +} |
| 53 | + |
| 54 | +ostream& operator<<(ostream& os, const Complex& z){ |
| 55 | + os << z.a << "+i" << z.b; |
| 56 | + return os; |
| 57 | +} |
| 58 | + |
| 59 | +int main() |
| 60 | +{ |
| 61 | + Complex x,y; |
| 62 | + string s1,s2; |
| 63 | + cin>>s1; |
| 64 | + cin>>s2; |
| 65 | + x.input(s1); |
| 66 | + y.input(s2); |
| 67 | + Complex z=x+y; |
| 68 | + cout<<z<<endl; |
| 69 | +} |
| 70 | + |
| 71 | +``` |
| 72 | + |
| 73 | +## Problem Solution Explanation |
| 74 | +Let's go through the code line by line and explain its functionality in detail. |
| 75 | + |
| 76 | +```cpp |
| 77 | +#include<iostream> |
| 78 | +``` |
| 79 | +- This line includes the input-output stream library in C++, which allows you to use `std::cin` for input and `std::cout` for output. |
| 80 | + |
| 81 | +```cpp |
| 82 | +using namespace std; |
| 83 | +``` |
| 84 | +- This line allows you to use names from the `std` namespace (like `cout`, `cin`, etc.) without having to prefix them with `std::`. It's a common practice to simplify the code. |
| 85 | + |
| 86 | +### Class Definition |
| 87 | + |
| 88 | +```cpp |
| 89 | +class Complex |
| 90 | +{ |
| 91 | +public: |
| 92 | + int a,b; |
| 93 | +``` |
| 94 | +- Here, a class `Complex` is defined. This class has two public member variables, `a` and `b`, which represent the real part and the imaginary part of a complex number, respectively. |
| 95 | +
|
| 96 | +For example, if you have a complex number `3 + i4`, then `a = 3` and `b = 4`. |
| 97 | +
|
| 98 | +```cpp |
| 99 | + void input(string s) |
| 100 | + { |
| 101 | + int v1=0; |
| 102 | + int i=0; |
| 103 | +``` |
| 104 | +- The `input` function takes a string `s` as input, which represents a complex number in the format "real_part + imaginary_part". `v1` and `i` are integer variables used to parse the real and imaginary parts from the string. |
| 105 | + |
| 106 | +```cpp |
| 107 | + while(s[i]!='+') |
| 108 | + { |
| 109 | + v1=v1*10+s[i]-'0'; |
| 110 | + i++; |
| 111 | + } |
| 112 | +``` |
| 113 | +- This loop extracts the real part of the complex number from the string. It starts from the beginning of the string and keeps reading characters until it finds the '+' sign, which separates the real and imaginary parts. |
| 114 | +- `v1=v1*10+s[i]-'0';` converts the character digit to an integer and adds it to `v1`. This builds the integer value digit by digit. |
| 115 | +- Example: For the string "3+i4", `v1` becomes `3`. |
| 116 | +
|
| 117 | +```cpp |
| 118 | + while(s[i]==' ' || s[i]=='+'||s[i]=='i') |
| 119 | + { |
| 120 | + i++; |
| 121 | + } |
| 122 | +``` |
| 123 | +- This loop skips any characters that are spaces, the '+' sign, or the 'i' character. This moves the index `i` to the beginning of the imaginary part in the string. |
| 124 | + |
| 125 | +```cpp |
| 126 | + int v2=0; |
| 127 | + while(i<s.length()) |
| 128 | + { |
| 129 | + v2=v2*10+s[i]-'0'; |
| 130 | + i++; |
| 131 | + } |
| 132 | +``` |
| 133 | +- This loop extracts the imaginary part of the complex number from the string, just like how the real part was extracted earlier. |
| 134 | +- `v2=v2*10+s[i]-'0';` converts the characters representing the imaginary part into an integer. |
| 135 | +- Example: For the string "3+i4", `v2` becomes `4`. |
| 136 | +
|
| 137 | +```cpp |
| 138 | + a=v1; |
| 139 | + b=v2; |
| 140 | + } |
| 141 | +``` |
| 142 | +- The extracted values `v1` and `v2` are assigned to the member variables `a` and `b` of the `Complex` object, representing the real and imaginary parts, respectively. |
| 143 | + |
| 144 | +### Operator Overloading |
| 145 | + |
| 146 | +```cpp |
| 147 | +Complex operator+(const Complex& x, const Complex& y) |
| 148 | +{ |
| 149 | + Complex result; |
| 150 | + |
| 151 | + result.a = x.a + y.a; |
| 152 | + result.b = x.b + y.b; |
| 153 | + return result; |
| 154 | +} |
| 155 | +``` |
| 156 | +- This function overloads the `+` operator to allow the addition of two `Complex` objects. |
| 157 | +- It takes two `Complex` objects `x` and `y` as input. |
| 158 | +- Inside the function, a new `Complex` object `result` is created. |
| 159 | +- The real parts (`a`) of `x` and `y` are added together and stored in `result.a`. |
| 160 | +- The imaginary parts (`b`) of `x` and `y` are added together and stored in `result.b`. |
| 161 | +- The `result` object is returned, representing the sum of the two complex numbers. |
| 162 | + |
| 163 | +Example: |
| 164 | +- If `x` is `3+i4` and `y` is `5+i6`, then `result.a` will be `3+5=8` and `result.b` will be `4+6=10`. So, the resulting complex number will be `8+i10`. |
| 165 | + |
| 166 | +```cpp |
| 167 | +ostream& operator<<(ostream& os, const Complex& z) |
| 168 | +{ |
| 169 | + os << z.a << "+i" << z.b; |
| 170 | + return os; |
| 171 | +} |
| 172 | +``` |
| 173 | +- This function overloads the `<<` operator to allow a `Complex` object to be output using `cout`. |
| 174 | +- It takes an `ostream` object `os` (like `cout`) and a `Complex` object `z`. |
| 175 | +- It outputs the complex number in the format "real_part+iimaginary_part". |
| 176 | +- Finally, it returns the `ostream` object `os` to allow chaining of `<<` operations. |
| 177 | + |
| 178 | +Example: |
| 179 | +- If `z` is `8+i10`, `cout << z;` will output `8+i10`. |
| 180 | + |
| 181 | +### Main Function |
| 182 | + |
| 183 | +```cpp |
| 184 | +int main() |
| 185 | +{ |
| 186 | + Complex x,y; |
| 187 | + string s1,s2; |
| 188 | + cin>>s1; |
| 189 | + cin>>s2; |
| 190 | +``` |
| 191 | +- The `main` function begins by creating two `Complex` objects `x` and `y`. |
| 192 | +- It then declares two strings `s1` and `s2`. |
| 193 | +- The program reads two complex numbers in string format from the user and stores them in `s1` and `s2`. |
| 194 | +
|
| 195 | +Example: |
| 196 | +- The user enters "3+i4" and "5+i6". |
| 197 | +
|
| 198 | +```cpp |
| 199 | + x.input(s1); |
| 200 | + y.input(s2); |
| 201 | +``` |
| 202 | +- The `input` function is called on `x` and `y` to parse the strings `s1` and `s2`, setting the real and imaginary parts of `x` and `y`. |
| 203 | + |
| 204 | +```cpp |
| 205 | + Complex z=x+y; |
| 206 | +``` |
| 207 | +- The overloaded `+` operator is used to add the two complex numbers `x` and `y`. The result is stored in a new `Complex` object `z`. |
| 208 | + |
| 209 | +Example: |
| 210 | +- `z` will be `8+i10` when `x` is `3+i4` and `y` is `5+i6`. |
| 211 | + |
| 212 | +```cpp |
| 213 | + cout<<z<<endl; |
| 214 | +} |
| 215 | +``` |
| 216 | +- The overloaded `<<` operator is used to output the complex number `z`. The output is followed by a newline. |
| 217 | +- The program prints the resulting complex number to the console. |
| 218 | + |
| 219 | +Example: |
| 220 | +- The output will be `8+i10`. |
| 221 | + |
| 222 | +### Summary |
| 223 | +- This code allows you to input two complex numbers, adds them together, and prints the result. |
| 224 | +- Operator overloading is used to simplify the addition and output operations for complex numbers, making the code more intuitive and readable. |
0 commit comments