We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
void call_complex(string& s, int& a, int& b) {
int num = 0;
int i = 1;
for (auto x : s) {
if (x == '+') {
continue;
}
else if (x == 'i') {
a = num;
num = 0;
i = 1;
continue;
}
num = num * i + (x - 48);
i = 10;
}
b = num;
}
int main() {
int a, b;
string s;
getline(cin, s);
call_complex(s, a, b);
Complex obj1{a, b};
getline(cin, s);
call_complex(s, a, b);
Complex obj2{a, b};
Complex obj3 = obj1 + obj2;
cout << obj3 << endl;
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Overload Operators
You are viewing a single comment's thread. Return to all comments →
include
include
using namespace std;
class Complex { public: int a, b; friend ostream& operator << (ostream& os, Complex obj); friend Complex operator + (Complex& a_obj, Complex& b_obj); };
ostream& operator << (ostream& os, Complex obj) { os << obj.a << "+i" << obj.b; return os; }
Complex operator + (Complex& a_obj, Complex& b_obj) { Complex c_obj; c_obj.a = a_obj.a + b_obj.a; c_obj.b = a_obj.b + b_obj.b; return c_obj; }
void call_complex(string& s, int& a, int& b) { int num = 0; int i = 1; for (auto x : s) { if (x == '+') { continue; } else if (x == 'i') { a = num; num = 0; i = 1; continue; } num = num * i + (x - 48); i = 10; } b = num; }
int main() {
}