DEFINE A CLASS CALLED Point: 1. There are two integer private data memebers call
ID: 3553511 • Letter: D
Question
DEFINE A CLASS CALLED Point:
1. There are two integer private data memebers called x and y;
2 There are 6 memeber functions:
a. constructor to set the values of x and y. Please set defalut values to 0
b. void setX(int) to set the data member x
c.void setY(int) to set the data member y
d.int getX() to get the x value of a point
e. int getY() to get the y value of a point
f. void pirnt() to display the coordinate of a point in the fromat of (x,y)
3. use the following codes to test your program
int main()
{
Point pointA(72,115), point B;
cout << "PointA X=" << pointA.getX()<< " PointA Y=" << pointA.getY()<< endl;
cout<< "PointB X=" << pointB.getX()<< " PointB Y=" << pointB.getY()<< endl;
pointB.setx(10);
pointB.sety(20);
pointB.print();
return 0;
}
Explanation / Answer
#include <iostream>
using namespace std;
class Point
{
private:
int x,y;
public:
Point(int _x=0,int _y=0) : x(_x),y(_y) {}
void setX(int _x)
{
x=_x;
}
void setY(int _y)
{
y=_y;
}
int getX()
{
return x;
}
int getY()
{
return y;
}
void print()
{
cout<<" X: "<<x<<" Y : "<<y<<endl;
}
};
int main()
{
Point pointA(72,115), pointB;
cout << "PointA X=" << pointA.getX()<< " PointA Y=" << pointA.getY()<< endl;
cout<< "PointB X=" << pointB.getX()<< " PointB Y=" << pointB.getY()<< endl;
pointB.setX(10);
pointB.setY(20);
pointB.print();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.