Вы находитесь на странице: 1из 6

Test 4 Inheritance & CSC 122 Sections 1 & 2

rRun-Times Polymorphism Time: Due: R May 12 Calculator: OK Form I 1 of 6


STUDY GUIDE Notes: ??? STUDY GUIDE
Directions: Read the problems carefully. Circle all correct answers to multiple choice questions. Explain why if you circle
FALSE. Fill in all blanks (unless you decide a T/F is FALSE). Circle the answer you feel most appropriately fills the
blank if choices are given. Answer to the best of your ability.

This is a take-home test. You may ask ME questions for clarification not each other. Feel free to attach pages to
the end of the test if you need to continue a long answer. Please keep in mind that questions that ask you to write
code are intended to be done without the aid of the compiler.1 Take your time, but dont lose track of how much
time is left.

Good luck! Have fun!

1) Indicate whether each member is accessible only in the class it is in, inside the tree below its declaration, or anywhere in
the program (you may abbreviate: ONLY, BELOW, ANYwhere).
class One
{ class Four : public Two
A ONLY {
public: protected:
B ANY G BELOW
}; public:
H ANY
class Two : public One };
{
protected: class Five : public Four
C BELOW {
public: public:
D ANY I ANY
}; J ANY
};
class Three : public One
{ class Six : public Three
E ONLY {
protected: K ONLY
F BELOW L ONLY
}; };

2) Draw the inheritance tree for the class hierarchy from #1.
One

Two Three

Four Six

Five

3) TRUE / FALSE In inheritance, the original class is called the base class.
TRUE / FALSE The base class is also called the child or descendant of any classes which inherit from it.
TRUE / FALSE Such classes are called parents or ancestors of the base class.
TRUE / FALSE All of these classes taken together make up an inheritance hierarchy or tree.

1
I cant keep you from using it, but try to do it on your own first. . . please?! *big sad puppy dog eyes*
STUDY GUIDE
STUDY GUIDE Start Date: R May 5
Test 4 Inheritance & CSC 122 Sections 1 & 2
rRun-Times Polymorphism Time: Due: R May 12 Calculator: OK Form I 2 of 6
STUDY GUIDE Notes: ??? STUDY GUIDE
4) 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.

A) type, actions, data, class, object


B) class, attributes, behaviors, type, class
C) variable, data, methods, class, object
D) function, static data, arguments, function, function
E) function, arguments, references, library, program

5) TRUE / FALSE In polymorphism, the keyword mutable is used.


TRUE / FALSE Once a method (of a particular overload signature) is virtual, any derived classes which override it are
using polymorphism.
TRUE / FALSE Of course, the derived classes must still use the virtual keyword for this to work.
TRUE / FALSE Any overloaded versions of such methods will also be polymorphic.

6) The idea behind polymorphism is to allow a pointer to point objects from any class. In this way, the can
remember which actual it was pointing to and call the correct method.

A) base, derived, pointer, class, virtual


B) function, arguments, references, library, program
C) polymorphic, polymorphic, function, object, polymorphic
D) class, attributes, behaviors, type, class
E) const, ractually blanks, object, argument, ractually blanks

7) TRUE / FALSE Of all the methods to make virtual, the least important is the destructor.
TRUE / FALSE If the destructor isnt virtual, the class objects wont be completely destroyed and we will fragment our
heap.

8) Given the following inheritance hierarchy:


class One class Two : public One
{ {
public: public:
void print(void) const; void print(void) const;
}; };

Tell what happens in the following code (specifically, which print() method is called in each case):

One o;
Two t;
One * p;
p = &o;
p->print();
p = &t;
p->print();
One::print is being called in both places.
Why is this happening?
The hierarchy is non-polymorphic and so the base type of the pointer is used rather than the actual type of the targeted
[derived] object.

9) When one class inherits from another, the original class is known as the base/super class. The new

class is known as the derived/sub class. (Dont use parent/child or the like. . . )
STUDY GUIDE
STUDY GUIDE Start Date: R May 5
Test 4 Inheritance & CSC 122 Sections 1 & 2
rRun-Times Polymorphism Time: Due: R May 12 Calculator: OK Form I 3 of 6
STUDY GUIDE Notes: ??? STUDY GUIDE
10) Access to parent class data can be tricky. A descendant can access their parents 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 with protected data is that it can only be accessed
the hierarchy.

A) accessor, private, sibling, outside


B) accessor, protected, parent, at or below its declaration in
C) class, rjust blanks, program, at or above its declaration in
D) mutator, declared, ancestor, at or below its declaration in
E) private, public, child, inside

11) Software engineering has two terms to describe the C++ ideas of inheritance and composition. State each term and
describe what is meant by each briefly.
is a is the term for inheritance where one class begins life with behavior and attributes from a parent class it chooses.
has a is the term for composition where one object contains data of another class type.

12) TRUE / FALSE Inheritance applies to all member variables and non-constructor-like methods.
TRUE / FALSE Any friends of a class are not inherited by its descendants.
TRUE / FALSE These rules apply to operator functions, too.
TRUE / FALSE Function objects, however, cannot participate in inheritance at all.

13) Show a polymorph-ized version of the hierarchy from #8 above. Re-evaluate #8s code fragment based on your new hierarchy.
By simply placing the virtual keyword in front of the print method like so:
class One class Two : public One
{ {
public: public:
virtual void print(void) const; virtual void print(void) const;
}; };

(We do not even need to repeat it in class Two, but it is considered clearer and better style.)

The hierarchy now becomes polymorphic at least with

respect to printing and so now the first call will still be

One::print but the second call will go to Two::print recalling that p now points to an object of type Two!

14) What does an initializer list (those for constructors, remember?) have to do with inheritance?
In order to construct our ancestral data members in a non-default way (or on purpose by default), we must call the appropri-
ate parental constructor from our initializer list. There is simply no other place to do so wed otherwise

have to let them

default

and then mutate later

just as

the initializer list is designed to avoid!

15) Parent classes are often more than their children. Parent classes typically have fewer than their children.

A) specific, methods
B) general, data members
C) useful, objects
D) protected, accessors
E) virtual, polymorphisms

16) How does the keyword protected differ from private for access to members of a class?
It allows any descendant of our class to access the members as if they were public.
How does it differ from public?
It keeps all other parts of the program from accessing its members as if they were private!

STUDY GUIDE
STUDY GUIDE Start Date: R May 5
Test 4 Inheritance & CSC 122 Sections 1 & 2
rRun-Times Polymorphism Time: Due: R May 12 Calculator: OK Form I 4 of 6
STUDY GUIDE Notes: ??? STUDY GUIDE
17) Given the following hierarchy, show (pseudo)code to print all elements of a polymorphic container. (Dont forget to show the
declaration of the container.)

class Shape
{
public:
virtual void print(ostream & os) const = 0;
};

class Circle : public Shape


{
public:
void print(ostream & os) const;
};

class Square : public Shape


{
public:
void print(ostream & os) const;
};

(Hint: Your code should be able to print all elements from a container which contains information about both Squares and
Circles.)

vector<Shape*> poly_shapes;

/* fill vector like so:

poly_shapes.push_back(new Circle(. . .));


poly_shapes.push_back(new Square(. . .));

or whatever derived classes we have...filling in the . . .


with the users relevant info, of course!
*/

for (vector<Shape*>::size_type s = 0; s != poly_shapes.size(); s++)


{
poly_shapes[s]->print(cout);
}

/* dont forget to release the pointers!

// for loop as above


delete poly_shapes[s];
*/

Could the container contain information about a Shape object? Why/Why not?
No. The Shape class is abstract and cannot be used to create objects
only
pointers.

STUDY GUIDE
STUDY GUIDE Start Date: R May 5
Test 4 Inheritance & CSC 122 Sections 1 & 2
rRun-Times Polymorphism Time: Due: R May 12 Calculator: OK Form I 5 of 6
STUDY GUIDE Notes: ??? STUDY GUIDE
18) Design an inheritance structure for each of the following situations:

i) books, pages, chapters


Pages

Chapters

Books
ii) fish, frogs, lambs
Animal . . . OR. . . Animal

Fish Frogs Lambs Water Dwellers Land Dwellers

Fish Frogs Lambs


iii) computer, stick and dirt, abacus
Calculating Tool . . . OR. . . Calculating Tool

Stick & Dirt Abacus Computer By Hand Mechanical

Stick & Dirt Abacus Computer

Bonus Problems:
19) Would any of the situations from #18 make sense as composition instead of inheritance? Discuss such designs here.
Yes, #i) would perhaps be better as composition: a has
book a
collection
of
chapters which are in turn made up of a

collection

of page s.
20) What is going on with the print method of Shape in #17 above? Whats the = 0 thing there?
The = 0 after the function head in its declaration actually makes it a pure virtual function. It is by way of saying: Im not
only making this function virtual, but Im never going to define it. So store it at the NULL address, for all I care!

Can you create objects of type Shape? Why/Why not?
No. Any class with a pure method is considered
virtual abstract or non-concrete and the compiler will not allow

us to create an object of such a type. (After all, what might happen ifit
did and then we
called
this
function weve stored at
NULL?! *horror*)
Application 21) If a hierarchy contains a layer (or single class) which has no data, but provides an abstract API for its children to follow
Programming
Interface
and may therefore serve to clarify the tree to programmers using these classes is this okay?
Sure! That would be a great way to enforce a common language for all descendants under [each sibling] of this new layer.
And, as noted in the asking, it would probably give clarity to programmers trying to decide what branch to inherit under.

If you do this, what members must be in the class for the hierarchy to compile and run correctly?
Since constructor-like methods are not inherited, wed need to define these for the new layer even though it doesnt have
its own data members. We might also want to define pass-thru friends/non-members if our hierarchy utilizes them, as such
functions would also not be inherited. Basically any functionality that would

not
be
inherited.
22) Multiple inheritance is seldom used. It is one of the C++ features that Java has removed, in fact. Give a specific example of
when you could use multiple inheritance.
Almost anything will do: C chooses both A and B as its parents, for example. Particularly, iostream chose both istream and

ostream

as its parents, for instance.
Now explain how to code your example without multiple inheritance.
To code such a situation without multiple inheritance at all, wed most likely use single inheritance from one parent say A
and compose ourselves of an extra data member of our other desired parents aka Bs type.
STUDY GUIDE
STUDY GUIDE Start Date: R May 5
Test 4 Inheritance & CSC 122 Sections 1 & 2
rRun-Times Polymorphism Time: Due: R May 12 Calculator: OK Form I 6 of 6
STUDY GUIDE Notes: ??? STUDY GUIDE
23) Why is multiple inheritance such a troublesome feature of C++? Give a specific example of what can go wrong.
Any time we have a common grand-parent a diamond pattern in our inheritance tree we have the ultimate in multiple

inheritance hell. Here we inherit two copies of the grand-parents data members! Keeping both of these copies in sync

can

be tricky,

to say the least!
Is there any way to resolve this trouble within the inheritance system? (That is, still use multiple inheritance, but not have the
trouble arise..?)
The only way around this within the inheritance system would be for both of our parents to have remembered to
inherit virtually from grandpa/grandma. If they did, the compiler
will
remove
the
extra
copy
of
the
data
members for
us. No extra copy no sync problems!
24) If we were to use protected or private instead of public for the mode of inheritance, what changes does this cause later
in the hierarchy?
Within the following table, the cell values indicate the member accessibility with respect to the child class which inherits
via the column heading inheritance mode a data member which has access mode (in the parent) given by the row heading.
Inherited as: public protected private
Originally:
public PUB PROT PRIV
protected PROT PROT PRIV
private PRIV PRIV PRIV

25) Explain how one can use the dynamic cast language feature to determine what type of object a base-class pointer actually
points to.
By casting the base pointer to the type youd like to know about, youll be told it is not of that type by receipt of a NULL pointer
from the cast. If you get a
non-NULL pointer, then the object the pointer targets is of your
desired
type!
26) The standard library also includes the typeinfo library. What struct/class in this library can be used to determine exact
type names at run-time? What function(s) in this library help(s) with this task?
The type info struct/class is in this library and objects of this type are returned by the typeid operator.
Also found in/with type info are an operator== for it and the name method which returns a string representing the
actual name of the type in question.

Explain how you could use the facilities of the typeinfo library to determine what type of object a base-class pointer
actually points to.
Using the typeid operator, one could compare the result of it applied to a pointed-to object with the result of it applied to
the desired type using operator== (or operator== on the name results).
27) Which of these two methodologies (dynamic cast or the typeinfo library) seems best? Why?
dynamic cast is simpler and less invasive.
typeinfo has more information about the system including non-polymorphic classes, built-in types, etc.

28) If a class has a static data member, this single memory location will be shared by all objects instantiated of the class.
What if some class chooses the class with the static member as its parent objects of the child class will .

A) get their own memory location to share


B) share the same memory location as did all their parent class objects
C) go out for tea on Saturday afternoons
D) inherit a non-static version of the data member static only applies the first time it is used
E) not inherit the static data member this is an exception to data members being inherited

29) Within the following table, place a check mark in each cell to indicate that the row class can inherit from the column class.
(Ive filled in the first box for you. *smile*) (Hint: There is some duplication in the entries. Of course this could work for you or
against you. *shrug*)

Parent: non-template template pattern template pattern


Child:
non-template X X X
template pattern X X X
template pattern X X X
STUDY GUIDE
STUDY GUIDE Start Date: R May 5

Вам также может понравиться