Abstract factory pattern c # generics wo

Why is C not considered an object-oriented language?

It seems that C has its own quasi-objects like "structures" that can be thought of as objects (on the higher level as we would normally think of it).

And C files themselves are basically separate "modules" too, right? Aren't modules then also something like 'objects'? I'm confused why C, which seems so similar to C ++, is viewed as a low-level "procedural" language while high-level C ++ is "object-oriented".

* edit: (clarification) why and where is the line drawn, for what is an 'object' and what is not?


It seems that C has its own quasi-objects like "structures" that can be thought of as objects

Let's read the Wikipedia page on object-oriented programming together and check off the features of C-style structures that correspond to what is traditionally considered to be object-oriented style:

(OOP) is a programming paradigm that uses "objects" - data structures made up of data fields and methods along with their interactions

C structures consist of fields and methods with their interactions ? No.

Programming techniques can include features such as data abstraction, encapsulation, messaging, modularity, polymorphism, and inheritance.

Do C-Structures do all of these things "first class"? No, the language works against you every step of the way.

The object-oriented approach encourages the programmer to put data in places that the rest of the program cannot directly access

Do C structs do this? No.

An object-oriented program usually contains several types of objects, each type corresponding to a particular type of complex data to be managed, or perhaps to a real-world object or concept

Do C structs do this? Yes.

It can be envisioned that objects wrap their data in a number of functions designed to ensure that the data is used properly


Each object can receive messages, process data, and send messages to other objects

Can a structure send and receive messages by itself? Can it process data? No.

OOP data structures tend to "have their own operators"

Does this happen in C? No.

Dynamic dispatch ... encapsulation ... subtype polymorphism ... object inheritance ... open recursion ... classes of objects ... instances of classes ... methods that act on the attached objects ... message transfer .. abstraction

Are there any of these characteristics of C structures? No.

Which properties of structures do you consider "object-oriented"? Because I Nothing find something else can than the fact that structures Types define.

Now, of course, you can create structures with fields that reference functions. You can add arrays to structures that are pointers to arrays of function pointers that correspond to virtual method tables. And so on. You can of course use C ++ in C emulate . But that's a very unidiomatic way of programming in C. You would be better off just using C ++.

And C files themselves are basically separate "modules" too, right? Aren't modules then also something like 'objects'?

Which properties of modules do you think of that make them behave like objects? Do modules support abstraction, encapsulation, messaging, modularity, polymorphism and inheritance?

Abstraction and encapsulation are pretty weak. Obviously, modules are built up modularly; That is why they are called modules. Messaging? Just in the sense that a method call is a message and modules can contain methods. Polymorphism? No Heritage? No Modules are pretty poor candidates for "objects".

The keyword is "oriented", not "object". Even C ++ code that uses objects but uses them like structures is not object-oriented .

C and C ++ can both do OOP (aside from no access control in C), but the syntax for doing this is inconvenient (to say the least), while the syntax in C ++ makes it very inviting. C is procedurally oriented, while C ++ is object-oriented despite almost identical core capabilities in this regard.

Code that uses objects to implement designs that can only be done on objects (which usually means that polymorphism is being exploited) is object-oriented code. Code that uses objects as little as datasets, even when inheritance is used in an object-oriented language, is really just procedural code, which is more complicated than it needs to be. Code in C that uses function pointers that are modified at run time with structures full of data creates a polymorphism and can even be called "object-oriented" in a procedural language.

Based on the highest principles:

An object is an encapsulation of data and behavior in an interrelated way so that they work as a whole that can be instantiated multiple times and manipulated as a black box if you know the external interface.

Structures contain data but no behavior and therefore cannot be viewed as objects.

Modules contain both behavior and data, but are not encapsulated in such a way that the two are related and it is certain that they cannot be instantiated more than once.

And that's before you get to inheritance and polymorphism ...

"structs" are just data. The usual quick and dirty test of "object orientation" is, "Is there a structure in which code and data can be encapsulated as a unit?" C fails because of this and is therefore procedural. C ++ passes this test.

C, like C ++, has the capability of providing data abstraction, which is an object-oriented programming language paradigm that existed before it.

  • C structures can have data (and that is their main purpose)
  • C structures can also define function pointers as data
  • C-Structures can and often are linked to a number of functions, just like methods. Just this Pointer is not passed implicitly. However, you must explicitly specify it as the first argument of any method designed to handle the specified structure. C ++ does this for you automatically when you use class / structure methods define and call .

OOP in C ++ extends the possibilities of abstracting data. Some say it is harmful while others consider it a good tool when used properly.

  • C ++ implies this Pointer in that the user does not have to pass it to "methods of the class / structure" as long as the type can be (at least partially) identified.
  • With C ++ you can restrict the access to certain methods (class functions) and thus make "defensive programming" or "foolproof".
  • C ++ promotes abstractions by introducing stronger type safety
    1. The new Operator instead of malloc + cast
    2. templates instead of invalid pointers
    3. Inline functions that instead of Macros Receive typed values
    4. Built-in Polymorphism, which you don't have to implement yourself, with which you Abstraction hierarchies , contracts and Specializations can create.

However, you will find many C hackers preaching that C can do just the right amount of abstraction, and that the overhead that C ++ creates is only holding them back from solving the real problem.

Inefficient abstract programming models that two years later you notice that some abstractions weren't very efficient, but now all of your code depends on all of the nice object models that surround it and you can't fix it without rewriting your app . - Linus Torvalds

Others tend to look at it in a more balanced way, accepting both the advantages and the disadvantages.

C makes it easy to shoot yourself in the foot; C ++ makes it harder, but doing this will blow your whole leg off. - Bjarne Stroustrup

You have to look at the other side of the coin: C ++.

In OOP, we think of an abstract object (and design the program accordingly), for example a car that can stop, accelerate, turn left or right, etc. A structure with a bunch of functions just doesn't fit the concept.

In the case of "real" objects, for example, we have to hide the members, or we can also have an inheritance with a real "is a" relationship and much more.

AFTER READING THE COMMENTS: Well, it's true that (almost) anything can be done with C (that's always true), but at first glance I thought that what separates c from c ++ is the way you design think of a program.

The only thing that really makes the difference is the policy imposed by the Compiler . ie purely virtual function, and so on. However, this answer will only cover technical issues, but I think the main difference (as mentioned) is the original way you think when you code, as C ++ gives you better built-in syntax for such things, rather than OOP to do in a somewhat awkward way in C.

You said it yourself. While C has things that are similar to objects, they are still not objects, and therefore C is not considered an OOP language.

Object-oriented refers to both an architectural pattern (or even a meta-pattern) and the languages ​​that contain functionality that is helpful in implementing or using that pattern.

You can implement an "OO" design (Gnome desktop is perhaps the best example of OO in pure C). I've even seen this with COBOL!

However, being able to implement an OO design dose does not do the OO language. Purists would argue that Java and C ++ aren't really OO because you can't override or inherit the basic "types" like "int" and "char", and Java doesn't support multiple inheritance. However, because they are the most widely used OO languages ​​and support most of the paradigms, they are considered OO languages ​​by most "real" programmers who are paid to create work code.

C, on the other hand, only supports structures (like COBOL, Pascal, and dozens of other procedural languages). You could argue that multiple inheritance is supported by allowing you to use any function on any data item, but most would view this as an error rather than a function.

Let's just look at the definition of OO:

  • Messaging
  • encapsulation
  • Late binding

C does not offer any of these three. In particular, will Messaging Not provided what is most important.

There are a number of main components to OO, but the big ones are that the majority of the code doesn't know what's in an object (they see the UI, not the implementation) that the state of an object is a managed unit (i.e. when the object ceases to be, so does its state), and when code calls an operation on an object, they do so without knowing exactly what that operation is or involves (all they do is to follow a pattern to throw a) "message" over the wall).

C does the encapsulation just fine; Code that does not recognize the definition of a structure cannot (rightly) see into it. All you have to do is add a definition like this to a header file:

Of course, a function is needed that creates the s (ie a factory) and delegates some of the work to the assigned object itself (ie via a "constructor" method) and also has a way of disposing of the object again (while it is over its "destructor" method is cleaned up) but those are details.

The method dispatch (ie the dispatch of messages) can also take place via the convention that the first element of the structure is actually a pointer to a structure full of function pointers and that each of these function pointers must use one as the first argument. When sending, the function must then be looked up and rewritten with the correct argument, which is not so difficult to do with a macro and a little cunning. (This function table is the core of what a class really is in a language like C ++.)

In addition, this also means late binding: the shipping code only knows that it is calling a certain offset in a table that the object points to. This only needs to be set during the assignment and initialization of the object. It's possible to go with even more complex shipping schemes that give you more runtime dynamics (at the price of speed), but they're cherries above the basic mechanism.

However, this does not mean that C is an OO language. The key is that C lets you do all of the tricky work, writing the conventions, and the shipping mechanism yourself (or uses a third-party library). That is a lot of work. It also doesn't provide syntactic or semantic support, so implementing a full class system (with things like inheritance) would be unnecessarily painful. If you are dealing with a complex problem that is well described by an OO model, an OO language is very helpful in writing the solution. The added complexity can be justified.

I think C is perfectly fine and decent to implement object oriented concepts, shrug . Most of what I believe to be the differences between the subset of object-oriented languages ​​with a common denominator are, from my pragmatic point of view, minor and syntactic.

For example, let's start by hiding information. In C, we can do this by simply hiding the definition of a structure and working with opaque pointers. This effectively models the vs. distinction of data fields as we get them with classes. And it's simple enough and hardly anti-idiomatic as the standard C library relies heavily on hiding information.

Of course, you lose the ability to easily control the structure's exact memory allocation using opaque types, but that's just a notable difference between, say, C and C ++. C ++ is definitely a superior tool when it comes to programming object-oriented concepts using C while maintaining control over memory layouts. However, this does not necessarily mean that Java or C # C are superior in this regard, as these two features convince you to completely lose the ability to control where objects are mapped in memory.

And we have to use a syntax that is different from Big Whoop's. Who cares? Maybe just someone who relies heavily on autocomplete in their IDE. I admit that this can be a very useful feature from a practical standpoint, but may not be one that requires discussion of whether a language is suitable for OOP.

We lack the ability to implement fields effectively. I will totally submit there. However, I don't think there is a specific rule that says: " All OO languages ​​should have a feature that allows subclasses to access members of a base class that normal clients should still not be able to access "Also, I rarely see use cases for Protected Members who are at least not a little suspicious of becoming a maintenance hurdle.

And of course we have to "emulate" the OO polymorphism with tables of function pointers and pointers to them in order to enable a dynamic dispatch with a little more boilerplate, around this analog and, but a little boilerplate never caused me much suffering.

Inheritance is similar. We can easily model this through composition, and it boils down to the same thing in the way compilers work internally. Of course we lose them Type security when we have one Run a downcast would like, and I would say if you have one at all Run a downcast don't use C for it, as the things people do in C make a Downcast to emulate be terrible may be safety standpoint, but i want people not at all are dejected . Type safety is something that you can easily overlook in C because the compiler gives so much leeway to interpret things like bits and bytes, which forfeits the ability to catch possible errors at compile time, but some languages ​​considered object-oriented aren't even static typed.

So I don't know, I think it's fine.Of course, I wouldn't use C to create a large code base that adheres to the SOLID principles, but it's not necessarily because of the flaws in the object-oriented front. Many of the features I would miss if I tried to use C for such a purpose would be related to language features that are not directly considered a requirement for OOP, such as: B. Strong type safety, destructors that are automatically called when objects go out of scope, operator overload, templates / generics and exception handling. It's when I miss the extra features I get for C ++.

Not a bad question.

If you call c an OO language, you will need to call almost all procedural languages ​​OO too. So it would make the term meaningless. c has no language support for OO. If has structures, but structures are not classes.

In fact, c doesn't have many functions at all compared to most languages. It's mainly used for its speed, simplicity, popularity, and support, including tons of libraries.

We use cookies and other tracking technologies to improve your browsing experience on our website, to show you personalized content and targeted ads, to analyze our website traffic, and to understand where our visitors are coming from.

By continuing, you consent to our use of cookies and other tracking technologies and affirm you're at least 16 years old or have consent from a parent or guardian.

You can read details in our Cookie policy and Privacy policy.