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
  • f. Demonstration polymorphic behavior by declaring a Point2D pointer and separately assigning a Point2D object then...

    f. Demonstration polymorphic behavior by declaring a Point2D pointer and separately assigning a Point2D object then a Point3D object to the pointer and calling the distance function using the pointer. Which distance function will be called? Does your code show this?[5 pts) Continue with problem 1, create a new project then copy/paste the project in problem 1 in this new project 3. a. Add exception handling to the Point2D class as follows. Validate the values assigned to x and y...

  • . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should...

    . Q7. Write a C++ class called Point2D that describes a 2-dimensional (x,y) point. It should have the following features: • A constructor that accepts two double values (x,y) to create a point object. A default constructor. • A void print() method that prints out the points coordinates. • A void set(Point2D p) method that allows you to set the point using the given object of Point2D. • A Point2D midPoint(Point2D p1) method that returns a Point2D object that is...

  • In C++ Write a program that contains a BankAccount class. The BankAccount class should store the...

    In C++ Write a program that contains a BankAccount class. The BankAccount class should store the following attributes: account name account balance Make sure you use the correct access modifiers for the variables. Write get/set methods for all attributes. Write a constructor that takes two parameters. Write a default constructor. Write a method called Deposit(double) that adds the passed in amount to the balance. Write a method called Withdraw(double) that subtracts the passed in amount from the balance. Do not...

  • 1. Extending the answer from Python HW #7 for question #1, write the following python code...

    1. Extending the answer from Python HW #7 for question #1, write the following python code to expand on the Car class. Include the python code you developed when answering HW #7. Add class “getter” methods to return the values for model, color, and MPG Add class “setter” methods to change the existing values for model, color, and MPG. The value to be used in the methods will be passed as an input argument. Add class method that will calculate...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the c...

    C++ Question 1) Consider a class Point that models a 2-D point with x and y coordinates. Define the class point that should have the following Private data members x and y (of type int), with default values of 0 A constant ID of type int A private static integer data member named numOfPoints This data member should be o Incremented whenever a new point object is created. o Decremented whenever a point object is destructed. A default constructor An...

  • Hi all, I need help to do a project based on C++ smart pointers. I have...

    Hi all, I need help to do a project based on C++ smart pointers. I have to implement the class template my_unique_ptr which is a pointer management class template. Also, it's required to implement the following public methods: a. Default constructor that initializes the object to point to nullptr. b. Constructor that takes a pointer Type * as a parameter and sets the object to point there. We say that the newly created object takes ownership of the pointed memory....

  • Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students...

    Help with C++ please Create a class called ClassQuiz. ClassQuiz will maintain quiz grades for students in a class. The class has the following member variables: int numStudents // holds the number of students in the class double* grades // to point to a dynamically allocated array of grades The class has the following public functions: +default constructors //set numStudents = 0; and grades = nullptr; +parameterized constructors //accepts numStudents and creates an array with size = numStudents; +AcceptGrades //...

  • python code? 1. Create a class called Person that has 4 attributes, the name, the age,...

    python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...

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