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 function with a static variable that is a pointer (with a default argument of zero). When the caller provides a value for this argument it is used to point at the beginning of an array of int. If you call the function with a zero argument (using the default argument), the function returns the next value in the array, until it sees a “-1” value in the array (to act as an end-of-array indicator). Exercise this function in main( ).
  2. Create a function that returns the next value in a Fibonacci sequence every time you call it. Add an argument that is a bool with a default value of false such that when you give the argument with true it “resets” the function to the beginning of the Fibonacci sequence. Exercise this function in main( ).
  3. Create a class that holds an array of ints. Set the size of the array using static const int inside the class. Add a const int variable, and initialize it in the constructor initializer list; make the constructor inline. Add a static int member variable and initialize it to a specific value. Add a static member function that prints the static data member. Add an inline member function called print( ) to print out all the values in the array and to call the static member function. Exercise this class in main( ).
  4. Create a class called Monitor that keeps track of the number of times that its incident( ) member function has been called. Add a print( ) member function that displays the number of incidents. Now create a global function (not a member function) containing a static Monitor object. Each time you call the function it should call incident( ), then print( ) member function to display the incident count. Exercise the function in main( ).
  5. Modify the Monitor class from Exercise 4 so that you can decrement( ) the incident count. Make a class Monitor2 that takes as a constructor argument a pointer to a Monitor1, and which stores that pointer and calls incident( ) and print( ). In the destructor for Monitor2, call decrement( ) and print( ). Now make a static object of Monitor2 inside a function. Inside main( ), experiment with calling the function and not calling the function to see what happens with the destructor of Monitor2.
  6. Make a global object of Monitor2 and see what happens.
  7. Create a class with a destructor that prints a message and then calls exit( ). Create a global object of this class and see what happens.
  8. In StaticDestructors.cpp, experiment with the order of constructor and destructor calls by calling f( ) and g( ) inside main( ) in different orders. Does your compiler get it right?
  9. In StaticDestructors.cpp, test the default error handling of your implementation by turning the original definition of out into an extern declaration and putting the actual definition after the definition of a (whose Obj constructor sends information to out). Make sure there’s nothing else important running on your machine when you run the program or that your machine will handle faults robustly.
  10. Prove that file static variables in header files don’t clash with each other when included in more than one cpp file.
  11. Create a simple class containing an int, a constructor that initializes the int from its argument, a member function to set the int from its argument, and a print( ) function that prints the int. Put your class in a header file, and include the header file in two cpp files. In one cpp file make an instance of your class, and in the other declare that identifier extern and test it inside main( ). Remember, you’ll have to link the two object files or else the linker won’t find the object.
  12. Make the instance of the object in Exercise 11 static and verify that it cannot be found by the linker because of this.
  13. Declare a function in a header file. Define the function in one cpp file and call it inside main( ) in a second cpp file. Compile and verify that it works. Now change the function definition so that it is static and verify that the linker cannot find it.
  14. Modify Volatile.cpp from Chapter 8 to make comm::isr( ) something that could actually work as an interrupt service routine. Hint: an interrupt service routine doesn’t take any arguments.
  15. Write and compile a simple program that uses the auto and register keywords.
  16. Create a header file containing a namespace. Inside the namespace create several function declarations. Now create a second header file that includes the first one and continues the namespace, adding several more function declarations. Now create a cpp file that includes the second header file. Alias your namespace to another (shorter) name. Inside a function definition, call one of your functions using scope resolution. Inside a separate function definition, write a using directive to introduce your namespace into that function scope, and show that you don’t need scope resolution to call the functions from your namespace.
  17. Create a header file with an unnamed namespace. Include the header in two separate cpp files and show that an unnamed space is unique for each translation unit.
  18. Using the header file from Exercise 17, show that the names in an unnamed namespace are automatically available in a translation unit without qualification.
  19. Modify FriendInjection.cpp to add a definition for the friend function and to call the function inside main( ).
  20. In Arithmetic.cpp, demonstrate that the using directive does not extend outside the function in which the directive was made.
  21. Repair the problem in OverridingAmbiguity.cpp, first with scope resolution, then instead with a using declaration that forces the compiler to choose one of the identical function names.
  22. In two header files, create two namespaces, each containing a class (with all inline definitions) with a name identical to that in the other namespace. Create a cpp file that includes both header files. Create a function, and inside the function use the using directive to introduce both namespaces. Try creating an object of the class and see what happens. Make the using directives global (outside of the function) to see if it makes any difference. Repair the problem using scope resolution, and create objects of both classes.
  23. Repair the problem in Exercise 22 with a using declaration that forces the compiler to choose one of the identical class names.
  24. Extract the namespace declarations in BobsSuperDuperLibrary.cpp and UnnamedNamespaces.cpp and put them in separate header files, giving the unnamed namespace a name in the process. In a third header file create a new namespace that combines the elements of the other two namespaces with using declarations. In main( ), introduce your new namespace with a using directive and access all the elements of your namespace.
  25. Create a header file that includes <string> and <iostream> but does not use any using directives or using declarations. Add “include guards” as you’ve seen in the header files in this book. Create a class with all inline functions that contains a string member, with a constructor that initializes that string from its argument and a print( ) function that displays the string. Create a cpp file and exercise your class in main( ).
  26. Create a class containing a static double and long. Write a static member function that prints out the values.
  27. Create a class containing an int, a constructor that initializes the int from its argument, and a print( ) function to display the int. Now create a second class that contains a static object of the first one. Add a static member function that calls the static object’s print( ) function. Exercise your class in main( ).
  28. Create a class containing both a const and a non-const static array of int. Write static methods to print out the arrays. Exercise your class in main( ).
  29. Create a class containing a string, with a constructor that initializes the string from its argument, and a print( ) function to display the string. Create another class that contains both const and non-const static arrays of objects of the first class, and static methods to print out these arrays. Exercise this second class in main( ).
  30. Create a struct that contains an int and a default constructor that initializes the int to zero. Make this struct local to a function. Inside that function, create an array of objects of your struct and demonstrate that each int in the array has automatically been initialized to zero.
  31. Create a class that represents a printer connection, and that only allows you to have one printer.
  32. In a header file, create a class Mirror that contains two data members: a pointer to a Mirror object and a bool. Give it two constructors: the default constructor initializes the bool to true and the Mirror pointer to zero. The second constructor takes as an argument a pointer to a Mirror object, which it assigns to the object’s internal pointer; it sets the bool to false. Add a member function test( ): if the object’s pointer is nonzero, it returns the value of test( ) called through the pointer. If the pointer is zero, it returns the bool. Now create five cpp files, each of which includes the Mirror header. The first cpp file defines a global Mirror object using the default constructor. The second file declares the object in the first file as extern, and defines a global Mirror object using the second constructor, with a pointer to the first object. Keep doing this until you reach the last file, which will also contain a global object definition. In that file, main( ) should call the test( ) function and report the result. If the result is true, find out how to change the linking order for your linker and change it until the result is false.
  33. Repair the problem in Exercise 32 using technique one shown in this book.
  34. Repair the problem in Exercise 32 using technique two shown in this book.
  35. Without including a header file, declare the function puts( ) from the Standard C Library. Call this function from main( ).



[47]Bjarne Stroustrup and Margaret Ellis, The Annotated C++ Reference Manual, Addison-Wesley, 1990, pp. 20-21.

Thinking in C++
Prev Contents / Index Next

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