C++ programing Implement a class which models a game piece on a 2D grid. The dat
ID: 3686432 • Letter: C
Question
C++ programing
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 Test all methods in the main function. Implement a non-default constructor which accepts two parameters which will ultimately set x and y to an initial position Implement the methods up, down, left and right 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 Test all methods in the main functionExplanation / Answer
#include <iostream>
using namespace std;
class MyClass{
public:
double X;
double Y;
MyClass();
MyClass(double x,double y);
double getX();
double getY();
};
MyClass::MyClass(void){
X = 0.0;
Y = 0.0;
}
MyClass::MyClass(double x,double y){
X = x;
Y = y;
}
double MyClass::getX( )
{
return X;
}
double MyClass::getY()
{
return Y;
}
int main( )
{
MyClass l,lo(5,6);
cout<<"x and y values of default constructor: "<<l.getX()<<" "<<l.getY();
cout<<"x and y values of default constructor: "<<lo.getX()<<" "<<lo.getY();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.