How easy it is depends on how many OO concepts you need. If you're simply looking for encapsulation, you can do this style:
Code:
/* dog.h */
struct dog;
/* public methods */
void dog_bark(struct dog *d);
void dog_walk(struct dog *d);
....
/* end dog.h */
/* dog.c */
struct dog {
int legs;
....
};
/* here are my private methods: */
static void scratch(struct dog *d)
{
stuff...
}
/* end private methods */
void dog_bark(struct dog *d)
{
stuff...
}
void dog_walk(struct dog *d)
{
stuff....
scratch(d); /* call to private method */
}
If you need polymorphism, it's easy to include some function pointers in the struct dog, or to include a pointer to a static table of function pointers in the struct dog. (That's more space efficient if dog has more than a couple of public methods.)
If you need single inheritance, you can do that by extending a struct like so:
Code:
struct poodle {
struct dog d; /* this must be the first thing in the poodle struct! */
int poofiness;
};
Then you can call the dog_* methods on a poodle by casting a poodle pointer to (struct dog*). If you're doing polymorphism too, you'd change the function pointers in the d field of the poodle before doing anything with it.
I'll try to explain more of whatever parts of this long ass post you find interesting, if any : ) Clearly it would be beneficial to do some OO programming in a language which actually has syntax for it before trying to fake it in C.