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

C++ problem 1) Create an array to store 10 Point2D points (the Point2D class fro

ID: 3842495 • Letter: C

Question

C++ problem

1) Create an array to store 10 Point2D points (the Point2D class from A6). Set the coordinates from 0,0 to 9,9. Move all the points left 5 units and up 10 units. Print their new location.

2) 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.

Point2D class:

#include <iostream>
using namespace std;

class Point2D{

private:
int x, y;
public:
// default constructor
Point2D(){
x = 0;
y = 0;
}
// parameterized constructor
Point2D(int xp, int yp){
xp = x;
yp = y;
}

// setters and getters

int getX(){
return x;
}

int getY(){
return y;
}

void setX(int xp){
x = xp;
}

void setY(int yp){
y = yp;
}

void moveHorizontally(int xp){
x = x + xp;
}

void moveVertically(int yp){
y = y + yp;
}

void moveToOrigin(){
x = 0;
y = 0;
}

void printLocation(){
cout<<"Point at ("<<x<<", "<<y<<")"<<endl;
}
};


int main(){

// creating object
Point2D p1;

p1.printLocation();

p1.setX(3);
p1.printLocation();
p1.setY(-4);
p1.printLocation();
p1.moveVertically(2);
p1.printLocation();
return 0;
}

Explanation / Answer

This is how you can create array of Point2D

void doSomthing() {
   Point2D *arr = new Point2D[10]; //alocate memory
   for (int i = 0; i <=9; i++)
       arr[i] = Point2D(i,i);    //create object using parameterized constructor

for (int i = 0; i <=9; i++) {
       arr[i].moveHorizontally(-5);
       arr[i].moveVertically(10);
       arr[i].printLocation(); }
}

Class Point3D


class Point3D: public Point2D {
   private:
       int z;
   public:
       Point3D() {
           Point2D(); //call parent class default constructor
           z = 0;
       }

       Point3D(int xp, int yp, int zp) {
           Point2D(xp,yp); // call parameterized constuctor
           z = zp; //initialize z
       }
      
       int getZ() {
           return z;
       }

       void setZ(int zp) {
           z = zp;
       }

       void moveAlongZ(int zp) {
           z = z + zp;
       }

       void moveToOrigin(){
           Point2D::moveToOrigin(); //this will reset x and y
           z = 0; //reset z
       }

       void printLocation(){
                  cout<<"Point at ("<<getX()<<", "<<getY()<<","<<z<<")"<<endl; //NOTE that the use of getX and getY methods
        }
}

I tried my best to keep the code simple. I have also commented few parts of the code to make things easy for you. If you are still facing problem regarding the code, please feel free to comment below. I shall get back to you at the earliest.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote