![]() ![]() | structs/point2.cc - Struct Point in C++ . | Lecture 4 - slide 3 : 41 Program 1 |
// struct Point is almost identical with class Point. Not necessarily a typical struct. #include <cmath> #include <iostream> using namespace std; struct Point { private: double x, y; public: Point(double, double); Point(); double getx () const; double gety () const; void move(double, double); double distance_to(Point); }; Point::Point(double x_coord, double y_coord): x(x_coord), y(y_coord){ } Point::Point(): x(0.0), y(0.0){ } double Point::getx () const{ return x; } double Point::gety () const{ return y; } void Point::move(double dx, double dy){ x += dx; y += dy; } double Point::distance_to(Point p){ return sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); } int main(){ Point p, q = Point(3.0, 4.0), r(3.0, 4.0); p.move(2,3); q.move(4,5); double d = q.distance_to(r); cout << d << endl; cout << "Point p: " << p.getx() << ", " << p.gety() << endl; cout << "Point q: " << q.getx() << ", " << q.gety() << endl; }