Arsylk

Narkoman Distribution

Apr 11th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <sstream>
  4.  
  5. using namespace std;
  6.  
  7. class Point {
  8.   private:
  9.     int x, y;
  10.  
  11.   //constructors
  12.   public:
  13.     Point() {
  14.       this->x = 0;
  15.       this->y = 0;
  16.     }
  17.     Point(int x, int y) {
  18.       this->x = x;
  19.       this->y = y;
  20.     }
  21.  
  22.   //getters & setters
  23.   public:
  24.     int getX() {
  25.       return x;
  26.     }
  27.     int getY() {
  28.       return y;
  29.     }
  30.  
  31.     void setX(int x) {
  32.       this->x = x;
  33.     }
  34.     void setY(int y) {
  35.       this->y = y;
  36.     }
  37.  
  38.   //methods
  39.   public:
  40.     string toString() {
  41.       stringstream ss;
  42.       ss << "X: " << x << "\n";
  43.       ss << "Y: " << y << "\n";
  44.       return ss.str();
  45.     }
  46.     float lengthFromRef() {
  47.       return hypot((x - Point::refPoint.getX()), (y - Point::refPoint.getY()));
  48.     }
  49.   /* static */
  50.   public:
  51.     static Point refPoint;
  52.     static void setRefPoint(int x, int y) {
  53.       refPoint.setX(x);
  54.       refPoint.setY(y);
  55.     }
  56. };
  57. Point Point::refPoint(0, 0);
  58.  
  59.  
  60. int main() {
  61.   Point point(3, 4);
  62.   cout << Point::refPoint.toString();
  63.   cout << "Length: " << point.lengthFromRef() <<endl;
  64.   Point::setRefPoint(9, -4);
  65.   cout << Point::refPoint.toString();
  66.   cout << "Length: " << point.lengthFromRef() <<endl;
  67.  
  68.   return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment