Inheritance vs Interface Polymorphism (C++)
Doxygen docs for the C++ implementation
Loading...
Searching...
No Matches
C++ Implimentation.cpp File Reference
#include <iostream>
Include dependency graph for C++ Implimentation.cpp:

Go to the source code of this file.

Classes

class  Animal
 Base class exposing a virtual speak() method. More...
class  Dog
 Animal subclass that overrides speak() to bark. More...
class  Cat
 Animal subclass that overrides speak() to meow. More...
class  AbstractAnimal
 Abstract base class combining shared state, a concrete default method, and a pure virtual method subclasses must implement. More...
class  DogWithDefault
 Concrete AbstractAnimal that implements the required speak() method. More...
class  ISpeaker
 Pure abstract class acting as an interface contract. More...
class  InterfaceDog
 ISpeaker implementation that barks. More...
class  InterfaceCat
 ISpeaker implementation that meows. More...
class  ISingleSpeaker
 Interface contract for a single, non-substitutable speaker. More...
class  PureBreedDog
 Sealed 'final' class ensures NO other subclass can modify this method path. More...

Functions

void makeSpeak (Animal &a)
 Dynamic Dispatch via Class vtable.
void makeSleep (AbstractAnimal &aa)
 Dynamic Dispatch using abstract parent reference.
void makeSpeak (ISpeaker &s)
 Contractual Dispatch via Dynamic vtable Lookups.
void directSpeak (PureBreedDog &explicitDog)
 Scenario A: Monomorphic target explicitly declared by final type reference.
int main ()
 Entry point exercising all four polymorphism scenarios.

Function Documentation

◆ directSpeak()

void directSpeak ( PureBreedDog & explicitDog)

Scenario A: Monomorphic target explicitly declared by final type reference.

Parameters
explicitDogReference to the concrete, non-overridable PureBreedDog type.

Definition at line 172 of file C++ Implimentation.cpp.

◆ main()

int main ( )

Entry point exercising all four polymorphism scenarios.

Returns
0 on successful completion.

Definition at line 180 of file C++ Implimentation.cpp.

◆ makeSleep()

void makeSleep ( AbstractAnimal & aa)

Dynamic Dispatch using abstract parent reference.

Parameters
aaReference to an AbstractAnimal; sleep() is a non-virtual, inherited default method shared by every subclass.

Definition at line 96 of file C++ Implimentation.cpp.

◆ makeSpeak() [1/2]

void makeSpeak ( Animal & a)

Dynamic Dispatch via Class vtable.

Parameters
aReference to a base Animal; the concrete override is resolved at runtime through the object's vtable pointer.

Definition at line 49 of file C++ Implimentation.cpp.

◆ makeSpeak() [2/2]

void makeSpeak ( ISpeaker & s)

Contractual Dispatch via Dynamic vtable Lookups.

Parameters
sReference to the ISpeaker interface contract.

Definition at line 140 of file C++ Implimentation.cpp.