fork download
  1. #include <iostream>
  2. #include <cassert>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <algorithm>
  6.  
  7.  
  8. typedef struct complex
  9. {
  10. complex(double re, double im)
  11. : re(re), im(im) {}
  12. void swap(complex & other);
  13. complex & operator=(complex const& other);
  14. void print();
  15. private:
  16. double re;
  17. double im;
  18. } complex;
  19.  
  20. void complex::swap(complex & other)
  21. {
  22. using std::swap;
  23. swap(re, other.re);
  24. swap(im, other.im);
  25. }
  26.  
  27. complex & complex::operator=(complex const& other)
  28. {
  29. if (this != &other)
  30. complex(other).swap(*this);
  31. return *this;
  32. }
  33.  
  34. void complex::print()
  35. {
  36. std::cout << re << ", " << im << std::endl;
  37. }
  38.  
  39. int main()
  40. {
  41. complex x(0, 1);
  42. complex y(0, -1);
  43.  
  44. x.print();
  45. y.print();
  46.  
  47. x = y;
  48. x.print();
  49. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
0, 1
0, -1
0, -1