Skip to content

Instantly share code, notes, and snippets.

@Plasmoxy
Last active May 19, 2021 07:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Plasmoxy/4a4468b7d132f80000f2a8367f389103 to your computer and use it in GitHub Desktop.
Save Plasmoxy/4a4468b7d132f80000f2a8367f389103 to your computer and use it in GitHub Desktop.
polymorphism simulation in c
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct __attribute__((__packed__)) greetable {
void (*greet)(struct greetable* this);
} Greetable; // interface
typedef struct __attribute__((__packed__)) person {
void (*greet)(struct person* this);
char* name;
} Person; // class
typedef struct __attribute__((__packed__)) worker {
void (*greet)(struct worker* this);
char* name;
int workedHours;
} Worker; // class extends Person
typedef struct __attribute__((__packed__)) teacher {
void (*greet)(struct teacher* this);
char* name;
int teachedHours;
int students;
} Teacher; // class extends Person
// methods.
void person_greet(Person* this) { printf("Hello im - %s\n", this->name); }
void worker_greet(Worker* this) {
printf("Hello im a worker %s and i worked %d hours\n", this->name, this->workedHours);
}
void teacher_greet(Teacher* this) {
printf("Hello im teacher %s with %d students, teached for %d hours.\n", this->name, this->students, this->teachedHours);
}
// constructors
Person* person_new(char* name) {
Person* p = calloc(1, sizeof(Person));
p->name = malloc(strlen(name)+1);
strcpy(p->name, name);
p->greet = person_greet;
return p;
}
Teacher* teacher_new(char* name, int teachedHours, int students) {
Teacher* t = calloc(1, sizeof(Teacher));
Person* super = person_new(name); // super(name)
memcpy(t, super, sizeof(Person)); free(super);
t->teachedHours = teachedHours;
t->students = students;
t->greet = teacher_greet;
return t;
}
Worker* worker_new(char* name, int workedHours) {
Worker* t = calloc(1, sizeof(Worker));
Person* super = person_new(name); // super(name)
memcpy(t, super, sizeof(Person)); free(super);
t->workedHours = workedHours;
t->greet = worker_greet;
return t;
}
int main()
{
printf("Through person polymorphism:\n");
Person* people[] = {
(Person*) person_new("Jozo"),
(Person*) teacher_new("Jurgen", 69, 16),
(Person*) worker_new("Vaclav", 420)
};
for (int i = 0; i < 3; i++) {
people[i]->greet(people[i]);
}
printf("\nGreetable polymorphism cast:\n");
Greetable* greeters[3]; // cast array
for (int i = 0; i < 3; i++) greeters[i] = (Greetable*) people[i];
for (int i = 0; i < 3; i++) {
greeters[i]->greet(greeters[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment