This assignment simulates object-oriented programming using C structs and function pointers to understand how Python's class system works at the implementation level.
object_simulator.c: Assignment file with TODO sections to completeREADME.md: This file
Complete all TODO sections in object_simulator.c:
- Part 1: Base Character Struct - Define the Character struct with data and function pointers
- Part 2: Character Methods - Implement functions for character behavior
- Part 3: Derived Character Types - Define Warrior and Mage structs using composition
- Part 4: Polymorphic Methods - Implement specialized behavior for each character type
Mac/Linux:
gcc object_simulator.c -o object_simulator
./object_simulatorWindows:
gcc object_simulator.c -o object_simulator.exe
object_simulator.exeYour completed program should produce output showing:
- Basic Character creation and attacks
- Warrior with specialized sword attack
- Mage with mana-based fireball attack
- Polymorphism demonstration where the same function call produces different behavior
In Python, you define classes with data and methods. In C, you use structs containing:
- Data fields (like
name,health) - Function pointers (simulating methods like
attack())
Function pointers let you store references to functions in structs:
void (*attack)(struct Character* self);This simulates Python's method binding where hero.attack() calls the appropriate function.
To simulate inheritance, embed the base struct as the first member:
typedef struct Warrior {
Character base; // "Inherits" Character
int strength; // Warrior-specific data
} Warrior;Same function call, different behavior based on object type:
hero.attack(&hero); // Calls character_attack
warrior.attack(&warrior); // Calls warrior_attackSubmit:
- GitHub repository URL with your completed
object_simulator.c - Reflection document (
reflection.txtorreflection.md) with 500-600 words