Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

class Point2D { public: /* * Default Constructor * Places point at (0, 0) */ Poi

ID: 3860306 • Letter: C

Question

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 ("<<x<<", "<<y<<")"<<endl;
}

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

Explanation / Answer

#include <iostream>

using namespace std;

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 setColor(string color)

{

this->color = color;

}

string getColor()

{

return color;

}

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 ("<<x<<", "<<y<<")"<<endl;

}

private:

int x;

int y;

string color;

};

class Point3D: public Point2D

{

private:

int z;

  

public:

/*

* Default Constructor

* Places point at (0, 0)

*/

Point3D():Point2D(0,0)

{

z= 0;

}

/*

* Constructor

* Places point at (xCoord, yCoord)

*/

Point3D(int xCoord, int yCoord,int zCoord) :Point2D(xCoord,yCoord)

{

z = zCoord;

}

int getZ() {

return z;

}

void setZ(int zCoord) {

z = zCoord;

}

void moveToOrigin()

{

setX(0);

setY(0);

z = 0;

}

/*

* Prints the current location of the point

*

*/

void printLocation() {

cout<<"Point located at ("<<getX()<<", "<<getY()<<" ,"<<z<<")"<<endl;

}

  

};

int main()

{

Point2D p1(7,8);

p1.setColor("Orange");

Point3D p2(4,5,6);

p2.setColor("Red"); // setColor() of Point2D class is accessible as Point3D inherits Point2D public

cout<<" P2 color : "<<p2.getColor();

cout<<endl;

p2.printLocation();

cout<<" using pointers to objects : ";

Point2D *ptr1;

ptr1 = &p1;

ptr1->moveHorizontally(4);

ptr1->printLocation();

Point3D *ptr2;

ptr2 = &p2;

ptr2->moveVertically(3);

ptr2->printLocation();

cout<<" Point a Point2D pointer to a Point3D object";

ptr1 = &p2;

cout<<endl;

ptr1->printLocation(); //coordinate z will be missing as pointer of class Point2D is used

return 0;

}

output: