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[]
?
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.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments