Topical Information

The purpose of this quiz is to give you a chance to focus your knowledge of inheritance in C++.

Quiz Information

Questions

  1. The idea behind inheritance is to allow one ____ to inherit some ____ and ____ from a previously defined ____. In this way, the new ____ can have a smaller definition while still containing all of the material of both itself and its ancestor.

    1. object, data, methods, class, type NO
    2. type, actions, data, class, object NO
    3. function, arguments, references, library, program NO
    4. class, attributes, behaviors, type, class YES
    5. function, static data, arguments, function, function NO
  2.  
    TRUE   FALSE   In inheritance, the original class is called the base class.
    TRUE   FALSE   The base class is also called the parent or ancestor of any classes which inherit from it.
    TRUE   FALSE   Such classes are called children or descendents of the base class.
    TRUE   FALSE   All of these classes taken together make up a hierarchy or inheritance tree.
  3. Access to parent class data can be tricky. A descendent can access their parent's private data by using a(n) ____ method of the parent class. Data which is ____ in the parent, on the other hand, can be accessed directly. Of course, anything which was public in the ____ can also be accessed. The difference between this and protected data is that it can only be accessed ____ the hierarchy.

    1. public, public, child, inside NO
    2. accessor, private, sibling, outside NO
    3. accessor, protected, parent, inside YES
    4. mutator, declared, ancestor, below its declaration in NO
    5. class, [just blank], program, above its declaration in NO
  4. In the following class hierarchy, show all of the data and methods available to each class (be it directly or through accessors).

        class One
        {
            A
            B
        };
    
        class Two : public One
        {
            C
            D
        };
    
        class Three : public One
        {
            E
            F
        };
    
        class Four : public Two
        {
            G
            H
        };
    
        class Five : public Four
        {
            I
            J
        };
    
    
        One:    A, B
        Two:    A, B, C, D
        Three:  A, B, E, F
        Four:   A, B, C, D, G, H
        Five:   A, B, C, D, G, H, I, J
    
    
  5. Draw the inheritance tree for the following classes. Label each class 'node' with its data/method members (only the ones it adds — not all the ones it owns).

        class One
        {
            A
            B
        };
    
        class Two : public One
        {
            C
            D
        };
    
        class Three : public One
        {
            E
            F
        };
    
        class Four : public Two
        {
            G
            H
        };
    
        class Five : public Four
        {
            I
            J
        };
    
    
                                      One [A, B]
                                       |
                           .-----------+-----------.
                           |                       |
                          Two [C, D]             Three [E, F]
                           |
                         Four [G, H]
                           |
                         Five [I, J]
    
    
  6. Indicate where each member would be accessible (such as in the class itself, inside the tree below its declaration, or anywhere in the program).

        class One
        {
            A
        public:
            B
        };
    
        class Two : public One
        {
        protected:
            C
        public:
            D
        };
    
        class Three : public One
        {
            E
        protected:
            F
        };
    
        class Four : public Two
        {
        protected:
            G
        public:
            H
        };
    
        class Five : public Four
        {
        public:
            I
            J
        };
    
    
        A:  class One
        B:  anywhere
        C:  Two or below
        D:  anywhere
        E:  class Three
        F:  Three or below
        G:  Four or below
        H:  anywhere
        I:  anywhere
        J:  anywhere
    
    
  7. Show an inheritance tree for tables (you know, dining, coffee, desk, pool, etc.). (You need no code — just a diagram with classes, attributes, and actions.)

    
        Show me your solution and I'll critique...
    
    
  8. Show code for an inheritance hierarchy for people who work at or attend a college (students, teachers, maintenance, secretaries, administrators, etc.). What common attributes and behaviors do they all share (id codes, pay or fees, schedules, etc.)? What relationships are shared among these groups that lead to an inheritance pattern (students and teachers go to classes, secretaries work under administrators, etc.)? (You don't have to define the methods of the classes, just show their definitions.)

    
        Show me your solution and I'll critique...