|
|
Now with functions and classes do you put the whole function inside the class or just the prototype. |
You can do it either way. Generally, it's better to put the prototype in the class definition in the header, and then put the actual function definition in the .cpp file. This minimises the amount of recompiling you have to do it you change the implementation of the function. |
Your setPoint function doesn't make much sense. You pass in two values to the function, and then you overwrite them with values read in by the user. What is it you actually want this function to do? |
What do you mean, isn't the setPoint function getting the x, y from the user anyways. |
What is it you actually want this function to do? |
|
|
class Point { //here we're setting the and getting the x,y cords and naming the functions //used to get the distance, the double x, y are only used by the class Point. private: double x; double y; public: Point (const double = 0, const double = 0); void setPoint(double, double); double getXCoordinate(); double distance(const Point); }; //here we want the second point for the coordinates? Point::Point( const double aX, const double aY) { x = aX; y = aY; } //this will get the distance between the two points and get the return defined double Point::distance(const Point somePoint) { double dx = somePoint.x - x; double dy = somePoint.y - y; return sqrt( dx * dx + dy * dy); } //I'm not sure what this is for if you could clarify that's be a help //(or is this getting the third point )? double Point::getXCoordinate() { return x; } /////////////////////////////////////////////////////////////// ////////////////////////////////////////////////// //in the main we're setting the 2 points P1 and P2, and p3 is going to come in //play int main() { Point P1(1, 2); Point P2(4, 6); Point P3; // when you use the class in main you have to use the objects infront of the //functions always? std::cout << "X coordinate of point P3 = " << P3.getXCoordinate() << '\n'; std::cout << "X coordinate of point P2 = " << P2.getXCoordinate() << '\n'; std::cout << "Distance between P1 and P2 = " << P1.distance(P2) << '\n'; return 0; } |
Yeah, it's a start and not the complete deal. You need a getter for the Y coordinate, and a destructor. P3 is there just to show you how the default constructor works. line 16 etc is the constructor. What you need to do is sit down with a textbook/notes and read up on Classes. It won't take you long. |