Consider the following Java definition of an integer list class. class IntegerLi
ID: 3630814 • Letter: C
Question
Consider the following Java definition of an integer list class.class IntegerList
{
private int[] list = new int[200];
private int size = 0;
public boolean append(int value)
{
if (size == list.length)
return false;
list[size++] = value;
return true;
}
public String toString()
{
String result = "";
for (int i = 0; i < size; i++)
result += list[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
Constructors: What is the use of Constructor The main use of constructors is to initialize objects. The function of initialization is automatically carried out by the use of a special member function called a constructor. some important points about constructors: A constructor takes the same name as the class name. The programmer cannot declare a constructor as virtual or static, nor can the programmer declare a constructor as const, volatile, or const volatile. No return type is specified for a constructor. The constructor must be defined in the public. The constructor must be a public member. Overloading of constructors is possible. This will be explained in later sections of this tutorial. Destructors What is the use of Destructors Destructors are also special member functions used in C++ programming language. Destructors have the opposite function of a constructor. The main use of destructors is to release dynamic allocated memory. Destructors are used to free memory, release resources and to perform other clean up. Destructors are automatically named when an object is destroyed. Like constructors, destructors also take the same name as that of the class name. Constructors: are used to instantiate and possibly initialise an object: // instantiate an object of the class cat cat mour = new cat(); Constructors can be overloaded just like other methods in JAVA. Overloading will be discussed later. Briefly, method overloading means that two or more methods can share the same name. Compiler determines version depends on the list of actual parameters. In this particular statement, the new operator is used to allocate dynamic memory from the heap, and also as the constructor to construct the object in that memory space. The address of the memory containing the object is returned and assigned to the reference variable named mour. If the memory cannot be allocated, an exception will be thrown. In JAVA you do not define a constructor when you define a new class, a default constructor that takes no parameters is defined on your behalf. You can also define your own constructor with no argument needed. Defining a constructor is similar to defining a method, but must have the same name as the class, do not have a return type and must not have return statements. Destructors : A destructor is a special method typically used to perform cleanup after an object is no longer needed by the program. C++ supports destructors, but JAVA does not support destructors. JAVA supports another mechanism for returning memory to the operating system when it is no longer needed by an object. Java is a garbage collected language you cannot predict when (or even if) an object will be destroyed. Hence there is no direct equivalent of a destructor. Hope it helps u . :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.