#include<iostream>
#include<cstdlib>
usingnamespace std;
class point {
private:
int x, y;
public:
point(int x1, int y1);
int getX();
int getY();
void print();
point (const point & pt)
{ x = pt.x; y = pt.y; }
};
point::point(int x1, int y1) {
x = x1;
y = y1;
}
int point::getX() {
return x;
}
int point::getY() {
return y; }
void point::print() {
cout << "(" << x << ", " << y << ") " << endl;
}
int main(){
point a(3, 2);
point b(1,1);
a.getX();
a.getY();
b.getX();
b.getY();
cout<<a.getX()<<endl<<a.getY()<<endl<<b.getX()<<endl<<b.getY()<<endl;
b.print();
a.print();
cout<<endl;
point d(a);
d.print();
return 0;
}
Do I HAVE to declare a new object with new values or is it possible just to change values in the same object?
Many thanks!
you can declare a public member function so called mutator which essentially provides way to change private members of a class ( can also be used in validation )
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
class point
{
private :
int x, y
public :
//...
void setX( int x ) { this->x = x; }
// ...
};
int main()
{
point p;
p.setX( 100 );
}