Created
October 26, 2020 17:01
-
-
Save giulioz/ddb2f8362384291f5ef5f37879ac95bb to your computer and use it in GitHub Desktop.
OOP Simulation using C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
// | |
// interface Animale | |
// | |
typedef struct Animale { | |
void (*faiVerso)(); | |
} Animale; | |
// | |
// class Cane | |
// | |
typedef struct Cane { | |
Animale super; | |
} Cane; | |
void Cane_faiVerso(Cane* this) { printf("bau\n"); } | |
Cane* newCane() { | |
Cane* this = malloc(sizeof(Cane)); | |
this->super.faiVerso = Cane_faiVerso; | |
return this; | |
} | |
// | |
// class Gatto | |
// | |
typedef struct Gatto { | |
Animale super; | |
} Gatto; | |
void Gatto_faiVerso(Gatto* this) { printf("miao\n"); } | |
Gatto* newGatto() { | |
Gatto* this = malloc(sizeof(Gatto)); | |
this->super.faiVerso = Gatto_faiVerso; | |
return this; | |
} | |
int main() { | |
Animale* fattoria[4]; | |
fattoria[0] = newGatto(); | |
fattoria[1] = newCane(); | |
fattoria[2] = newGatto(); | |
fattoria[3] = newCane(); | |
for (size_t i = 0; i < 4; i++) { | |
fattoria[i]->faiVerso(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment