6.1

What is a constructor? Is it mandatory to use constructors in a class?

Answer

A constructor is a ‘special’ member function whose task is to initialize the object of its class. It is not mandatory to use constructor in a class.

6.2

How do we invoke a constructor function?

Answer

Constructor function are invoked automatically when the objects are created.

6.3

List some of the special properties of the constructor functions.

Answer

Special properties of the constructor functions:

  • 1. They should be declared in the public section.
  • 2. They are invoked automatically when the objects are created.
  • 3. They do not have return type, not even void.
  • 4. They cannot be inherited.
  • 5. Like other C++ functions, they can have default arguments.
  • 6. Constructors cannot be virtual.
6.4

What is a parameterized constructor?

Answer

The constructors that can take arguments are called parameterized constructors.

6.5

Can we have more than one constructors in a class? If yes, explain the need for such a situation.

Answer

Yes, we have when we need to overload the constructor, then we have to do this.

6.6

What do you mean by dynamic initialization of objects? Why do we need to this?

Answer

Initializing value of object during run time is called dynamic initialization of objects.
One advantage of dynamic initialization is that we can provide various initialization formats using overloaded constructors.

6.7

How is dynamic initialization of objects achieved?

Answer

Appropriate function of a object is invoked during run-time and thus dynamic initialization of object is achieved.
Consider following constructor:
santo (int p, int q, float r);
santo (int p, int q, int r);
It two int type value and one float type value are passed then sant (int p, int q, float r) is invoked.
It three int type value are passed then santo (int p, into q, int r) is invoked.

6.8

Distinguish between the following two statements:

time T2(T1);
time T2 = T1;
T1 and T2 are objects of time class.

Answer

time T2 (T1); ==> explicitly called of copy constructor
time T2 = T1; ==> implicitly called of copy constructor.

6.9

Describe the importance of destructors.

Answer

Destructors are important to release memory space for future use.

6.10

State whether the following statements are TRUE or FALSE.

  • (a) Constructors, like other member functions, can be declared anywhere in the class.
  • (b) Constructors do not return any values.
  • (c) A constructor that accepts no parameter is known as the default constructor.
  • (d) A class should have at least one constructor.
  • (e) Destructors never take any argument.
Answer
  • (a) FALSE
  • (b) TRUE
  • (c) TRUE
  • (d) TRUE
  • (e) TRUE

 


Next Previous