Is the C programming language object-oriented?

后端 未结 16 1163
我寻月下人不归
我寻月下人不归 2021-01-30 16:22

I was talking with a co-worker about C and C++ and he claimed that C is object-oriented, but I claimed that it was not. I know that you can do object-oriented-like things in C,

相关标签:
16条回答
  • 2021-01-30 16:56

    Real programmers can write object-oriented code in ANY language.

    I have seen Object Oriented Cobol. Cobol that calls Cobol. Do you want to call these programmers "Real"?

    0 讨论(0)
  • 2021-01-30 16:57

    C is a object based language, it does not support many features of object oriented languages such as inheritance, polymorphism etc.

    0 讨论(0)
  • 2021-01-30 16:58
    1. C is not object oriented in strict sense since it doesn't have a built-in syntax supported object oriented capability like class, inheritance and so on.

    But if you know the trick you can easily add object oriented capability to it simply using struct, function pointer, & self-pointer. DirectFB is such a C library written in an object oriented way. The bad thing it is more error prone since it is not governed by syntax and compile type checking. It is based on coding convention instead.

    e.g.

      IDirectFB/*a typedef of a struct*/ *dfb = NULL;
      IDirectFBSurface/*another typedef of a struct*/ *primary = NULL;
      DirectFBCreate (&dfb); /*factory method to create a struct (e.g. dfb) with 
                             pointers to function and data. This struct is 
                             like an object/instance of a class in a language with build-in 
                             syntax support for object oriented capability  */
      dfb->SetCooperativeLevel/*function pointer*/ 
              (dfb/*self pointer to the object dfb*/, 
               DFSCL_FULLSCREEN);
      dsc.flags = DSDESC_CAPS;
      dsc.caps  = DSCAPS_PRIMARY | DSCAPS_FLIPPING;
      dfb->CreateSurface/*function pointer, also a factory method 
                           to create another object/instance */
              ( dfb/*self pointer to the object dfb*/, 
                &dsc, 
                &primary/*another struct work as object of another class created*/ );
      primary->GetSize/*function pointer*/ 
                  (primary/*self pointer to the object primary*/, 
                   &screen_width, 
                   &screen_height);
    

    2 . C++ is object oriented since it has built-in support for object oriented capability like class and inheritance. But there is argument that it is not a full or pure object oriented language since it does allow C syntax (structural programming syntax) in it. I also remember that C++ lack a few object oriented capabilities but not remember each one exactly.

    0 讨论(0)
  • 2021-01-30 16:59

    You can program in an object-orientated style in more or less any language. (I think runtime polymorphism -- i.e. virtual methods -- requires a language that supports function pointers.)

    Here are a couple of examples:

    • A short summary of object-orientated style in C: http://www.emilmont.net/doku.php?id=c:object_oriented_c
    • A comparison between the same program written in C and C++: http://www.eventhelix.com/realtimemantra/basics/object_oriented_programming_in_c.htm
    0 讨论(0)
  • 2021-01-30 17:04

    C is not object oriented language. C is a general-purpose, imperative language, supporting structured programming. Because C isn't object oriented therefore C++ came into existence in order to have OOPs feature and OOP is a programming language model organized around objects. A language in order to have OOPs feature needs to implement certain principles of OOPs.Few of them are Inheritance, Polymorphism, Abstraction , Encapsulation.

    0 讨论(0)
  • 2021-01-30 17:04

    C is not object oriented. C++ is not object oriented. Let me explain: Object Oriented is an extension to Simula's old fashion event driven subset. Real Object Oriented are functional and reflective because Object Oriented is really a group of paradigms (event driven, functional, reflective). Only four language are really object oriented and these are Lisp,Smalltalk,Ruby and Ocaml. Perl lags between because its not functional. Scala is not reflective so also lags behind. C++ is only event driven with some Simula like facilities but its completely structured programming language and its not declarative or even matchs real world. Object Oriented matchs real world with Functional (Maths), Event Driven (Conversations) and Reflectiveness (Evolution). C++ only has conversations. C++ is not declarative like maths or doesnt evolve like life. C++ only converses like people. C++ is like an idiot that doesnt know how maths work or how life evolves.

    0 讨论(0)
提交回复
热议问题