Lecture 22 Flashcards
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++
requires
handling objects via their pointers – 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.
public:
declares a field or method is fully visible from any object
private:
places fields or methods on “lockdown,” making them invisible outside of the class.
protected:
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.