Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4.  
  5. struct Complex {
  6.  
  7. int real, imag;
  8. Complex(int r = 0, int i =0) {real = r; imag = i;}
  9.  
  10. Complex operator + (Complex const &obj) {
  11.  
  12. // Note that the operator on left is passed as a refrence and can be modified directly by modifying *this.
  13.  
  14. Complex res;
  15. res.real = real + obj.real;
  16. res.imag = imag + obj.imag;
  17. return res;
  18. }
  19. void print() { cout << real << " + i" << imag << endl; }
  20. };
  21.  
  22. int main()
  23. {
  24. Complex c1(10, 5), c2(2, 4);
  25. Complex c3 = c1 + c2;
  26. c3.print();
  27. c1.print();
  28. c2.print();
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement