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

Determine whether the following statements in Section A are true or false. If an

ID: 673186 • Letter: D

Question

Determine whether the following statements in Section A are true or false. If an answer is true, explain why and, where appropriate, provide an example. If the answer is false, please explain why.

1. Constructors can have names different from their classes.

2. Destructors have default arguments.

3. If a program uses the delete operator, a destructor is not needed.

4. Explicit constructors need to be included in every class.

5. Destructors are called each time an object goes out of existence.

6. A class can have several default constructor functions.

7. Constructors need to be declared for each class.

8. Constructors are automatically called when objects are created.

Explanation / Answer

1)   False , Constructors always have the same name as their class, as defined by C++ syntax.

One of the good reasons for constructor having the same name is their expressiveness. For example, in Java you create an object like,

Now, the constructor is defined as,

So the statement above very well expresses that, you are creating an object and while this process the constructor MyClass() is called.

Now, whenever you create an object, it always calls its constructor. If that class is extending some other Base class, then their constructor will be called first and so on. All these operations are implicit. First the memory for the object is allocated (on heap) and then the constructor is called to initialize the object. If you don't provide a constructor, compiler will generate one for your class.

2)   False , Destructors have no arguments, since they are called implicitly when the object is destroyed.

When I don't declare a constructor for example, the compiler will provide me with a default constructor that will have no arguments and no definition (body), and thus, will take no action.

If I now don't declare a destructor, the compiler will provide me with a default destructor with no defintion (body), and thus, I think no action.

Because if you don't have any (publiclly-accessible) constructors or destructors, then an object of the class cannot be instantiated. Consider:

3) False. The delete operator calls the destructor of the object it is deleting.

4) False. If you don't specify a constuctor, the compiler creates a default constructor. However, it is best to create a constructor that sets all the data members of the class to some sensible value.

5) True.

6) True. You can use different constructors so that the object can be created with different parameters, if needed.

7) False. Constructors only need to be declared if the class constructor has parameters without a default value.

8) True.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote