Sams Teach Yourself C++ in 21 Days

(singke) #1
strcpy(fullname,firstname);
offset = strlen(firstname);
strcpy(fullname+offset,” “);
offset += 1;
strcpy(fullname+offset,middlename);
offset += strlen(middlename);
strcpy(fullname+offset,”. “);
offset += 2;
strcpy(fullname+offset,lastname);

cout << firstname << “-” << middlename << “-”
<< lastname << endl;
cout << “Fullname: “ << fullname << endl;

return 0;
}


  1. The array is five elements by four elements, but the code initializes 4×5.

  2. You wanted to write i<5, but you wrote i<=5instead. The code will run when i ==
    5 and j == 4, but there is no such element as SomeArray[5][4].


Day 14


Quiz


  1. A down cast (also called “casting down”) is a declaration that a pointer to a base
    class is to be treated as a pointer to a derived class.

  2. This refers to the idea of moving shared functionality upward into a common base
    class. If more than one class shares a function, it is desirable to find a common
    base class in which that function can be stored.

  3. If neither class inherits using the keyword virtual,two Shapesare created, one
    for Rectangleand one for Shape. If the keyword virtualis used for both classes,
    only one shared Shapeis created.

  4. Both Horseand Birdinitialize their base class,Animal, in their constructors.
    Pegasusdoes as well, and when a Pegasusis created, the Horseand Birdinitial-
    izations of Animalare ignored.

  5. The following is one possible answer:
    class Vehicle
    {
    virtual void Move() = 0;
    }

  6. None must be overridden unless you want to make the class nonabstract, in which
    case all three must be overridden.


848 Appendix D

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

Free download pdf