The purpose of this quiz is to give you a chance to focus your knowledge of inheritance in C++.
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.
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. |
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.
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
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]
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
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...
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...