Follow Techotopia on Twitter

On-line Guides
All Guides
eBook Store
iOS / Android
Linux for Beginners
Office Productivity
Linux Installation
Linux Security
Linux Utilities
Linux Virtualization
Linux Kernel
System/Network Admin
Programming
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Databases
Mail Systems
openSolaris
Eclipse Documentation
Techotopia.com
Virtuatopia.com
Answertopia.com

How To Guides
Virtualization
General System Admin
Linux Security
Linux Filesystems
Web Servers
Graphics & Desktop
PC Hardware
Windows
Problem Solutions
Privacy Policy

  




 

 

Thinking in C++
Prev Contents / Index Next

Exercises

Solutions to selected exercises can be found in the electronic document The Thinking in C++ Annotated Solution Guide, available for a small fee from www.BruceEckel.com.

  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 that 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 instead of 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 Chapter 6) that contains the default constructor as before, and a second constructor that 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 that there are #ifdefs around all the vartype code as described in the section on enum. Make vartype a regular and public enumeration (with no instance) and modify print( ) so that it requires a vartype argument to tell it what to do.
  9. Implement Mem2.h and make sure that the modified class still works with MemTest.cpp.
  10. Use class Mem to implement Stash. Note that because the implementation is private and thus hidden from the client programmer, the test code does not need to be modified.
  11. In class Mem, add a bool moved( ) member function that takes the result of a call to pointer( ) and tells you whether the pointer has moved (due to reallocation). Write a main( ) that tests your moved( ) member function. Does it make more sense to use something like moved( ) or to simply call pointer( ) every time you need to access the memory in Mem?

Thinking in C++
Prev Contents / Index Next

 
 
   Reproduced courtesy of Bruce Eckel, MindView, Inc. Design by Interspire