The purpose of this exercise is to effectively demonstrate t

The purpose of this exercise is to effectively demonstrate, that if an external code receives the address of an instance of a class, it can change the state of the object of that class, even it is against the intentions of the programmer who implemented the class. In order to prevent such an intrusion, accessors of reference type fields should not return the field itself, but rather a copy of it.

1. The ArrayPractice class has two private data fields, both are arrays. Declare the fields as

-- an array to store integer numbers of type int named numbers

-- an array to store Rectangle references (type is Rectangle) named boxes

\"Do not instantiate the fields at the declaration!\"

2. Each field has an associated accessor method as usual

3. There is a second accessor method associated to boxes named as getCopyOfBoxes, see the definition later.

4. Define a method named loadArrays( ). The method is void and takes no parameter. The method assigns the entries of the integer array and those of the Rectangle array such that for each array it

--runs a for loop to the length of the array

--entries of the int array assigned a random integer between 1 and 100

--each entry of the Rectangle array is assigned a new Rectangle object with random integer length and width values. Select the integers again between 1 and 100. Note that the individual Rectangle objects do not have names.

5. The class has a default constructor and an initializer constructor such that it takes one parameter, an int type value named size. The constructor

-instantiates the fields numbers and boxes such that the array length is                 size for both

-calls the loadArray() method

6. Define a method named copyNumbers( ). The method takes no parameter and returns an array integers, a copy of the numbers field. In the method declare and instantiate an array (a local variable) named say, temp, with the same type and to the same length as those of the field. The method runs a for loop to load the temp array such that it assigns each temp entry the corresponding numbers entry index-by-index. The method returns temp.

7. Define a method named copyBoxes( ). This method is entirely analogous to the previous method copyNumbers except in the for loop of we do NOT assign boxes[k] to temp[k], but rather a new Rectangle which is a copy of the boxes[k]. This copy must be instantiated by the copy constructor you added to the Rectangle class in Exercise 1 above. That is, the value assigned to temp[k] will be new Rectangle(boxes[k])

8. Complete the definition of the second accessor method getCopyOfBoxes() mentioned above so as to return copyBoxes( ) rather than boxes itself.

Solution

The following is a simple class that holds an integer and provides a constructor and access functions. Note that no destructor is needed because C++ can clean up integer member variables for us.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

class Simple

{

private:

    int m_id;

public:

    Simple(int id)

    {

        setID(id);

    }

    void setID(int id) { m_id = id; }

    int getID() { return m_id; }

};

Here’s a sample program that uses this class:

1

2

3

4

5

6

7

8

int main()

{

    Simple simple(1);

    simple.setID(2);

    std::cout << simple.getID() << \'\ \';

    return 0;

}

As you would expect, this program produces the result:

Somehow, when we call simple.setID(2);, C++ knows that function setID() should operate on object simple, and that m_id actually refers to simple.m_id. Let’s examine the mechanics behind how this works.

The hidden *this pointer

Take a look at the following line of code from the example above:

1

    simple.setID(2);

Although the call to function setID() looks like it only has one argument, it actually has two! When compiled, the compiler converts simple.setID(2); into the following:

1

    setID(&simple, 2); // note that simple has been changed from an object prefix to a function argument!

Note that this is now just a standard function call, and the object simple (which was formerly an object prefix) is now passed by address as an argument to the function.

But that’s only half of the answer. Since the function call now has an added argument, the member function definition needs to be modified to accept (and use) this argument as a parameter. Consequently, the following member function:

1

    void setID(int id) { m_id = id; }

is converted by the compiler into:

1

    void setID(Simple* const this, int id) { this->m_id = id; }

When the compiler compiles a normal member function, it implicitly adds a new parameter to the function named “this”. The this pointer is a hidden const pointer that holds the address of the object the member function was called on.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

class Simple

{

private:

    int m_id;

public:

    Simple(int id)

    {

        setID(id);

    }

    void setID(int id) { m_id = id; }

    int getID() { return m_id; }

};

The purpose of this exercise is to effectively demonstrate, that if an external code receives the address of an instance of a class, it can change the state of
The purpose of this exercise is to effectively demonstrate, that if an external code receives the address of an instance of a class, it can change the state of
The purpose of this exercise is to effectively demonstrate, that if an external code receives the address of an instance of a class, it can change the state of
The purpose of this exercise is to effectively demonstrate, that if an external code receives the address of an instance of a class, it can change the state of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site