63.2k views
1 vote
The main purpose of a constructor is to initialize the data members at the moment that an object is created. true or false?

1 Answer

4 votes

Answer:

True

Step-by-step explanation:

In Object Oriented Programming, a constructor is a type of subroutine whose purpose is to create an object.

when the constructor is called, it initializes the data members of the object and establishes the invariant of the class.

Some important concepts:

Data members: The data members are the data encapsulated within the object (e.g. int hour for the variable 'hour' in an object called 'date')

Invariant of the class: The class invariant is the invariant used to constrain the object (e.g. values between 0 and 24 for the variable 'hour')