Exercises






Exercises

Exercise 1

For each of the following errors, write a simple test case containing the error, and try to compile it. In the error messages, look for the key words that relate to the error in the code.

  1. Attempting aggregate initialization of an array object with more initializers than the number of elements it holds

  2. Attempting default construction of an array object holding elements whose type does not have a default constructor

  3. Attempting aggregate initialization with a type that cannot be converted to the type of elements the array type holds

  4. Attempting copy construction from an array object of a different type

  5. Attempting to call get with an index value that is out of bounds

Exercise 2

Write a struct named elt that holds a value of type unsigned char but doesn't initialize it. Create an object of type std::tr1::array<elt, 6> with no initializer, and show the stored values. Now write a new struct, elt1, that holds a value of type unsigned char and has a default constructor that sets the stored value to 1. Create an object of type std::tr1::array <elt1, 6> with no initializer, and show the stored values.

Exercise 3

Repeat the preceding exercise, but when you create each array object, use an aggregate initializer that initializes some but not all of the stored objects. (You'll have to add another constructor to elt1. It should take a value of type unsigned char and copy it to the stored value.)

Exercise 4

Create an object of type array<int, 6> that initially holds all 0s. Change all its stored values to 1. Create an object of type array<int, 6> that initially holds all 1s. Change all its stored values to 0.

Exercise 5

Create two objects of type array<int, 6>. The first one should contain the values 1 through 6 in ascending order. The second one should contain the value 1, 2, 3, 3, 9000, and 9001, in that order. Which of the two is less than the other? Write a test program to verify your answer.

Exercise 6

Repeat Exercise 5 from Chapter 1 with array instances instead of tuple instances.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows