Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

class MutableString { private char[] chars = new char[200]; private int size = 0

ID: 3555670 • Letter: C

Question

class MutableString

{

    private char[] chars = new char[200];

    private int size = 0;

    public boolean set(char aChar, int index)

    {

        if (index < 0 || index >= chars.length)

            return false;

        chars[index] = aChar;

        return true;

    }

    public String toString()

    {

        String result = "";

        for (int i = 0; i < size; i++)

            result += chars[i];

        return result;

    }

}

Suppose this class was rewritten in C++ in two ways, the first with the array that represents the list as a stack-dynamic variable, and then with the list as a heap dynamic variable. Explain when constructors and destructors would be needed. Explain why no constructors or destructors are needed in the Java implementation.

Explanation / Answer

Destructor will be needed in implementing the above with list as a heap dynamic variable in C++

Destructor not required in implementation based on:
1. Array that represents the list as a stack-dynamic variable in C++
2. JAVA implementation given above

Java has its own garbage collection implementation so it does not require any destructor like C++.

And stack-dynamic variable in C++ dont need a destructor as their scope is only when that function is not over. Once the function gets ended, they will automatically get deleted.

But we need destructor in case of Heap Dynamic variables as they will not get automatically deleted.