Juscallmesteve
Juscallmesteve

Reputation: 157

Unable to change private variables using member functions

#include <iostream>
#include <string>

using namespace std;

struct Point {
private:
    int xCord,yCord;

public:
    void setX(int x);
    void setY(int y);
    int getX();
    int getY();
    int rotate(int x, int y, Point p1);
    int moveHorizontally(int x, int a, int b);
    int moveVertically(int y, int a, int b);    
};

int main() {
    Point p1;

    p1.setX(1);     //sets X
    p1.setY(2);     //sets Y

    cout << p1.getX() << ", " << p1.getY() << endl;  //prints current value of X & Y 

    p1.rotate(p1.getX(), p1.getY(), p1);
    cout << p1.getX() << ", " << p1.getY() << endl; 

    return 0;
}

void Point::setX(int newX) {
    xCord = newX;
}

void Point::setY(int newY) {
    yCord = newY;
}

int Point::getY() {       //This will just return the y Cord.
    return yCord;
}

int Point::getX() {      //This will just return the x Cord.
    return xCord;
}

int Point::moveHorizontally(int x, int tempX, int tempY) {
    //Move the point to the right if positive.
    //Move the point to the left if negative.
    int newX = tempX + (x);
    return newX;
}

int Point::moveVertically(int y, int tempX, int tempY) {
    //Move the point up if positive.
    //Move the point down if negative.
    int newY = tempY + (y);
    return newY;
}

int Point::rotate(int tempX, int tempY, Point p1){  
    //(1,2) -->> (-2,1)
    int tempX_DNC = tempX;
    int tempY_DNC = tempY;
    int quadrant;

    if((tempX > 0) && (tempY > 0)) {     //Quadrant 1: x(positive), y(positive)  Then rotates to Quad 2
        quadrant = 1;
        tempX = -(tempY);
        tempY = tempX_DNC;
    } else if ((tempX < 0) && (tempY > 0)) {      //Quadrant 2: x(negative), y(positive)  Then rotates to Quad 3
        quadrant = 2;
        tempX = -(tempY_DNC);
        tempY = tempX_DNC;
    } else if ((tempX < 0) && (tempY < 0)) {     //Quadrant 3: x(negative), y(negative)   Then rotates to Quad 4
        quadrant = 3;
        tempX = -(tempY_DNC);
        tempY = tempX_DNC;
    } else if ((tempX > 0) && (tempY < 0)) {     //Quadrant 4: x(positive), y(negative)  Then rotates to Quad 1
        quadrant = 4;
        tempX = -(tempY_DNC);
        tempY = tempX_DNC; 
    } else {
        quadrant = 0;
    }

    //This will rotate the points 90* to the left.
    //(1,2) will then become (-2,1)
    //I could have if in quadrant1, all are positive, if in quadrant 2 the x would be negative and y would be positive
    //If in quadrant 3 the x and y will both be negative, if in quadrant 4 the x would be positive and the y would be negative
    cout << tempX << ", " << tempY << endl;
    p1.setX(tempX);
    p1.setY(tempY);
    cout <<"x is: " <<p1.getX() <<endl;
    cout <<"Y is: " <<p1.getY() <<endl;
}

Code is above.

So I am creating a class Point. Point has 2 private variables xCord, yCord. I want to call the rotate function and have that be able to modify the xCord, yCord but it does not. I am not sure why. I tried passing the Point p1 to the function and to see if that would fix the issue but it did not, I also tried without passing the Point p1 and just having Point p1 inside the function definition.

p1.setX(VARIABLE); works when it is in main(). but not when I call p1.setX(VARIABLE) inside another member function.

Upvotes: 1

Views: 101

Answers (3)

robert
robert

Reputation: 3726

You are passing a copy of p1 to the rotate function. Only this copy is modified.

Upvotes: 2

Piet Jan
Piet Jan

Reputation: 11

void point::rotate() {
xcord = ycord;
ycord = xcord;
}

is all you need for basic rotation

Upvotes: -2

463035818_is_not_an_ai
463035818_is_not_an_ai

Reputation: 122830

You pass the point by value:

int rotate(int x, int y, Point p1);
                               ^^--------pass-by-value

ie. the p1 inside the function is a copy of p1 in main that gets deleted once the function returns. If you want to change the point that is passed as parameter inside the function then pass it by reference:

int rotate(int x, int y, Point& p1);
                            ^^--------pass-by-reference

PS: ... However, as rotate is a member function of Point you should probably rather rotate the instance on which you are calling it, change its signature to

int rotate(int x, int y);

and instead of changing the coordinates of some point passed as parameter do this:

 this->setX(tempX);  // this-> not really necessary, just added for clarity
 this->setY(tempY);

Alternatively you keep it as is and pass the point that is supposed to be rotated as parameter, but then you should consider making the method static.

PPS: If you want to change it to pass-by-reference, you have to change the signature in the class declaration to:

int rotate(int x, int y, Point& p1);

and the definition you have to change to:

int Point::rotate(int tempX, int tempY, Point& p1) { /*...*/ }

Upvotes: 1

Related Questions