What is a constructor? Is it mandatory to use constructors in a class?
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.
How do we invoke a constructor function?
Constructor function are invoked automatically when the objects are created.
List some of the special properties of the constructor functions.
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.
What is a parameterized constructor?
The constructors that can take arguments are called parameterized constructors.
Can we have more than one constructors in a class? If yes, explain the need for such a situation.
Yes, we have when we need to overload the constructor, then we have to do this.
What do you mean by dynamic initialization of objects? Why do we need to this?
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.
How is dynamic initialization of objects achieved?
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.
Distinguish between the following two statements:
time T2 = T1;
T1 and T2 are objects of time class.
time T2 (T1); ==> explicitly called of copy constructor
time T2 = T1; ==> implicitly called of copy constructor.
Describe the importance of destructors.
Destructors are important to release memory space for future use.
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.
- (a) FALSE
- (b) TRUE
- (c) TRUE
- (d) TRUE
- (e) TRUE