1

This is a question that has haunted me for a long time now.

Is it possible to implement OOP principles like Polymorphism and Inheritance in a non-OO language like C using only procedures and global variables? Do we need special treatment from lower level abstractions for that?

Does this question makes any sense at all?!

Bryan Anderson
  • 15,969
  • 8
  • 68
  • 83
Mehran
  • 1,977
  • 1
  • 18
  • 33

3 Answers3

1

Of course, its possible to use a language like C in an OO manner. Polymorphism can be emulated with pointers to functions (but don't expect to get easy manageable code this way). Perhaps this discussion

http://ootips.org/oop-in-c.html

will help you to get some more insights.

Doc Brown
  • 19,739
  • 7
  • 52
  • 88
1

Is it possible to implement OOP principles like Polymorphism and Inheritance in a none OO language like C?

Yes, but doing it right takes some work and the libraries that do it in C can be quite cumbersome to use due to all the pointers to Foo passed as arguments to function pointers in Foo instances. Check out Berkeley DB and GTK+. (I must admit I've never programmed to GTK+.)

More generally, procedural languages can do OO; those that have closures make it esp. easy since objects are a poor man's closures.

Fred Foo
  • 355,277
  • 75
  • 744
  • 836
  • Hehe. Much appreciated. It is a valid type of response of course. Especially with the other answers. Much reminds me of doing OO in Intel 80286 assembly (interop with Turbo Pascal 5.5 - yeehay). I love lisp. I simply can't read what I wrote; I tend to agree on 'objects are a poor man's closures' only in pure functional style. – sehe May 28 '11 at 22:17
  • @sehe: I don't do much Lisp/Scheme these days, but in both Python and C++ I tend to build lots of callables/functionals, also in non-FP-style code. I'm very glad C++ will finally get closures in the next standard :) – Fred Foo May 28 '11 at 22:45
0

It's definitely possible. Checkout this article by Mark Dominus on the same topic. Mark shows how the basic principles of object oriented programming can be emulated with just C alone.

Theoretically, it should be possible to emulate OO using any Turing complete language. The amount of effort that it would take to do so may vary on the capabilities of the language. Considering we're still using C in spaceships and several other critical systems, emulating OO seems to be a rather trivial problem.

Anurag
  • 140,337
  • 36
  • 221
  • 257