C ++ Assignment Please help 1) Create an array to store 10 Point2D points (the P
ID: 3841649 • Letter: C
Question
C ++ Assignment
Please help
1) Create an array to store 10 Point2D points (the Point2D class from below). Set the coordinates from 0,0 to 9,9. Move all the points left 5 units and up 10 units. Print their new location.
Here is code for Point2D class :
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;
};
...
Point2D cp1;
cp1.printLocation();
Point2D cp2(2, 3);
cp2.printLocation();
cp1.setX(5);
cp1.setY(3);
cp1.printLocation();
cp1.moveHorizontally(10);
cp1.moveVertically(-10);
cp1.printLocation();
cp1.moveToOrigin();
cp1.printLocation();
cp2.moveHorizontally(8);
cp2.moveVertically(7);
cp2.printLocation();
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.
3) 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 color and try to set its color. Is the setColor behavior available for a Point3D class? Why or why not?
4) 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.
5) 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?
6) Modify the member functions so that exercise 5 behaves as expected for a Point3D
Explanation / Answer
class Point2D //Adding new member color (only relevant code is shown)
{
private:
string color;
public:
string getColor()
{
return color;
}
void setColor(string value)
{
color = value;
}
}
class Point3D : public Point2D
{
private:
int z;
public:
Point3D(){
z = 0;
}
Point3D(int xCoord, int yCoord, int zCoord){
x = xCoord;
y - yCoord;
z = zCoord;
}
int getZ()
{
rerurn z;
}
void setZ(int a)
{
z = a;
}
void moveAlongZ(int distance)
{
z = z + distance;
}
void moveToOrigin()
{
x = 0;
y = 0;
z = 0;
}
void printLocation() {
cout<<"Point located at ("<<x<<", "<<y<< "," << z << ")"<<endl;
}
}
.....
Point2D cp2;
Point3D cp3;
Point2D *ptr2D;
Point3D *ptr3D;
cp2.setColor("Blue");
cp3.setColor("Red");
cp3.printLocation();
cp3.setZ(4);
cp3.moveToOrigin();
cp3.printLocation();
cp3.moveAlongZ(12);
cp3.printLocation();
ptr2D = &cp2;
ptr3D = &cp3;
ptr2D->moveToOrigin();
ptr2D->printLocation();
ptr3D->moveToOrigin();
ptr3D->printLocation();
5.ptr2D = &cp3; //Yes it is OK
ptr2D->printLocation() //This will print only x and y. No this is not expected for 3D. Since ptr2D is of type Point2D, so it will call printLocation() method of Point2D, hence printing x and y.
6. We need to make the functions of Point2D as virtual.for e.x "void printLocation" declaration will become "virtual void printLocation". Now if we call ptr2D->printLocation(), it will call printVirtual() function of Point3D as this ptr is holding the address of Point3D.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.