-7

as far as I know, C is not an object oriented programming language. how can we make the user defined data types and allow some operations to be performed on those data Types.Just like the primitive data types are defined and allow some operations to be performed on them.

Neerg
  • 1
  • 2
  • 5
    because C was invented *before* OOP became popular. C can be viewed as semi-portable assembly language. Anyway, it is possible to apply OOP into C programs. – tstanisl Mar 15 '21 at 11:28
  • thank you, any recommended reading for the later idea(applying OOP in C programns). – Neerg Mar 15 '21 at 11:33
  • 1
    The [GTK](http://gtk.org/) GUI toolkit is open source and shows how to code object oriented programs in C. I recommend to study its documentation and source code. The [GNU guile](https://www.gnu.org/software/guile/) interpreter has object oriented features, and is free software coded in C. I recommend to study its documentation and source code. Budget a full month of work at least. – Basile Starynkevitch Mar 15 '21 at 11:40
  • 1
    A very simple way to achieve abstraction or data hiding is using opaque pointers. I'd suggest you look them up – mediocrevegetable1 Mar 15 '21 at 11:42
  • 2
    You can see https://nullprogram.com/blog/2014/10/21/ . It's about OOP techniques used in Linux Kernel. You must know that one must be quite proficient in C to understand use it correctly and efficiently. – tstanisl Mar 15 '21 at 11:42
  • 1
    @tstanisl That's a naive method, the author is apparently not that experienced since he doesn't mention opaque type, which is the professional way to do it. And AFAIK the Linux kernel didn't use such design much. Mostly because it is stuck with the old dysfunctional Unix APIs. In general I wouldn't recommend anyone to study Linux code for advise about how to program anything else but... Linux. – Lundin Mar 15 '21 at 11:54
  • 1
    @GhislainHABUMUGISHA The recommended way of doing OOP in C is to not do it. Use a language that supports OOP. Like C++ – klutt Mar 15 '21 at 11:54
  • 2
    Re “That left me wondering how are those set of values developed?”: C includes fundamental types that are specified in the language and built into compilers: `char`, `int`, `float`, pointers, and others, and their values follow language rules. Derived types, notably arrays and structures, can be built from those, and their values and meanings derive from code written into programs. There are no constraints on techniques for writing such code; programmers may use OOP techniques or others as they desire. – Eric Postpischil Mar 15 '21 at 12:10

1 Answers1

2

There are no object-oriented languages. There are just languages with different degrees of OO feature support. OO is a program design method, and as such it is mostly language-agnostic.

OO can roughly be summarized in 3 things:

  • Autonomous modules who do their own designated task, with limited knowledge of the surrounding program. ("loose coupling")

  • Private encapsulation of data and functions, so that users of the class need not worry about which parts that are internal and which ones that are part of the API. Prevents accidental or intentional misuse of internals. Reduces "namespace clutter".

  • Inheritance and polymorphism, which can be used for code re-use and API design.

The 1st is pure program design and the one that people most often get wrong. The 2nd and 3rd are supported by C, but implicitly and not very elegantly.

For private encapsulation, you can use static file scope variables. It works fine in some situations like single core embedded systems. But it gets problematic in other situations - it makes your class "singleton" single instance and turns it thread-unsafe.

You can do more proper private encapsulation with the concept of opaque type where you forward declare a struct and then only define that struct in a file not visible to the caller. This gives you true, multi-instance private encapsulation and can also be used for polymorphism, when combined with function pointers. The down side is that it's somewhat cumbersome and not many people are aware of it. Schools fail to teach it.

So it is perfectly possible to do OO programs in C, just as it is perfectly possible to make broken, non-OO designs in C++ or Java. It's just a whole lot easier to do OO when you have language support for it.

For example the C++ concept of "RAII", which isn't strictly speaking an OO feature in itself, but it helps a whole lot when doing OO design, since it gives you implicit constructor/destructor calls and you don't have to rely on the caller doing them explicitly as is the case in C. Another example is the mighty handy this pointer, in C you have to pass it along manually to each function call.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • *"The 2nd and 3rd are supported by C, but implicitly and not very elegantly."* - I would say that even this is a far stretch. I would rather say something like *"It's possible to mimic inheritance and polymorphism in C, but it's very clunky and rarely worth the effort."* – klutt Mar 15 '21 at 11:57
  • I would rather say that reliable encapsulation is difficult. The common way is to add `_` suffix and comment that given member is private. Similar situation is in Python. – tstanisl Mar 15 '21 at 12:16
  • `RAII` can be emulated in C. One has to pack all function pointers into a dedicated structure shared among all members of a given class. Then a pointer to this `ops` struct becomes an identifier of the class. Two objects can check if they belong to the same derived class by comparing their `ops` with `==` operator – tstanisl Mar 15 '21 at 12:20
  • 1
    @klutt It's not difficult at all. For any API you write in C, you are very likely going to pass along a struct anyway. So just make it so that the user has no access to the contents. As for polymorphism, it is rather clunky to implement in _any_ language... simply because if you get the base class wrong, you easily end up with lots of strange interfaces and pointless meta programming. – Lundin Mar 15 '21 at 13:35
  • Example here: https://stackoverflow.com/questions/3824329/partitioning-struct-into-private-and-public-sections – Lundin Mar 15 '21 at 13:35
  • @tstanisl That sounds like inheritance, not RAII (resource acquisition is initialization). And as far as I know, there's no good way to emulate it in C. There's `atexit` but that's hardly the same thing. – Lundin Mar 15 '21 at 13:37
  • 1
    @Lundin, You're right, I had `RTTI` in mind. Sorry for confusion – tstanisl Mar 15 '21 at 13:40
  • @tstanisl RTTI in C is typically done in the very old-fashioned way of passing an enum around with the data. – Lundin Mar 15 '21 at 13:43