Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I learn to implement OOP in C? Are there projects that use OOP in C?

Tags:

c

oop

Recently, I finished reading K&R with its, almost all, exercises and examples. I was planning to move to "Accelerated C++" that I came across Axel Schreiner's book OOP with ANSI-C.

I am intrigued and want to learn it. But before investing time in it, I want to know the worth of implementing OOP in C. So that I can decide how much time I must spend on it.

  1. Implementing OOP in C, is it really used? Or its just for mental exercise?
  2. Are their any existing C projects that use OOP?
  3. When it is a good idea to use OOP in C?
  4. Should I invest my time in it?

I think its appropriate that I mention my background here so that you guys can guide me in a better way. I finished C, C++, Java and OOP theory about a year ago, have got a job too. But then Joel's blog and SO made me realize that I lack in a lot many things. So I picked up the books again and start studying them properly.

K&R, Accelerated C++, Algorithm in C++ and some other books are my attempt to to improve my skills. I am not new to OOP.
So what would you suggest?

Thanks for your time.

like image 375
Andrew-Dufresne Avatar asked Jul 24 '10 13:07

Andrew-Dufresne


People also ask

Can you implement OOP in C?

Object-Oriented Programming in C Although the fundamental OOP concepts have been traditionally associated with object-oriented languages, such as Smalltalk, C++, or Java, you can implement them in almost any programming language including portable, standard-compliant C (ISO-C90 Standard).

When should you not use OOP?

These include: design patterns, abstraction, encapsulation, modularity, polymorphism, and inheritance. When not to use OOP: Putting square pegs in round holes: Don't wrap everything in classes when they don't need to be. Sometimes there is no need and the extra overhead just makes your code slower and more complex.

Which language is best for OOPs concepts?

Java is one of the best and most widely-used OOP in the market today. Java has come a long way and is widely known for its implementation and strategic development.

Is Object-Oriented Programming dying?

Object-oriented programming has fulfilled many of its promises. Software systems today are longer-lived and more amenable to change and extension than ever. Nevertheless we observe that object orientation is slowly dying, with the introduction of ever more complex and heterogeneous systems.


2 Answers

OO is used in C as often as needed. Generally I don't agree with the opinion that one cannot do OOP in C, as soon as you provide set of functions that operate on a given type you have OOP. Take for example, you decide to create a data structure. If you provide functions to create, add, remove, and find elements of the data structure it's OO. Generally other languages provide syntactical sugar by automatically implying an instance variable and scoping in various properties of that instance automatically.

  1. As far as "is it really used", the answer is yes. It's not for mental exercise, it's a valid paradigm in C.
  2. The best example that comes to mind is GObject, used by GLib, GTK+ and many projects unrelated to GNOME. GObject provides a way of building objects in C. However it's not necessary to use third party support to have OO in C. Many existing projects have it, although it may not be present in the interface (a great thing in my opinion), and used internally for various purposes (cleanliness, data protection, all the usual OO justifications).
  3. It is a good idea to use OOP in C whenever you find the need to group behaviours and/or data. When you can justify the little extra syntactic expense in using your objects interface, and the time spent not actually completing your solution. Don't get sidetracked.
  4. You should not waste time learning OO because you think it's superior, rather it should complement your future solutions, and you should add it to your toolkit. Use it when it seems the right thing to do. Become familiar with how one does OO in C, the best way would be to do some C++, or examine any good project that uses a little C-style OO. It will seem natural to you after that.
like image 84
Matt Joiner Avatar answered Oct 15 '22 08:10

Matt Joiner


Implementing OOP in C, is it really used? Or its just for mental exercise?

Yes, it's really used, but unsurprisingly, it's not as common as OOP in languages that were designed for it.

Are their any existing C projects that use OOP?

I can remember using a couple:

  • GLib GObject System
  • GTK+ GUI framework

When it is a good idea to use OOP in C?

Some problems are very well modeled by OOP. GUIs, for example, are often designed with inheritance between window types, using polymorphism to override and specialize behavior.

You should use OOP whenever your problem has a beautiful OOP solution. With the footnote that you shouldn't stick 100 lines of OOP code in a 15000 line project, just because it's "kewl"! :)

Should I invest my time in it?

That really depends on what you plan to do with it. I'd never advocate not learning anything - it's great to get exposure to how, why and when to use it. It's great to see a language that wasn't designed for OOP support OOP. You read the book, so I assume you have some affinity to C and interest in OOP. Give it a shot, maybe throw together some GUI applications in GTK+. On the other hand, if you're looking to become the next design patterns master web developer, it's probably not the best approach, you may consider other languages that are more focused toward that area.

So what would you suggest?

I'd suggest you use the best tools at your disposal that fit the problems you're trying to solve. Decide what problem you're going to solve. A GUI desktop app, a web server, a game, a cmd line utility... once you decide on your problem, you'll be able to better decide which technology fits for developing a solution. I've found that you can't really master anything with "toys", so you need to be doing something real.

like image 40
Stephen Avatar answered Oct 15 '22 09:10

Stephen