Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <sstream>
- using namespace std;
- class Point {
- private:
- int x, y;
- //constructors
- public:
- Point() {
- this->x = 0;
- this->y = 0;
- }
- Point(int x, int y) {
- this->x = x;
- this->y = y;
- }
- //getters & setters
- public:
- int getX() {
- return x;
- }
- int getY() {
- return y;
- }
- void setX(int x) {
- this->x = x;
- }
- void setY(int y) {
- this->y = y;
- }
- //methods
- public:
- string toString() {
- stringstream ss;
- ss << "X: " << x << "\n";
- ss << "Y: " << y << "\n";
- return ss.str();
- }
- float lengthFromRef() {
- return hypot((x - Point::refPoint.getX()), (y - Point::refPoint.getY()));
- }
- /* static */
- public:
- static Point refPoint;
- static void setRefPoint(int x, int y) {
- refPoint.setX(x);
- refPoint.setY(y);
- }
- };
- Point Point::refPoint(0, 0);
- int main() {
- Point point(3, 4);
- cout << Point::refPoint.toString();
- cout << "Length: " << point.lengthFromRef() <<endl;
- Point::setRefPoint(9, -4);
- cout << Point::refPoint.toString();
- cout << "Length: " << point.lengthFromRef() <<endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment