Implement a class which models a game piece on a 2D grid. The data members will
ID: 3692615 • Letter: I
Question
Implement a class which models a game piece on a 2D grid. The data members will be x and y which will be private and of type integer. Implement a default constructor setting x and y to 0. Implement a method which returns the value of x Implement a method which returns the value of y Implement a non-default constructor which accepts two parameters which will ultimately set x and y to an initial position Implement the methods up to modify the position of the game piece Implement a method which calculates the distance of the current game piece to another game piece passed into the method Distance formula: d = Squareroot(x1 - x2)^2 + (y1 - y2)^2 Test all methods in the main functionExplanation / Answer
implementing in c++
#include<iostream.h>
#include<conio.h>
#include<cmath.h>
class Model2DGrid
{
private:
int x,y;
public:
Model2DGrid()
{
x=0;
y=0;
}
public int valueofx()
{
return x;
}
public int valueofy()
{
return y;
}
Model2DGrid(int initial_x, int initial_y)
{
x= initial_x;
y=initial_y;
}
public void modifyPositon(int new_x, new_y)
{
x=new_x;
y=new_y;
cout<<"New vale of x is "<<x;
cout<<"New value of y is "<<y;
}
public void calculateDistance(int x1, int x2, int y1, int y2)
{
int d;
x1=x1-x2;
y1=y1-y2;
x1=x1*x2;
y1=y1*y2;
x1=x1+y1;
d=sqrt(x1);
cout<<"Distance is "<<d;
}
};
int main()
{
clrscr();
int x,y;
Model2DGrid mdl ;
x=mdl.valueofx();
y=mdl.valueofy();
Model2DGrid mdl2(10, 20) ; //pass the initial vales of x and y
mdl.modifyPositon(50,50);
mdl.calculateDistance(10,20,40,60);
getch();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.