|
Inheritance vs Interface Polymorphism (C)
Doxygen docs for the C implementation
|
#include <stdio.h>
Go to the source code of this file.
Classes | |
| struct | Animal |
| Base "class" simulated as a struct holding a speak function pointer. More... | |
| struct | Dog |
| "Subclass" of Animal that barks. More... | |
| struct | Cat |
| "Subclass" of Animal that meows. More... | |
| struct | AbstractAnimal |
| Abstract base "class" combining shared state and an abstract speak function pointer. More... | |
| struct | DogWithDefault |
| Concrete AbstractAnimal "subclass" that implements speak(). More... | |
| struct | ISpeaker |
| Interface contract simulated as a struct holding only a function pointer. More... | |
| struct | InterfaceDog |
| ISpeaker implementation that barks. More... | |
| struct | InterfaceCat |
| ISpeaker implementation that meows. More... | |
| struct | PureBreedDog |
| The sole concrete "speaker" type; no vtable is needed. More... | |
Typedefs | |
| typedef struct Animal | Animal |
| Base "class" simulated as a struct holding a speak function pointer. | |
| typedef struct AbstractAnimal | AbstractAnimal |
| Abstract base "class" combining shared state and an abstract speak function pointer. | |
| typedef struct ISpeaker | ISpeaker |
| Interface contract simulated as a struct holding only a function pointer. | |
Functions | |
| void | dogSpeak (Animal *self) |
| speak() implementation for Dog. | |
| void | catSpeak (Animal *self) |
| speak() implementation for Cat. | |
| void | Dog_init (Dog *d) |
| Initializes a Dog instance, wiring its vtable pointer to dogSpeak. | |
| void | Cat_init (Cat *c) |
| Initializes a Cat instance, wiring its vtable pointer to catSpeak. | |
| void | makeSpeak (Animal *a) |
| Dynamic Dispatch via manual function-pointer vtable. | |
| void | Animal_sleep (AbstractAnimal *aa) |
| A concrete default function shared by all "subclasses". | |
| void | dogWithDefaultSpeak (AbstractAnimal *self) |
| speak() implementation for DogWithDefault. | |
| void | DogWithDefault_init (DogWithDefault *d) |
| Initializes a DogWithDefault instance. | |
| void | makeSleep (AbstractAnimal *aa) |
| Dynamic Dispatch using abstract parent pointer. | |
| void | interfaceDogSpeak (ISpeaker *self) |
| speak() implementation for InterfaceDog. | |
| void | interfaceCatSpeak (ISpeaker *self) |
| speak() implementation for InterfaceCat. | |
| void | InterfaceDog_init (InterfaceDog *d) |
| Initializes an InterfaceDog instance. | |
| void | InterfaceCat_init (InterfaceCat *c) |
| Initializes an InterfaceCat instance. | |
| void | makeSpeakInterface (ISpeaker *s) |
| Contractual Dispatch via manual function-pointer lookup. | |
| void | PureBreedDog_speak (PureBreedDog *self) |
| Direct, non-polymorphic speak implementation for PureBreedDog. | |
| void | directSpeak (PureBreedDog *explicitDog) |
| Scenario A: Monomorphic target - direct call, no vtable indirection. | |
| int | main () |
| Entry point exercising all four polymorphism scenarios. | |
| typedef struct AbstractAnimal AbstractAnimal |
Abstract base "class" combining shared state and an abstract speak function pointer.
| typedef struct Animal Animal |
Base "class" simulated as a struct holding a speak function pointer.
| typedef struct ISpeaker ISpeaker |
Interface contract simulated as a struct holding only a function pointer.
| void Animal_sleep | ( | AbstractAnimal * | aa | ) |
A concrete default function shared by all "subclasses".
| aa | Pointer to the AbstractAnimal base. |
Definition at line 84 of file C Implimentation.c.
| void Cat_init | ( | Cat * | c | ) |
Initializes a Cat instance, wiring its vtable pointer to catSpeak.
| c | Pointer to the Cat instance to initialize. |
Definition at line 55 of file C Implimentation.c.
| void catSpeak | ( | Animal * | self | ) |
speak() implementation for Cat.
Definition at line 35 of file C Implimentation.c.
| void directSpeak | ( | PureBreedDog * | explicitDog | ) |
Scenario A: Monomorphic target - direct call, no vtable indirection.
| explicitDog | Pointer to the concrete PureBreedDog instance. |
Definition at line 196 of file C Implimentation.c.
| void Dog_init | ( | Dog * | d | ) |
Initializes a Dog instance, wiring its vtable pointer to dogSpeak.
| d | Pointer to the Dog instance to initialize. |
Definition at line 49 of file C Implimentation.c.
| void dogSpeak | ( | Animal * | self | ) |
speak() implementation for Dog.
Definition at line 26 of file C Implimentation.c.
| void DogWithDefault_init | ( | DogWithDefault * | d | ) |
Initializes a DogWithDefault instance.
| d | Pointer to the DogWithDefault instance to initialize. |
Definition at line 105 of file C Implimentation.c.
| void dogWithDefaultSpeak | ( | AbstractAnimal * | self | ) |
speak() implementation for DogWithDefault.
| self | Pointer to the AbstractAnimal base within a DogWithDefault instance. |
Definition at line 93 of file C Implimentation.c.
| void InterfaceCat_init | ( | InterfaceCat * | c | ) |
Initializes an InterfaceCat instance.
| c | Pointer to the InterfaceCat instance to initialize. |
Definition at line 164 of file C Implimentation.c.
| void interfaceCatSpeak | ( | ISpeaker * | self | ) |
speak() implementation for InterfaceCat.
| self | Pointer to the ISpeaker base within an InterfaceCat instance. |
Definition at line 144 of file C Implimentation.c.
| void InterfaceDog_init | ( | InterfaceDog * | d | ) |
Initializes an InterfaceDog instance.
| d | Pointer to the InterfaceDog instance to initialize. |
Definition at line 158 of file C Implimentation.c.
| void interfaceDogSpeak | ( | ISpeaker * | self | ) |
speak() implementation for InterfaceDog.
| self | Pointer to the ISpeaker base within an InterfaceDog instance. |
Definition at line 135 of file C Implimentation.c.
| int main | ( | ) |
Entry point exercising all four polymorphism scenarios.
Definition at line 204 of file C Implimentation.c.
| void makeSleep | ( | AbstractAnimal * | aa | ) |
Dynamic Dispatch using abstract parent pointer.
| aa | Pointer to the AbstractAnimal base; sleep() is a shared, non-overridden default function. |
Definition at line 115 of file C Implimentation.c.
| void makeSpeak | ( | Animal * | a | ) |
Dynamic Dispatch via manual function-pointer vtable.
| a | Pointer to the Animal base whose speak function pointer is invoked. |
Definition at line 61 of file C Implimentation.c.
| void makeSpeakInterface | ( | ISpeaker * | s | ) |
Contractual Dispatch via manual function-pointer lookup.
| s | Pointer to the ISpeaker contract whose speak function pointer is invoked. |
Definition at line 170 of file C Implimentation.c.
| void PureBreedDog_speak | ( | PureBreedDog * | self | ) |
Direct, non-polymorphic speak implementation for PureBreedDog.
| self | Pointer to the PureBreedDog instance. |
Definition at line 187 of file C Implimentation.c.