Question

C++ Please help. 1) Create an array to store 10 Point2D points (the Point2D class from...

C++

Please help.

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.

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
using namespace std;

class Point2D
{
private:
int x,y;
string color;
  
public:
Point2D()
{
x = 0;
y = 0;
}
Point2D(int x,int y)
{
this->x = x;
this->y = y;
}
void setX(int x)
{
this->x = x;
}
void setY(int y)
{
this->y = y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
void setColor(string color)
{
this->color = color;
}
string getColor()
{
return color;
}
void move()
{
this->x = this->x -5;
this->y = this->y+10;
}
void moveToOrigin()
{
x = 0;
y = 0;
}
void print()
{
cout<<"\n("<<x<<","<<y<<")";
}
  
};
class Point3D : public Point2D
{
private:
int z;
  
public:
Point3D():Point2D(0,0)
{
z= 0;
}
Point3D(int x,int y,int z):Point2D(x,y)
{
this->z = z;
  
}
int getZ()
{
return z;
}
void setZ(int z)
{
this->z = z;
}

void moveAlongZ(int z)
{
this->z = this->z +z;
}
void moveToOrigin()
{
setX(0);
setY(0);
setZ(0);
}
void print()
{
cout<<"\n("<<getX()<<","<<getY()<<","<<z<<")";
}
  
};
int main()
{
int i;
Point2D p[10];//array of 10 Point2D Points
//Set the coordinates from 0,0 to 9,9.
for(i=0;i<10;i++)
{
p[i].setX(i);
p[i].setY(i);
}

//Move all the points left 5 units and up 10 units. Print their new location.
for(i=0;i<10;i++)
{
p[i].print();
p[i].move();
p[i].print();
  
}
cout<<endl;
Point2D p1;
p1.setColor("blue");
p1.print();
cout<<p1.getColor();

Point3D p2(3,4,5);
p2.setColor("red");//setColor behavior available for a Point3D class as it derives all functions
p2.print();
cout<<p2.getColor();

//pointer to Point2D object is used to move and print location
Point2D *ptr1 = &p1;
ptr1->move();
ptr1->print();
  
//pointer to Point2D object is used to move and print location
Point3D *ptr2 = &p2;
ptr2->moveAlongZ(4);
ptr2->print();
  
  


return 0;
}

Output:


(0,0)
(-5,10)
(1,1)
(-4,11)
(2,2)
(-3,12)
(3,3)
(-2,13)
(4,4)
(-1,14)
(5,5)
(0,15)
(6,6)
(1,16)
(7,7)
(2,17)
(8,8)
(3,18)
(9,9)
(4,19)

(0,0)blue
(3,4,5)red
(-5,10)
(3,4,9)

Add a comment
Know the answer?
Add Answer to:
C++ Please help. 1) Create an array to store 10 Point2D points (the Point2D class from...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT