Advertisement
Anon017706349

point

Apr 16th, 2019
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #ifndef POINT_H
  2. #define POINT_H
  3.  
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. template <class T = int>
  9. class Point
  10. {
  11. private:
  12. T x;
  13. T y;
  14.  
  15. public:
  16. Point(); //default constructor
  17. Point(T, T); // overloaded constructor
  18. ~Point(); // destructor
  19.  
  20. // setters
  21. void set_x(T);
  22. void set_y(T);
  23.  
  24. // getters
  25. T get_x();
  26. T get_y();
  27.  
  28. //overload operators
  29. template<class U>
  30. friend std::istream& operator>>(istream&, Point<U>&); // overload >>
  31. template<class U>
  32. friend std::ostream& operator<<(ostream&, Point<U>&); // overload <<
  33.  
  34. };
  35.  
  36. #endif
  37.  
  38.  
  39. #include "Point.h"
  40. #include <iostream>
  41.  
  42. using namespace std;
  43.  
  44. template<class T>
  45. Point<T>::Point() //default constructor
  46. {
  47. this->x = 0;
  48. this->y = 0;
  49. }
  50.  
  51. template<class T>
  52. Point<T>::Point(T a, T b)
  53. {
  54. this->x = a;
  55. this->y = b;
  56. }
  57.  
  58. template<class T>
  59. Point<T>::~Point()
  60. {
  61. // cout << "Destructor called" << endl;
  62. }
  63.  
  64. template<class T>
  65. void Point<T>::set_x(T a)
  66. {
  67. this->x = a;
  68. }
  69.  
  70. template<class T>
  71. void Point<T>::set_y(T b)
  72. {
  73. this->y = b;
  74. }
  75.  
  76. template <class T>
  77. T Point<T>::get_x()
  78. {
  79. return x;
  80. }
  81.  
  82. template <class T>
  83. T Point<T>::get_y()
  84. {
  85. return y;
  86. }
  87.  
  88. template <class T>
  89. istream& Point<T>::operator>>(istream &in, Point<T> &p)
  90. {
  91. cout << "Please enter x and y coordinate (separated by a single space)";
  92. in >> p.x >> p.y;
  93. }
  94.  
  95. template <class T>
  96. ostream& Point<T>::operator<<(ostream &out, Point<T> &p)
  97. {
  98. out << "(" << p.x << "," << p.y << ")"; //(x,y)
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement