Bruce Eckel's Thinking in C++, 2nd Ed Contents | Prev | Next

Exercises

  1. Create a Text class that contains a string object to hold the text of a file. Give it two constructors: a default constructor and a constructor that takes a string argument which is the name of the file to open. When the second constructor is used, open the file and read the contents into the string member object. Add a member function contents( ) to return the string so (for example) it can be printed. In main( ), open a file using Text and print the contents.
  2. Create a Message class with a constructor that takes a single string with a default value. Create a private member string, and in the constructor simply assign the argument string to your internal string. Create two overloaded member functions called print( ): one that takes no arguments and simply prints the message stored in the object, and one that takes a string argument, which it prints in addition to the internal message. Does it make sense to use this approach rather than the one used for the constructor?
  3. Determine how to generate assembly output with your compiler, and run experiments to deduce the name-decoration scheme.
  4. Create a class that contains four member functions, with 0, 1, 2, and 3 int arguments, respectively. Create a main( ) that makes an object of your class and calls each of the member functions. Now modify the class so it has instead a single member function with all the arguments defaulted. Does this change your main( ) ?
  5. Create a function with two arguments and call it from main( ) . Now make one of the arguments a “placeholder” (no identifier) and see if your call in main( ) changes
  6. Modify Stash3.h and Stash3.cpp to use default arguments in the constructor. Test the constructor by making two different versions of a Stash object.
  7. Create a new version of the Stack class (from the previous chapter) which contains the default constructor as before, and a second constructor which takes as its arguments an array of pointers to objects and the size of that array. This constructor should move through the array and push each pointer onto the Stack. Test your class with an array of string.
  8. Modify SuperVar so there are #ifdefs around all the vartype code as described in the section on enum.

Contents | Prev | Next


Contact: webmaster@codeguru.com
CodeGuru - the website for developers.
[an error occurred while processing this directive]