This is just the outline of the code I am trying.please help me !
void surfaceintensity(int xpos,int ypos ,int zpos)
{
x[1]=xpos;
x[2]=ypos;
x[3]=zpos;
}
Suppose I have an object t1 and I have sent values to the function surface intensity as:
t1.surfaceintensity(10,20,30)
If i do it above mentioned way then,will the values of
x[1]=10;
x[2]=20;
x[3]=30;
If not how can I assign these values to the array x[]?
-
What errors are you getting? What is the expected output? What is the actual output? What is the question?ciphermagi– ciphermagi2014年03月22日 15:52:35 +00:00Commented Mar 22, 2014 at 15:52
-
"I guess there is something wrong"... be concreteLihO– LihO2014年03月22日 15:52:49 +00:00Commented Mar 22, 2014 at 15:52
-
"I guess something is wrong" is not a very accurate description of a problem, and posting a huge block of code without even saying what it is supposed to do does not really help either.Baum mit Augen– Baum mit Augen ♦2014年03月22日 15:52:53 +00:00Commented Mar 22, 2014 at 15:52
-
1@deviantfan I suspect that the problem is the lack of a code beautifier / indentationuser2485710– user24857102014年03月22日 15:53:22 +00:00Commented Mar 22, 2014 at 15:53
-
The part where x[1]=xpos,x[2]=ypos is wrong I guess,couldnt come with correct solution for this problem,I am sorry for not being so good with my question.I hope you understandnovice1618– novice16182014年03月22日 16:03:39 +00:00Commented Mar 22, 2014 at 16:03
2 Answers 2
If I understand you right, I think our code does what you expect. However you should use array index 0..2 instead of 1..3!
Comments
The way I understand your question, you have a class (let's call it MyClass) which has a member function surfaceintensity(). This member function assigns some values to the elements of an array x which is also a member of your class.
You're unsure if assigning values to that array from inside the member function will actually change the array of the instance its called upon. If that is the case, then look at the following example (just copy/paste it, it should compile):
#include <iostream>
class MyClass
{
public:
MyClass()
{
x[0] = 0;
x[1] = 0;
x[2] = 0;
}
void surfaceintensity(int xpos,int ypos ,int zpos)
{
x[0]=xpos;
x[1]=ypos;
x[2]=zpos;
}
void print()
{
std::cout << x[0] << "/" << x[1] << "/" << x[2] << std::endl;
}
private:
int x[3];
};
int main()
{
MyClass t1;
t1.print();
t1.surfaceintensity(10,20,30);
t1.print();
return 0;
}
This will print
0/0/0
10/20/30
This demonstrates that the answer to your question is: yes, assigning values to member variables does change the internal state of the object.
I hope this was what you we're asking. If not, please edit your question and clarify.