Lecture 22 Flashcards Preview

COP3503 > Lecture 22 > Flashcards

Flashcards in Lecture 22 Deck (17):

Polymorphism allows

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.


use of polymorphism in C++

handling objects via their
– A pointer of a subclass can always be stored as
a variable of its superclass.


Polymorphism allows for multiple

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.


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

–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.


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

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


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

–All undefined methods must be implemented for the class to be instantiated.
–These implementations should follow the base class’s specifications.


pure virtual functions allow what?

–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.


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

a base class to a derived class.
–This includes fields and method definitions.


Inheriting from a class means that

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.



declares a field or method is fully visible from any object



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



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.


Declaring another class as a friend grants it

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.


the override keyword

may be appended at the end
of the declaration’s signature


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

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.


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

–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.


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

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.