Skip to content

Instantly share code, notes, and snippets.

@FranklinChen
Created November 7, 2011 02:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FranklinChen/1344060 to your computer and use it in GitHub Desktop.
Save FranklinChen/1344060 to your computer and use it in GitHub Desktop.
Most basic inheritance in C
#include <stdlib.h>
#include <stdio.h>
/* Single inheritance, treating Animal as abstract base class. */
typedef struct Animal_vtable {
void (*eat)(void *);
void (*sound)(void *);
} Animal_vtable;
typedef struct Animal {
Animal_vtable *vtable;
} Animal;
void Animal_eat(Animal *animal) {
animal->vtable->eat(animal);
}
void Animal_sound(Animal *animal) {
animal->vtable->sound(animal);
}
typedef struct Dog {
Animal base;
} Dog;
void Dog_eat(Dog *dog);
void Dog_sound(Dog *dog);
/* Assume Dog does not add more virtual functions. */
Animal_vtable Dog_vtable = {
(void (*)(void *))Dog_eat,
(void (*)(void *))Dog_sound
};
typedef struct Cat {
Animal base;
} Cat;
void Cat_eat(Cat *cat);
void Cat_sound(Cat *cat);
/* Assume Cat does not add more virtual functions. */
Animal_vtable Cat_vtable = {
(void (*)(void *))Cat_eat,
(void (*)(void *))Cat_sound
};
/* Implementations. */
void Dog_eat(Dog *dog) {
printf("Eating dog chow\n");
}
void Dog_sound(Dog *dog) {
printf("Woof!\n");
}
Dog *new_Dog(void) {
Dog *dog = malloc(sizeof(Dog));
dog->base.vtable = &Dog_vtable;
return dog;
}
void Cat_eat(Cat *cat) {
printf("Eating mouse\n");
}
void Cat_sound(Cat *cat) {
printf("Meow!\n");
}
Cat *new_Cat(void) {
Cat *cat = malloc(sizeof(Cat));
cat->base.vtable = &Cat_vtable;
return cat;
}
int main(void) {
Animal *animal = (Animal *)new_Dog();
Animal_eat(animal);
Animal_sound(animal);
animal = (Animal *)new_Cat();
Animal_eat(animal);
Animal_sound(animal);
return 0;
}
#include <iostream>
using namespace std;
class Animal {
public:
virtual void eat() = 0;
virtual void sound() = 0;
};
class Dog : public Animal {
public:
virtual void eat() { cout << "Eating dog chow" << endl; }
virtual void sound() { cout << "Woof!" << endl; }
};
class Cat : public Animal {
public:
virtual void eat() { cout << "Eating mouse" << endl; }
virtual void sound() { cout << "Meow!" << endl; }
};
int main(void) {
Animal *animal = new Dog();
animal->eat();
animal->sound();
animal = new Cat();
animal->eat();
animal->sound();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment