2.7. The Class Constructor
Our WordCount program requires that the user provide a file name. Optionally, the user can turn on several options, such as generating trace output and timing diagnostics. The Main() entry point processes the command-line options. That done, it must create a WordCount object and pass the file name and possible options to that object before asking the object to process the file.
One solution, of course, is to create a WordCount object with the default values and then reset the values the user passed in through the command-line options. The problem with this strategy is that it requires two steps, and the crucial second one is something that the user might overlook.
The class constructor is a mechanism by which the user ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access