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++ Vol 2 - Practical Programming
Prev Home Next

Exercises

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

1.      Create a variation of SingletonPattern.cpp where all functions are static. Is the instance( ) function still necessary in this case?

2.      Starting with SingletonPattern.cpp, create a class that provides a connection to a service that stores and retrieves data from a configuration file.

3.      Using SingletonPattern.cpp as a starting point, create a class that manages a fixed number of its own objects. Assume the objects are database connections and you only have a license to use a fixed quantity of these at any one time.

4.      Modify KissingPrincess2.cpp by adding another state to the system, so that each kiss cycles the creature to the next state.

5.      Find C16:TStack.h from Thinking in C++, Volume 1, 2nd Edition (downloadable from www. BruceEckel.com). Create an Adapter for this class such that you can apply the STL algorithm for_each( ) to the elements of the TStack, using your adapter. Create a TStack of string, fill it with strings and use for_each( ) to count all the letters in all the strings in the TStack.

6.      Create a framework (that is, use the Template Method pattern) that takes a list of file names on the command line. It opens each file except the last for reading, and the last file it opens for writing. The framework will process each input file using an undetermined policy and write the output to the last file. Inherit to customize this framework to create two separate applications:
1) Converts all the letters in each file to uppercase.
2) Searches the files for words given in the first file.

7.      Modify Exercise 6 to use Strategy instead of Template Method.

8.      Modify Strategy.cpp to include State behavior, so that the Strategy can be changed during the lifetime of the Context object.

9.      Modify Strategy.cpp to use a Chain of Responsibility approach, where you keep trying different ways to get someone to say their name without admitting you ve forgotten it.

10.    Add a class Triangle to ShapeFactory1.cpp.

11.    Add a class Triangle to ShapeFactory2.cpp.

12.    Add a new type of GameEnvironment called GnomesAndFairies to AbstractFactory.cpp.

13.    Modify ShapeFactory2.cpp so that it uses an Abstract Factory to create different sets of shapes (for example, one particular type of factory object creates thick shapes, another creates thin shapes, but each factory object can create all the shapes: circles, squares, triangles, and so on).

14.    Modify VirtualConstructor.cpp to use a map instead of if-else statements inside Shape::Shape(string type).

15.    Break a text file up into an input stream of words (keep it simple: just break the input stream on white space). Create one Builder that puts the words into a set, and another that produces a map containing words and occurrences of those words (that is, it does a word count).

16.      Create a minimal Observer-Observable design in two classes, without base classes and without the extra arguments in Observer.h and the member functions in Observable.h. Just create the bare minimum in the two classes, and then demonstrate your design by creating one Observable and many Observers and cause the Observable to update the Observers.

17.      Change InnerClassIdiom.cpp so that Outer uses multiple inheritance instead of the inner class idiom.

18.    Modify PaperScissorsRock.java to replace the double dispatch with a table lookup. The easiest way to do this is to create a map of maps, with the key of each map the typeid(obj).name( ) information of each object. Then you can do the lookup by saying: map[typeid(obj1).name( )][typeid(obj2).name( )].
Notice how much easier it is to reconfigure the system. When is it more appropriate to use this approach vs. hard-coding the dynamic dispatches? Can you create a system that has the syntactic simplicity of use of the dynamic dispatch but uses a table lookup?

19.    Create a business-modeling environment with three types of Inhabitant: Dwarf (for engineers), Elf (for marketers), and Troll (for managers). Now create a class called Project that instantiates the different inhabitants and causes them to interact( ) with each other using multiple dispatching.

20.    Modify the previous exercise to make the interactions more detailed. Each Inhabitant can randomly produce a Weapon using getWeapon( ): a Dwarf uses Jargon or Play, an Elf uses InventFeature or SellImaginaryProduct, and a Troll uses Edict and Schedule. You decide which weapons win and lose in each interaction (as in PaperScissorsRock.cpp). Add a battle( ) member function to Project that takes two Inhabitants and matches them against each other. Now create a meeting( ) member function for Project that creates groups of Dwarf, Elf, and Manager and battles the groups against each other until only members of one group are left standing. These are the winners.

21.    Add a Hummingbird Visitor to BeeAndFlowers.cpp.

22.    Add a Sunflower type to BeeAndFlowers.cpp and notice what you need to change to accommodate this new type.

23.    Modify BeeAndFlowers.cpp so that it does not use Visitor, but reverts to a regular class hierarchy instead. Turn Bee into a collecting parameter.


Thinking in C++ Vol 2 - Practical Programming
Prev Home Next

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