Click here to Skip to main content
15,892,804 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
See more:
Hi All,
I am C++/VC++ developer, I know C as well. For design in C++ or C# we have classes. We can sepreate and abstract our logic on the basis of Class. Now there is nothing like class in 'C language'. We have to be dependant on functions only for all the design purpose. Can you please suggest any way or link to so I can design software in C language in better way.

Thanks and Regards
Posted
Comments
Sergey Alexandrovich Kryukov 19-Jan-12 2:23am    
Hi Pranit,

Maybe this question is naive, but in essence it is very interesting, as it helps to take a fresh and deep look on OOP.

I've voted 5 for the question, which happens quite rarely!
--SA
[no name] 19-Jan-12 2:35am    
Thanks Sergey!

1 solution

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
 
Share this answer
 
v2
Comments
[no name] 19-Jan-12 2:28am    
Thanks for answer.5! Actually I am working on one C project thats why I wanted to make design in C.
Sergey Alexandrovich Kryukov 19-Jan-12 2:41am    
You are very welcome. Too bad the format of Quick Questions & Answers and especially limited time won't allow to elaborate it in more detail, code samples, etc.

Good luck, call again.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900