0

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[]?

9
  • What errors are you getting? What is the expected output? What is the actual output? What is the question? Commented Mar 22, 2014 at 15:52
  • "I guess there is something wrong"... be concrete Commented 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. Commented Mar 22, 2014 at 15:52
  • 1
    @deviantfan I suspect that the problem is the lack of a code beautifier / indentation Commented 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 understand Commented Mar 22, 2014 at 16:03

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

No, it will not work since you (and the original poster) are using index 1..3 instead of 0..2 in surfaceintensity(). Still I do not understand what the problem of the OP is :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.