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 three const int values, then add them together to produce a value that determines the size of an array in an array definition. Try to compile the same code in C and see what happens (you can generally force your C++ compiler to run as a C compiler by using a command-line flag).
  2. Prove to yourself that the C and C++ compilers really do treat constants differently. Create a global const and use it in a global constant expression; then compile it under both C and C++.
  3. Create example const definitions for all the built-in types and their variants. Use these in expressions with other consts to make new const definitions. Make sure they compile successfully.
  4. Create a const definition in a header file, include that header file in two .cpp files, then compile those files and link them. You should not get any errors. Now try the same experiment with C.
  5. Create a const whose value is determined at runtime by reading the time when the program starts (you’ll have to use the <ctime> standard header). Later in the program, try to read a second value of the time into your const and see what happens.
  6. Create a const array of char, then try to change one of the chars.
  7. Create an extern const declaration in one file, and put a main( ) in that file that prints the value of the extern const. Provide an extern const definition in a second file, then compile and link the two files together.
  8. Write two pointers to const long using both forms of the declaration. Point one of them to an array of long. Demonstrate that you can increment or decrement the pointer, but you can’t change what it points to.
  9. Write a const pointer to a double, and point it at an array of double. Show that you can change what the pointer points to, but you can’t increment or decrement the pointer.
  10. Write a const pointer to a const object. Show that you can only read the value that the pointer points to, but you can’t change the pointer or what it points to.
  11. Remove the comment on the error-generating line of code in PointerAssignment.cpp to see the error that your compiler generates.
  12. Create a character array literal with a pointer that points to the beginning of the array. Now use the pointer to modify elements in the array. Does your compiler report this as an error? Should it? If it doesn’t, why do you think that is?
  13. Create a function that takes an argument by value as a const; then try to change that argument in the function body.
  14. Create a function that takes a float by value. Inside the function, bind a const float& to the argument, and only use the reference from then on to ensure that the argument is not changed.
  15. Modify ConstReturnValues.cpp removing comments on the error-causing lines one at a time, to see what error messages your compiler generates.
  16. Modify ConstPointer.cpp removing comments on the error-causing lines one at a time, to see what error messages your compiler generates.
  17. Make a new version of ConstPointer.cpp called ConstReference.cpp which demonstrates references instead of pointers (you may need to look forward to Chapter 11).
  18. Modify ConstTemporary.cpp removing the comment on the error-causing line to see what error messages your compiler generates.
  19. Create a class containing both a const and a non-const float. Initialize these using the constructor initializer list.
  20. Create a class called MyString which contains a string and has a constructor that initializes the string, and a print( ) function. Modify StringStack.cpp so that the container holds MyString objects, and main( ) so it prints them.
  21. Create a class containing a const member that you initialize in the constructor initializer list and an untagged enumeration that you use to determine an array size.
  22. In ConstMember.cpp, remove the const specifier on the member function definition, but leave it on the declaration, to see what kind of compiler error message you get.
  23. Create a class with both const and non-const member functions. Create const and non-const objects of this class, and try calling the different types of member functions for the different types of objects.
  24. Create a class with both const and non-const member functions. Try to call a non-const member function from a const member function to see what kind of compiler error message you get.
  25. In Mutable.cpp, remove the comment on the error-causing line to see what sort of error message your compiler produces.
  26. Modify Quoter.cpp by making quote( ) a const member function and lastquote mutable.
  27. Create a class with a volatile data member. Create both volatile and non-volatile member functions that modify the volatile data member, and see what the compiler says. Create both volatile and non-volatile objects of your class and try calling both the volatile and non-volatile member functions to see what is successful and what kind of error messages the compiler produces.
  28. Create a class called bird that can fly( ) and a class rock that can’t. Create a rock object, take its address, and assign that to a void*. Now take the void*, assign it to a bird* (you’ll have to use a cast), and call fly( ) through that pointer. Is it clear why C’s permission to openly assign via a void* (without a cast) is a “hole” in the language, which couldn’t be propagated into C++?


[43] Some folks go as far as saying that everything in C is pass by value, since when you pass a pointer a copy is made (so you’re passing the pointer by value). However precise this might be, I think it actually confuses the issue.

[44] At the time of this writing, not all compilers supported this feature.

Thinking in C++
Prev Contents / Index Next

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