Lecture 22 Flashcards

1
Q

Polymorphism allows

A

the programmer to provide alternate, abstracted views of an object.
–While this isn’t useful if all we have is Circle, the reasoning becomes more important with Square, Pentagon, and Hexagon (for example).
–Squares aren’t Circles, but they are both Shapes, with Shape properties.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

use of polymorphism in C++

requires

A
handling objects via their
pointers
– A pointer of a subclass can always be stored as
a variable of its superclass.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Polymorphism allows for multiple

A

classes to share similar abstract views that may be seen as a single “role”.
–Very distinct, different types sometimes share functionally similar methods.
–The implementation of these methods may be specific to each implementing class, and is not directly shareable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Step 1: creating an abstract base class that declares the common methods.

A

–One or more methods are declared (but not defined).
–Use the virtual keyword!
–These methods have specifications that should be followed whenever they are implemented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Step 2: declaring our classes to be extensions of that abstract base class.

A

–This allows instances of our classes to be considered as instances of that abstract base type.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Step 3: implementing the declared methods of the base class.

A
–All undefined methods must be implemented for the class to be instantiated.
–These implementations should follow the base class’s specifications.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

pure virtual functions allow what?

A
–This reflects interfaces in Java.
–This allows a base class to ensure it remains abstract and to force a base class to implement the method manually.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In C++, entire class specifications can be inherited from

A
a base class to a derived class.
–This includes fields and method definitions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Inheriting from a class means that

A
the derived class should be considered a “more specific” version of the base class.
–It inherits all the original specifications and adds more of its own.
–In C++, any (publicly) derived class is automatically polymorphic to its base class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

public:

A

declares a field or method is fully visible from any object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

private:

A

places fields or methods on “lockdown,” making them invisible outside of the class.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

protected:

A
declares a field or method is invisible outside of the class, except to those inheriting from the class.
–This can be very useful to extend core functionality in derived classes.
–One example:  providing an empty protected method called by the base class in certain situations.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Declaring another class as a friend grants it

A

private-level access to all fields and methods.
–This only applies for the exact friend-granting class.
–Likewise, only the exact friend stated is granted special access.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

the override keyword

A

may be appended at the end

of the declaration’s signature

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

To make sure that a derived class is properly overriding (or implementing) a base class method, the

A

override keyword may be appended at the end of the declaration’s signature.
–If the overridden method does not exist, or is not virtual, a compiler error will result.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

when extending a class in C++, an access specifier is used to

A
–This allows the derived class to restrict access to the base class’s members.
–All base class fields and methods will have their access be at least as strict as the given modifier.
–protected inheritance will cause public base class methods to become protected within the derived class.
–The implied polymorphism of the derived class to its base will be similarly restricted.
17
Q

To make sure that a derived class cannot possibly override a method, the

A
base class may declare a method final.
–Any attempt to override it in a derived class will be marked as a compiler error.
–Both final and override appear at the absolute end of a method declaration signature.