Sams Teach Yourself C++ in 21 Days

(singke) #1
Answers 863

D


Exercises


  1. The C++ standard iostreamheader file declares coutand endlin namespace std.
    They cannot be used outside of the standard namespace stdwithout a namespace
    qualifier.

  2. You can add the following line between lines 0 and 1.
    using namespace std;
    You can add the following two lines between 0 and 1:
    using std::cout;
    using std::endl;
    You can change line 3 to the following:
    std::cout << “Hello world!” << std::endl;

  3. The following is one possible answer:
    Namespace MyStuff
    {
    class MyClass
    {
    //MyClass stuff
    }
    }


Day 19


Quiz


  1. Templates are built in to the C++ language and are type-safe. Macros are imple-
    mented by the preprocessor and are not type-safe.

  2. The parameter to the template creates an instance of the template for each type. If
    you create six template instances, six different classes or functions are created. The
    parameters to the function change the behavior or data of the function, but only
    one function is created.

  3. The general template friend function creates one function for every type of the
    parameterized class; the type-specific function creates a type-specific instance for
    each instance of the parameterized class.

  4. Yes, create a specialized function for the particular instance. In addition to creating
    Array::SomeFunction(), also create Array::SomeFunction()to change
    the behavior for integer arrays.

  5. One for each instance type of the class.


32 0672327112_app_d.qxd 11/19/04 12:30 PM Page 863

Free download pdf