1

I would like to know, how to loop through a array of int to get its value and set its value. I know how to use the for loop to to get instantly, but I am not sure how it works, when I am using in user created objects and esp using the get set method.

I am totally new to this and have very little guidance from my lectures. I hope you guys can assist to help me. This up to where I have done.

//point.h

class point {
private:
    int x[4];

public:
    int getx();
    void setx();
};  

//point.cpp

class point {
   point::getx(){
      // ??????
   }

   point::setx(){
      // ???????
   }

//main.cpp

 int main(){
     point objPoint;
     objPoint.setx(/* ???? */);
     ???? = objPoint.getx();
 }
7
  • Q: Why do you have two different definitions of class "point" (or "why are you re-declaring "class point" when you're defining the implementations of getx() and setx()"? Q: Why doesn't "setx()" have an argument (the value you want to set x to)? Q: Why "int x[4]"? Q: What does any of this have to do with a "loop"??? Commented Oct 20, 2012 at 5:10
  • It seems like the flaw here is logical as much as anything. How will getx() know which of the 4 values to return? How will setx() know which value to set, and what will it set it too? Commented Oct 20, 2012 at 5:12
  • 1
    "have very little guidance" There are excellent books providing guidance: stackoverflow.com/questions/388242/… Commented Oct 20, 2012 at 5:17
  • @Shep It can be done. Thats y i asked the question. Commented Oct 20, 2012 at 5:43
  • @rasul1719435 - what John said below is absolutely correct: "An array of Points works like this, Point pointArray[4];. You still write your Point class for a single point, then you declare an array of Points in main. You don't put the array inside the Point class." IMHO... Commented Oct 20, 2012 at 18:57

2 Answers 2

2

First of all, your getx method should return int*, not just int, and your setx should receive const int* as parameter. Second, in your point.cpp file you shouldn't redeclare class point.

int* point::getx() { //version with copying
    int* ans = new int[4];
    for (int i = 0; i < 4; i++) {
        ans[i] = x[i];
    }
    return ans;
}

void point::setx(const int* y) {
    for (int i = 0; i < 4; i++) {
        x[i] = y[i];
    }
}

Then you can use them like this

int y[4] = {1, 2, 3, 4};
int* z;
objPoint.setx(y);
z = objPoint.getx();

Just don't forget to delete[] z when you're done.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, right. delete z will also work in this case, but don't do that) My mistake, corrected answer
2

If I'm understanding you correctly, you probably want something like more this:

point.h:

class Point{
private:
  int x, y;
public:
  int getx();
  int gety();
  void setx(int value);
  void sety(int value);
};  

point.cpp

int Point::getx() { return x; }
int Point::gety() { return y; }
void Point::setx(int value) { x = value; }
void Point::sety(int value) { x = value; }

main.cpp

int main(int argc, char *argv[])
{
  Point objPoint;
  objPoint.setx(1);
  int x = objPoint.getx();
  cout << "x=" << x << endl;
  return 0
}

Even better, you might wish to define a constructor like Point (int xvalue, int yvalue).

IMHO ...

4 Comments

Yes. but i would like to use array
An array of Points works like this, Point pointArray[4];. You still write your Point class for a single point, then you declare an array of Points in main. You don't put the array inside the Point class.
@john U see i need to create a array of point class which should take in 4 sets of x per object. So if i create 5 sets of point objects, each object should have 4 sets of x each. Thats y i need to implement the x as array. If i dont create arrays of x than how can impelement this?
@rasul1719435 OK I misunderstood you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.