I need to write the code for a Point class. However, there will be three files i
ID: 3918078 • Letter: I
Question
I need to write the code for a Point class. However, there will be three files involved, a header file Point.h, a Point.cpp and a PointTest.cpp. The Point class has eight members – two data fields (x and y) which should contain doubles, two constructors, a default constructor and a constructor with two parameters, and four functions, two accessor functions and two mutator functions. Members of the point class will be declared Public and Private as appropriate. The Point class will consist of a definition and an implementation file. Finally, the PointTest.cpp file will allow the user to instantiate two Point objects with user-selected values for x and y. The program will then calculate the distance between the two points.
Explanation / Answer
Point.h: --------- #ifndef _POINT_H_ #define _POINT_H_ class Point { private: double x; double y; public: Point(); Point(double x, double y); void setX(double x); void setY(double y); double getX(); double getY(); }; #endif Point.cpp: ---------- #include "Point.h" Point::Point() { this->x = 0; this->y = 0; } Point::Point(double x, double y) { this->x = x; this->y = y; } void Point::setX(double x) { this->x = x; } void Point::setY(double y) { this->y = y; } double Point::getX() { return x; } double Point::getY() { return y; } PointTest.cpp: --------------- #include #include #include "Point.h" using namespace std; double distance(Point &p1, Point &p2) { return sqrt(pow(p1.getX()-p2.getX(), 2) + pow(p1.getY()-p2.getY(), 2)); } int main() { double x, y; Point p1, p2; cout > x; cout > y; p1.setX(x); p1.setY(y); cout > x; cout > y; p2.setX(x); p2.setY(y); coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.