pass values between functions?

The Partridge Family were neither partridges nor a family. Discuss.
indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: pass values between functions?

Post by indus » January 22nd, 2013, 8:25 am

You declare an array of Planet objects and than go with the for loop

Code: Select all

planet* planArray = new planet[planetNum];  // in order to be able to do that you need 
                                                                        //  to have a default constructor defined
for (int i = 0; i < planetNum; i++)
{
      // you can use here setters the same way as the 
      // getters in the Star class 
      // just add void setXXXX methods to the Planet class 
      // for example
      int dist = ...;              // make calculations needed for the distance and than 
      planet[i]->setDistance(dist);
}
example setter method for distance. It will ofcourse work only if you have the int distance attribute

Code: Select all

void setDistance(int a)
{
     distance = a;
}
What you're doing now is declaring an array of planets with each iteration of the for loop and using different values for the array length.

Damus
Posts: 7
Joined: January 19th, 2013, 6:43 pm

Re: pass values between functions?

Post by Damus » January 22nd, 2013, 1:31 pm

You sir have made my day.

Everything is working smoothly, I did however have to make a change to what you suggested because yours wasn't working for me.

Code: Select all

planet[i]->setDistance(dist);
I am not sure what -> is but it did not work, I kept getting errors with it. So I went with...

Code: Select all

Planet* planArray = new Planet[planetNum];
	for ( int i = 1; i < planetNum + 1; i++ )
	{
		int distance = rand() % 99 + 1;
		planArray[i].M_distance = distance;	
		cout << "\nI am planet number " << i << " and my distance is " << planArray[i].M_distance << "\n";
	}
This works exactly how I want it to, so far. I need to put in many more lines to get multiple values for everything that a planet is, but it should be similar to this, only more extensive.

If there is anything I did do wrong however, or should be done different, ect.. please let me know.

Thanks a ton, I really appreciate it. I wish I knew about array of objects, would have saved me the last two days of face palming.

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: pass values between functions?

Post by indus » January 23rd, 2013, 8:14 am

Well glad I could help a bit.
Seems as if you have M_distance as a public attribute. That is considered to be bad practices. You want to keep your class elements private is what all the hardcore c++-ers will tell you. Thats why you'll eventually need access modifiers (get and set methods). Like this for example

Code: Select all

class Planet
{
private:
    int M_distance;
public:
    Planet(){}
    ~Planet(){}
    void setDistance(int value){ M_distance = value; }
    int getDistance(){ return M_distance; }
};

int main ()
{
   srand ( time(NULL) );
   Planet* planArray = new Planet[3];
   for ( int i = 1; i < 3 + 1; i++ )
   {
      int distance = rand() % 99 + 1;
      planArray[i].setDistance(distance);
      cout << "\nI am planet number " << i << " and my distance is " << planArray[i].getDistance() << "\n";
   }
   return 0;
}
This way you have more control over the logic you are trying to implement and you can make validations of the values you pass. If lets say you decide that you want to change the color of the planet when the distance changes, than you could add a line of code to the setter method for example setColor(blah) and the color will change automatically on each call of setDistance.
If you pass it a negative value now it is totally acceptable, but it does not make sense. In the setDistance you could do a check if (value < 0) dont set it but give a warning for example.
Last edited by indus on January 23rd, 2013, 3:40 pm, edited 1 time in total.

Damus
Posts: 7
Joined: January 19th, 2013, 6:43 pm

Re: pass values between functions?

Post by Damus » January 23rd, 2013, 2:41 pm

Code: Select all

public:
    Planet(){}
    ~Planet(){}
    void setDistance(int value){ M_distance = value; }
    int getDistance(){ return M_distance; }
};
I am not 100% that I am understanding how this part works so please clarify if I am incorrect...

I am setting two public fuctions in my Planet class. The void setDistance function is the one I use in order to set M_distance (which is private) to whatever I need it to be, such as a random number. Ok, I think that makes sense, and getDistance? It looks like it returns it from the Planet class at planArray.getDistance()?

I think I am understanding it now actually.

indus
Posts: 35
Joined: November 7th, 2012, 12:35 am

Re: pass values between functions?

Post by indus » January 23rd, 2013, 3:25 pm

Yes thats correct. The setMethod sets the value of a given private attribute and the getMethod gets the value of that attribute and returns it.
You might as well define just a getMethod without a setter and this way you`ll never be able to change the value of the attribute.

Code: Select all

class Planet
{
private:
    string name;
public:
    Planet(string n) : name(n) {}
    ~Planet(){}
    string getName(){ return name; }
};

int main ()
{
   Planet p("Earth");
   cout << "I am a planet named " << p.getName() << endl;
   return 0;
}

Post Reply