classes/point1.cc - Class Point i C++ together with a main function - member functions defined in class definition. | Lecture 4 - slide 2 : 40 Program 2 |
// A simple C++ class - in transition from the C# version. #include <cmath> #include <iostream> using namespace std; class Point { private: double x, y; public: Point(double x_coord, double y_coord): x(x_coord), y(y_coord) { } Point(): x(0.0), y(0.0) { } double getx (){ return x; } double gety (){ return y; } void move(double dx, double dy){ x += dx; y += dy; } double 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; }