Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c9d3430

Browse files
Operator Overloading
1 parent 86968e5 commit c9d3430

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
// Operator Overloading C++
2+
3+
// Link -> https://www.hackerrank.com/challenges/overload-operators/problem?isFullScreen=true
4+
5+
/*
6+
You are given a class - Complex.
7+
8+
class Complex
9+
{
10+
public:
11+
int a,b;
12+
};
13+
Operators are overloaded by means of operator functions, which are regular functions with special names. Their name begins with the operator keyword followed by the operator sign that is overloaded. The syntax is:
14+
15+
type operator sign (parameters) { ... body ... }
16+
You need to overload operators + and << for the Complex class.
17+
18+
The operator + should add complex numbers according to the rules of complex addition:
19+
20+
(a+ib)+(c+id) = (a+c) + i(b+d)
21+
Overload the stream insertion operator << to add "" to the stream:
22+
23+
cout<<c<<endl;
24+
The above statement should print "" followed by a newline where and .
25+
26+
Input Format
27+
28+
The overloaded operator + should receive two complex numbers ( and ) as parameters. It must return a single complex number.
29+
30+
The overloaded operator << should add "" to the stream where is the real part and is the imaginary part of the complex number which is then passed as a parameter to the overloaded operator.
31+
32+
Output Format
33+
34+
As per the problem statement, for the output, print "" followed by a newline where and .
35+
36+
Sample Input
37+
38+
3+i4
39+
5+i6
40+
Sample Output
41+
42+
8+i10
43+
Explanation
44+
45+
Given output after performing required operations (overloading + operator) is 8+i10.
46+
*/
47+
48+
#include<bits/stdc++.h>
49+
#define endl "\n"
50+
51+
using namespace std;
52+
53+
class Complex
54+
{
55+
public:
56+
int a,b;
57+
void input(string s)
58+
{
59+
int v1=0;
60+
int i=0;
61+
while(s[i]!='+')
62+
{
63+
v1=v1*10+s[i]-'0';
64+
i++;
65+
}
66+
while(s[i]==' ' || s[i]=='+'||s[i]=='i')
67+
{
68+
i++;
69+
}
70+
int v2=0;
71+
while(i<s.length())
72+
{
73+
v2=v2*10+s[i]-'0';
74+
i++;
75+
}
76+
a=v1;
77+
b=v2;
78+
}
79+
};
80+
81+
Complex operator +(const Complex X, const Complex Y)
82+
{
83+
Complex Z;
84+
Z.a = X.a + Y.a;
85+
Z.b = X.b + Y.b;
86+
87+
return Z;
88+
}
89+
ostream& operator <<(ostream& os, const Complex C)
90+
{
91+
return os << C.a << "+" << "i" << C.b;
92+
}
93+
94+
int main()
95+
{
96+
Complex x,y;
97+
string s1,s2;
98+
cin>>s1;
99+
cin>>s2;
100+
x.input(s1);
101+
y.input(s2);
102+
Complex z=x+y;
103+
cout<<z<<endl;
104+
return 0;
105+
}
106+
107+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /