fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. // complex.h
  5. namespace CN {
  6. class complex {
  7. public:
  8. complex(int num);
  9. friend std::string to_string(const complex &c);
  10. friend std::ostream& operator<<(std::ostream& out, const complex& o);
  11. friend std::istream& operator>>(std::istream& in, complex& o);
  12.  
  13. private:
  14. int i;
  15. };
  16. }
  17.  
  18. // complex.cpp
  19. // #include "complex.h"
  20.  
  21. namespace CN {
  22. complex::complex(int num) {
  23. i = num;
  24. }
  25.  
  26. std::string to_string(const complex &mc)
  27. {
  28. return std::to_string(mc.i);
  29. }
  30.  
  31. std::ostream& operator<<(std::ostream &out, const CN::complex &mc)
  32. {
  33. out << "CN::complex(" << to_string(mc) << ")";
  34. return out;
  35. }
  36.  
  37. std::istream& operator>>(std::istream &in, CN::complex &mc)
  38. {
  39. in >> mc.i;
  40. return in;
  41. }
  42. }
  43.  
  44. //
  45. int main() {
  46. CN::complex complex(111);
  47. std::cin >> complex;
  48. std::cout << complex << std::endl;
  49. std::cout << to_string(complex) << std::endl;
  50. }
Success #stdin #stdout 0s 4236KB
stdin
222
stdout
CN::complex(222)
222