C++ provides a pair of in-built functions called constructor and destructor. The compiler automatically executes these functions. When an object is created, constructor is executed. The programmer can also pass values to the constructor to initialize member variables with different values. The destructor destroys the object. It is executed at the end of program when objects are of no use.
Constructors and destructors decide how the objects of a class are created, initialized, copied, and destroyed. They are member functions. Their names are distinguished from all other member functions because they have the same name as the class they belong to.
It is also possible to create constructor with arguments as in normal functions.
Similar to functions, it is also possible to overload constructors and assign default arguments.
When we pass an object by value into a function, a temporary copy of that object is created. All copy constructors require one argument, with reference to an object of that class. Using copy constructors, it is possible for the programmers to declare and initialize one object using reference of another object. Thus, whenever a constructor is called, a copy of an object is created.
We can also make the object constant by the keyword const. Any effort to alter values of variables made by it will generate an error. The constant object can access only constant functions.
The compiler automatically calls the constructor and destructor. We can also call the constructor and destructor in the same fashion as we call the normal user-defined function. The calling methods are different for constructor and destructor.
Objects are created with names. It is possible to declare objects without name. Such objects are known as anonymous objects.
When the constructor and destructor are private, they cannot be executed implicitly, and hence, it is a must to execute them explicitly.
We can also call the constructor and destructor in the same fashion as we call the normal user defined function.
The class declaration can also be done inside the other class. While declaring object of such class, it is necessary to precede the name of the outer class. The name of outer class is called qualifier name, and the class defined inside is called as nested class.
The dynamic construction means allocation of memory by constructor for objects and dynamic destruction means releasing memory using the destructor.
To use main() as a constructor and destructor, we need to define class with the name main.
Similar to normal and member functions, constructors also support recursion.
For global object, constructor is executed before main() function and destructor is executed after the completion of execution main() function.
Test Your Skills
Test Your Skills Now!
This is only a practice test, it is designed to help you revise your concepts
The test contains questions, only 1 option is correct for each question
This is a timed test.
After you have finished the test, press on the 'Finish Test' button to know your score and get the correct answers