Question: class Point2D { public: /* * Default Constructor * Places point at (0, 0) */ Point2D() { x = 0; y = 0; } /*
class Point2D { public: /* * Default Constructor * Places point at (0, 0) */ Point2D() { x = 0; y = 0; }
/* * Constructor * Places point at (xCoord, yCoord) */ Point2D(int xCoord, int yCoord) { x = xCoord; y = yCoord; }
int getX() { return x; }
int getY() { return y; }
void setX(int xCoord) { x = xCoord; }
void setY(int yCoord) { y = yCoord; }
void moveHorizontally(int distance) { x+=distance; }
void moveVertically(int distance) { y+=distance; }
void moveToOrigin() { x = y = 0; }
/* * Prints the current location of the point * */ void printLocation() { cout<<"Point located at ("< private: int x; int y; }; double dist(const Point2D& p1, const Point2D& p2) { return sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2)); } Part 1: 1) Make a new class Point3D which inherits from the Point2D class. The class has one more data member for the z coordinate. There are two constructors, a default one and one with 3 coordinates. Add the new member functions getZ, setZ and moveAlongZ. Override moveToOrigin and printLocation to do the right thing for all 3 coordinates. Make a few Point3D objects and exercise their functionality. 2) Add a new data member, string color to your Point2D class and a new getter and setter function for color. Create a Point2D object and set its color. Then create a Point3D object and try to set its color. Is the setColor behavior available for a Point3D class? Why or why not? 3) Make a Point2D* pointer variable and point it to a newly created Point2D object. Make a Point3D* pointer variable and point it to a newly created Point3D object. Use the pointer variables to move the points and print their locations. 4) Point a Point2D pointer to a Point3D object. Does this work? Why or why not? Use the Point2D pointer to print the location of the Point3D object. What does it print? Is that what you expected for the location of a 3D point? 5) Modify the member functions so that exercise 5 behaves as expected for a Point3D
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
