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

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 Documentation

◆ AbstractAnimal

typedef struct AbstractAnimal AbstractAnimal

Abstract base "class" combining shared state and an abstract speak function pointer.

◆ Animal

typedef struct Animal Animal

Base "class" simulated as a struct holding a speak function pointer.

◆ ISpeaker

typedef struct ISpeaker ISpeaker

Interface contract simulated as a struct holding only a function pointer.

Function Documentation

◆ Animal_sleep()

void Animal_sleep ( AbstractAnimal * aa)

A concrete default function shared by all "subclasses".

Parameters
aaPointer to the AbstractAnimal base.

Definition at line 84 of file C Implimentation.c.

◆ Cat_init()

void Cat_init ( Cat * c)

Initializes a Cat instance, wiring its vtable pointer to catSpeak.

Parameters
cPointer to the Cat instance to initialize.

Definition at line 55 of file C Implimentation.c.

◆ catSpeak()

void catSpeak ( Animal * self)

speak() implementation for Cat.

Parameters
selfPointer to the Animal base within a Cat instance.

Definition at line 35 of file C Implimentation.c.

◆ directSpeak()

void directSpeak ( PureBreedDog * explicitDog)

Scenario A: Monomorphic target - direct call, no vtable indirection.

Parameters
explicitDogPointer to the concrete PureBreedDog instance.

Definition at line 196 of file C Implimentation.c.

◆ Dog_init()

void Dog_init ( Dog * d)

Initializes a Dog instance, wiring its vtable pointer to dogSpeak.

Parameters
dPointer to the Dog instance to initialize.

Definition at line 49 of file C Implimentation.c.

◆ dogSpeak()

void dogSpeak ( Animal * self)

speak() implementation for Dog.

Parameters
selfPointer to the Animal base within a Dog instance.

Definition at line 26 of file C Implimentation.c.

◆ DogWithDefault_init()

void DogWithDefault_init ( DogWithDefault * d)

Initializes a DogWithDefault instance.

Parameters
dPointer to the DogWithDefault instance to initialize.

Definition at line 105 of file C Implimentation.c.

◆ dogWithDefaultSpeak()

void dogWithDefaultSpeak ( AbstractAnimal * self)

speak() implementation for DogWithDefault.

Parameters
selfPointer to the AbstractAnimal base within a DogWithDefault instance.

Definition at line 93 of file C Implimentation.c.

◆ InterfaceCat_init()

void InterfaceCat_init ( InterfaceCat * c)

Initializes an InterfaceCat instance.

Parameters
cPointer to the InterfaceCat instance to initialize.

Definition at line 164 of file C Implimentation.c.

◆ interfaceCatSpeak()

void interfaceCatSpeak ( ISpeaker * self)

speak() implementation for InterfaceCat.

Parameters
selfPointer to the ISpeaker base within an InterfaceCat instance.

Definition at line 144 of file C Implimentation.c.

◆ InterfaceDog_init()

void InterfaceDog_init ( InterfaceDog * d)

Initializes an InterfaceDog instance.

Parameters
dPointer to the InterfaceDog instance to initialize.

Definition at line 158 of file C Implimentation.c.

◆ interfaceDogSpeak()

void interfaceDogSpeak ( ISpeaker * self)

speak() implementation for InterfaceDog.

Parameters
selfPointer to the ISpeaker base within an InterfaceDog instance.

Definition at line 135 of file C Implimentation.c.

◆ main()

int main ( )

Entry point exercising all four polymorphism scenarios.

Returns
0 on successful completion.

Definition at line 204 of file C Implimentation.c.

◆ makeSleep()

void makeSleep ( AbstractAnimal * aa)

Dynamic Dispatch using abstract parent pointer.

Parameters
aaPointer to the AbstractAnimal base; sleep() is a shared, non-overridden default function.

Definition at line 115 of file C Implimentation.c.

◆ makeSpeak()

void makeSpeak ( Animal * a)

Dynamic Dispatch via manual function-pointer vtable.

Parameters
aPointer to the Animal base whose speak function pointer is invoked.

Definition at line 61 of file C Implimentation.c.

◆ makeSpeakInterface()

void makeSpeakInterface ( ISpeaker * s)

Contractual Dispatch via manual function-pointer lookup.

Parameters
sPointer to the ISpeaker contract whose speak function pointer is invoked.

Definition at line 170 of file C Implimentation.c.

◆ PureBreedDog_speak()

void PureBreedDog_speak ( PureBreedDog * self)

Direct, non-polymorphic speak implementation for PureBreedDog.

Parameters
selfPointer to the PureBreedDog instance.

Definition at line 187 of file C Implimentation.c.