Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.37 KB | None | 0 0
  1. /******************************************************************************
  2.  
  3. Online C++ Compiler.
  4. Code, Compile, Run and Debug C++ program online.
  5. Write your code in this editor and press "Run" button to compile and execute it.
  6.  
  7. *******************************************************************************/
  8.  
  9. class MyReal {
  10. public:
  11. float x;
  12. float y;
  13.  
  14. public:
  15. MyReal(): x(0), y(0) { }
  16. MyReal(const float z) {
  17. this->x = z;
  18. this->y = z;
  19. }
  20. MyReal(const float x, const float y) {
  21. this->x = x;
  22. this->y = y;
  23. }
  24.  
  25. // Способ 1
  26. public:
  27. MyReal operator+(const MyReal &rhs) const {
  28. return MyReal(rhs.x + y, rhs.x - y);
  29. }
  30.  
  31. MyReal operator+() const {
  32. return MyReal(x + y);
  33. }
  34.  
  35. // Способ 2
  36. public:
  37. friend MyReal operator+(const MyReal &r) {
  38. return MyReal(r.x + r.y);
  39. }
  40.  
  41. friend MyReal operator+(const MyReal &lhs, const MyReal &rhs) const {
  42. return MyReal(lhs.x + rhs.y, lhs.x - rhs.y);
  43. }
  44. };
  45.  
  46. #include <iostream>
  47.  
  48. using namespace std;
  49.  
  50. int main()
  51. {
  52. MyReal x(2, 5);
  53. MyReal y(2, 5);
  54.  
  55. x+y;
  56.  
  57. cout<<x.x;
  58.  
  59. return 0;
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement