This post is a continuation of the previous topic, with a different program. In the following program, a constructor and destructor are used; they are included by default whenever a class is working inside a program. The constructor essentially "creates" the class' objects as they are declared, and when main ends, the destruct or "destroys" any created objects to free memory locations that the objects used so the memory can be used by another program. In many programs involving classes, a destructor or constructor doesn't need to be declared, but if main relies on several classes and many objects for each class, they can come in handy for tracking how many objects are created during the program's run. Either can also output a message upon creation or destruction of a class object (as the destructor does below). The constructor always executes at the moment a new class object is declared, and the destructor executes prior to ending the program, before return 0; executes.
And the output:
(yes, that is my terminal... ^^")
No comments:
Post a Comment