I have a class i created below
class people
{
public:
int x;
int y;
int z;
void calculate();
};
people tony
tony.x=1;
tony.y=2;
tony.z=3;
people ed
ed.x=2;
ed.y=3;
ed.z=4;
i basically want this program to take the values assigned to tony and ed to calculate using the void calculate(); function. im wondering how to input the values assigned to edx, y, and z, and tonyx, y, and z into the function and have the function calculate x=y*z for both of them. my problem is that i am using variables that are different for both objects and dont quite understand how this would work.
also i know that the dot operator in ed.1 is not right can somebody kindly tell me how i would do that?
passing variables into a function and altering the orignal value
Just a suggestion: in your example, don't name a variable with a number. When you say 1 = 2 * 3, is that number 2 times number 3, or is it variable 2 times variable 3?
Anyway, do you mean to have the calculate function take a people parameter?
i.e.
Anyway, do you mean to have the calculate function take a people parameter?
i.e.
class people
{
public:
int x;
int y;
int z;
// I have no idea what it is you actually want to do with this function, so this is a shot in the dark
void calculate(people& p)
{
x = p.y * p.z;
p.x = y * z;
}
};
Alright,
Should I spare you the lecture about variable names, and classes?
You may want to do some research on classes.
If you want to update the classes, though.
You can pass a reference to Tony as a parameter.
So the prototype of the function would look something along the lines of:
So, what this says, basically is: "I want the pointer to the block of memory that holds information related to person".
You make your changes and voila.
A more interesting alternative (and correct when it comes to class, I believe) is inheritence and polymorphism.
Human is the base class, tony and ed are People. (Sorry for the name, can't think of anything better at the moment). The base class has a virtual method (notice I didn't call it a function) that People inherits from the Human class. Because the base class method is virtual, that means you most override the method to perform the appropriate action.
Because this is the only way you'll be using calculate, then you could just not declare the method as virtual and let the child class call the base class` method.
Should I spare you the lecture about variable names, and classes?
You may want to do some research on classes.
If you want to update the classes, though.
You can pass a reference to Tony as a parameter.
So the prototype of the function would look something along the lines of:
void calculate (People *person);
So, what this says, basically is: "I want the pointer to the block of memory that holds information related to person".
You make your changes and voila.
A more interesting alternative (and correct when it comes to class, I believe) is inheritence and polymorphism.
Human is the base class, tony and ed are People. (Sorry for the name, can't think of anything better at the moment). The base class has a virtual method (notice I didn't call it a function) that People inherits from the Human class. Because the base class method is virtual, that means you most override the method to perform the appropriate action.
Because this is the only way you'll be using calculate, then you could just not declare the method as virtual and let the child class call the base class` method.
why would i use inheritance? inheritance would just overcomplicate things. a class is a template, i just want the function to be reusable for each object and hold different results based on what the variables x, y, and z. (i see what you mean and changed the original post)
Alright,
Should I spare you the lecture about variable names, and classes?
You may want to do some research on classes.
If you want to update the classes, though.
You can pass a reference to Tony as a parameter.
So the prototype of the function would look something along the lines of:
void calculate (People *person);
So, what this says, basically is: "I want the pointer to the block of memory that holds information related to person".
You make your changes and voila.
A more interesting alternative (and correct when it comes to class, I believe) is inheritence and polymorphism.
Human is the base class, tony and ed are People. (Sorry for the name, can't think of anything better at the moment). The base class has a virtual method (notice I didn't call it a function) that People inherits from the Human class. Because the base class method is virtual, that means you most override the method to perform the appropriate action.
Because this is the only way you'll be using calculate, then you could just not declare the method as virtual and let the child class call the base class` method.
I found a way to do some of the work for it. What i did was i have a source file named pokemon.cpp and pokemon,h. i used references to pump variables into the calculate stat functions.
now i just have to wrok on creating the objects (pokemon) and find a way to have each pokemons base stats alter their stats calculation.
now i just have to wrok on creating the objects (pokemon) and find a way to have each pokemons base stats alter their stats calculation.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement