COMPILER DESIGN

ADVANCED TOPICS IN COMPILER DESIGN

CODE GENERATION FOR OBJECT ORIENTED LANGUAGES

Question [CLICK ON ANY CHOICE TO KNOW THE RIGHT ANSWER]
Constructors are used to ____ ?
A
Initialize a newly created object.
B
To build a user interface.
C
To create a sub class
D
Free memory.
Explanation: 

Detailed explanation-1: -Constructors initialize the attributes in newly created objects. They have the same name as the class. A constructor signature is the constructor name followed by the parameter list which is a list of the types of the parameters and the variable names used to refer to them in the constructor.

Detailed explanation-2: -The name of the constructor provides the name of the class to instantiate. The constructor initializes the new object. The new operator returns a reference to the object it created. Often, this reference is assigned to a variable of the appropriate type.

Detailed explanation-3: -A class object with a constructor must be explicitly initialized or have a default constructor. Except for aggregate initialization, explicit initialization using a constructor is the only way to initialize non-static constant and reference class members.

Detailed explanation-4: -Such a method is called a constructor. Hence, a constructor is a method having the same name as that of the class and is used to initialize the instance variable of the objects.

Detailed explanation-5: -In class-based, object-oriented programming, a constructor (abbreviation: ctor) is a special type of subroutine called to create an object. It prepares the new object for use, often accepting arguments that the constructor uses to set required member variables.

There is 1 question to complete.