Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef POINT_H
- #define POINT_H
- #include <iostream>
- using namespace std;
- template <class T = int>
- class Point
- {
- private:
- T x;
- T y;
- public:
- Point(); //default constructor
- Point(T, T); // overloaded constructor
- ~Point(); // destructor
- // setters
- void set_x(T);
- void set_y(T);
- // getters
- T get_x();
- T get_y();
- //overload operators
- template<class U>
- friend std::istream& operator>>(istream&, Point<U>&); // overload >>
- template<class U>
- friend std::ostream& operator<<(ostream&, Point<U>&); // overload <<
- };
- #endif
- #include "Point.h"
- #include <iostream>
- using namespace std;
- template<class T>
- Point<T>::Point() //default constructor
- {
- this->x = 0;
- this->y = 0;
- }
- template<class T>
- Point<T>::Point(T a, T b)
- {
- this->x = a;
- this->y = b;
- }
- template<class T>
- Point<T>::~Point()
- {
- // cout << "Destructor called" << endl;
- }
- template<class T>
- void Point<T>::set_x(T a)
- {
- this->x = a;
- }
- template<class T>
- void Point<T>::set_y(T b)
- {
- this->y = b;
- }
- template <class T>
- T Point<T>::get_x()
- {
- return x;
- }
- template <class T>
- T Point<T>::get_y()
- {
- return y;
- }
- template <class T>
- istream& Point<T>::operator>>(istream &in, Point<T> &p)
- {
- cout << "Please enter x and y coordinate (separated by a single space)";
- in >> p.x >> p.y;
- }
- template <class T>
- ostream& Point<T>::operator<<(ostream &out, Point<T> &p)
- {
- out << "(" << p.x << "," << p.y << ")"; //(x,y)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement