I am getting an error saying that I can’t access private members x and y. How do I write the methods getX() and getY() so that they could see x and y? Thanks.
#include <iostream>
#include <string>
using namespace std;
class Point {
public:
Point(int x, int y);
Point();
int getX();
int getY();
private:
int x, y;
};
int Point::getX() {
return x;
}
int Point::getY() {
return y;
}
void main () {
Point p(5,5);
Point g;
cout << p.x << endl;
cout << g.y;
string s;
cin >> s;
}
Um, you already have written
getXandgetY, you just need to use them:Note that, because
getX()andgetY()don’t modify your class, they should beconst: