I don't understand why would you use C if you know C++, but the question itself is pretty interesting.
Here is the thing:
you can simulate or implement object-oriented style of programming even in Assembly language. From the syntactic point of view, it would look pretty ugly, but the technology of development will be really object-oriented, just not so
fool-proof as in a good object-oriented language.
You don't have classes, but you can use C structures (
struct
). You cannot define the methods inside the structure, but it is not so important: you can just use a separate header and a separate C file for the set of methods working on a certain structure type. What is more important and more OOP-like: all your methods should have the very first parameter of the structure-by-reference or pointer-to-structure type. This will closely match the invisible parameter of the OOP
instant method called "this" ("self" is some other languages).
This is the way you can simulate
encapsulation. Simulation of
inheritance will be much more ugly: as an option, you should implement a container of pointer of type "extensions", simulated.
You did not ask about it, but how about simulation of the very essence of OOP — virtual methods and late binding? You can do it, too! You will need to create a structure simulating
virtual method table (virtual table). You can implement it as a list or array of pointers and organize indirect calls via each pointer. This is more difficult exercise but quite doable. I would say, I'll only be able to tell you that you know C well if you can do it. :-)
There is one what to see how such object-oriented C "features" are simulated automatically by Microsoft software. Use the IDL language used to create interfaces for COM components (not just COM, please see
http://msdn.microsoft.com/en-us/library/windows/desktop/aa367091%28v=vs.85%29.aspx[
^] vs.
http://www.omg.org/gettingstarted/omg_idl.htm[
^]). Get some IDL code with interfaces or create your own and compile it into C code using MIDL compiler (
http://msdn.microsoft.com/en-us/library/windows/desktop/aa367300%28v=vs.85%29.aspx[
^]).
Learn this auto-generated code — you will get a very good source of idea on how to implement OOP in C!
—SA