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
Scripting Languages
Development Tools
Web Development
GUI Toolkits/Desktop
Mail Systems
Eclipse Documentation

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




Thinking in C++
Prev Contents / Index Next


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

  1. Turn the “bird & rock” code fragment at the beginning of this chapter into a C program (using structs for the data types), and show that it compiles. Now try to compile it with the C++ compiler and see what happens.
  2. Take the code fragments in the beginning of the section titled “References in C++” and put them into a main( ). Add statements to print output so that you can prove to yourself that references are like pointers that are automatically dereferenced.
  3. Write a program in which you try to (1) Create a reference that is not initialized when it is created. (2) Change a reference to refer to another object after it is initialized. (3) Create a NULL reference.
  4. Write a function that takes a pointer argument, modifies what the pointer points to, and then returns the destination of the pointer as a reference.
  5. Create a class with some member functions, and make that the object that is pointed to by the argument of Exercise 4. Make the pointer a const and make some of the member functions const and prove that you can only call the const member functions inside your function. Make the argument to your function a reference instead of a pointer.
  6. Take the code fragments at the beginning of the section titled “Pointer references” and turn them into a program.
  7. Create a function that takes an argument of a reference to a pointer to a pointer and modifies that argument. In main( ), call the function.
  8. Create a function that takes a char& argument and modifies that argument. In main( ), print out a char variable, call your function for that variable, and print it out again to prove to yourself that it has been changed. How does this affect program readability?
  9. Write a class that has a const member function and a non-const member function. Write three functions that take an object of that class as an argument; the first takes it by value, the second by reference, and the third by const reference. Inside the functions, try to call both member functions of your class and explain the results.
  10. (Somewhat challenging) Write a simple function that takes an int as an argument, increments the value, and returns it. In main( ), call your function. Now discover how your compiler generates assembly code and trace through the assembly statements so that you understand how arguments are passed and returned, and how local variables are indexed off the stack.
  11. Write a function that takes as its arguments a char, int, float, and double. Generate assembly code with your compiler and find the statements that push the arguments on the stack before a function call.
  12. Write a function that returns a double. Generate assembly code and determine how the value is returned.
  13. Produce assembly code for PassingBigStructures.cpp. Trace through and demystify the way your compiler generates code to pass and return large structures.
  14. Write a simple recursive function that decrements its argument and returns zero if the argument becomes zero, otherwise it calls itself. Generate assembly code for this function and explain how the way that the assembly code is created by the compiler supports recursion.
  15. Write code to prove that the compiler automatically synthesizes a copy-constructor if you don’t create one yourself. Prove that the synthesized copy-constructor performs a bitcopy of primitive types and calls the copy-constructor of user-defined types.
  16. Write a class with a copy-constructor that announces itself to cout. Now create a function that passes an object of your new class in by value and another one that creates a local object of your new class and returns it by value. Call these functions to prove to yourself that the copy-constructor is indeed quietly called when passing and returning objects by value.
  17. Create a class that contains a double*. The constructor initializes the double* by calling new double and assigning a value to the resulting storage from the constructor argument. The destructor prints the value that’s pointed to, assigns that value to -1, calls delete for the storage, and then sets the pointer to zero. Now create a function that takes an object of your class by value, and call this function in main( ). What happens? Fix the problem by writing a copy-constructor.
  18. Create a class with a constructor that looks like a copy-constructor, but that has an extra argument with a default value. Show that this is still used as the copy-constructor.
  19. Create a class with a copy-constructor that announces itself. Make a second class containing a member object of the first class, but do not create a copy-constructor. Show that the synthesized copy-constructor in the second class automatically calls the copy-constructor of the first class.
  20. Create a very simple class, and a function that returns an object of that class by value. Create a second function that takes a reference to an object of your class. Call the first function as the argument of the second function, and demonstrate that the second function must use a const reference as its argument.
  21. Create a simple class without a copy-constructor, and a simple function that takes an object of that class by value. Now change your class by adding a private declaration (only) for the copy-constructor. Explain what happens when your function is compiled.
  22. This exercise creates an alternative to using the copy-constructor. Create a class X and declare (but don’t define) a private copy-constructor. Make a public clone( ) function as a const member function that returns a copy of the object that is created using new. Now write a function that takes as an argument a const X& and clones a local copy that can be modified. The drawback to this approach is that you are responsible for explicitly destroying the cloned object (using delete) when you’re done with it.
  23. Explain what’s wrong with both Mem.cpp and MemTest.cpp from Chapter 7. Fix the problem.
  24. Create a class containing a double and a print( ) function that prints the double. In main( ), create pointers to members for both the data member and the function in your class. Create an object of your class and a pointer to that object, and manipulate both class elements via your pointers to members, using both the object and the pointer to the object.
  25. Create a class containing an array of int. Can you index through this array using a pointer to member?
  26. Modify PmemFunDefinition.cpp by adding an overloaded member function f( ) (you can determine the argument list that causes the overload). Now make a second pointer to member, assign it to the overloaded version of f( ), and call the function through that pointer. How does the overload resolution happen in this case?
  27. Start with FunctionTable.cpp from Chapter 3. Create a class that contains a vector of pointers to functions, with add( ) and remove( ) member functions to add and remove pointers to functions. Add a run( ) function that moves through the vector and calls all of the functions.
  28. Modify the above Exercise 27 so that it works with pointers to member functions instead.

[48] Thanks to Owen Mortensen for this example

Thinking in C++
Prev Contents / Index Next

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