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.
#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)
C++ Please help. 1) Create an array to store 10 Point2D points (the Point2D class from...
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...
java Generics Objectives: OOWorking with a Generic Class 1. Create a class called Node that is Generic. The class has one class attribute that is an element. It will need 2 Constructors, a setter and getter for the class attribute, and a toString method. 2. Write a Lab10Driver that will: Instantiate a Node Integer object with no value. I. Il. Ill. IV. a. Then call the set method to set the value to 5. Instantiate a Node String object with...
Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name. Add a constructor which expects color and name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of ...
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...
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...
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...
. 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...
Create a class called Flower. Add one member variables Color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name. Add a constructor which expects color and name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of...
Draw the UML DIAGRAM ALSO PLEASE DRAW THE UML DIAGRAM.ALSO in java should use the program in java For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class The Point3D class will have the...