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();
 }
User97693321
  • 3,336
  • 7
  • 45
  • 69
rasul1719435
  • 115
  • 4
  • 10
  • 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"??? – paulsm4 Oct 20 '12 at 05: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? – Shep Oct 20 '12 at 05:12
  • 1
    _"have very little guidance"_ There are excellent books providing guidance: http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – jogojapan Oct 20 '12 at 05:17
  • @Shep It can be done. Thats y i asked the question. – rasul1719435 Oct 20 '12 at 05: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... – paulsm4 Oct 20 '12 at 18:57
  • @paulsm4 I understand, 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 – rasul1719435 Oct 21 '12 at 09:48
  • @rasul1719435 - Are you saying that your "Point" consists of 4 "X" coordinates ... and no "Y" coordinates? That makes no sense. Or are you saying that you need 4 "int's" to accomodate one coordinate? In that case, you should probably have *two* classes: one for "Point", the other for "Coordinate". IMHO... – paulsm4 Oct 21 '12 at 17:04

2 Answers2

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.

Anton Guryanov
  • 12,109
  • 1
  • 15
  • 16
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 ...

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Yes. but i would like to use array – rasul1719435 Oct 20 '12 at 05:32
  • 1
    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 Oct 20 '12 at 07:26
  • @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 Oct 21 '12 at 09:49
  • @rasul1719435 OK I misunderstood you. – john Oct 21 '12 at 11:01