tags:

views:

418

answers:

9

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, but C++ is a true object-oriented language.

What are your thoughts?

Also, it triggered discussion on who decides what it means to be object-oriented and that it's tough to say what object-oriented really officially means. What are you thoughts on this?

+3  A: 

C isn't object oriented. That was the entire purpose behind the ++

As far as a definition of what it takes to be object oriented: check wikipedia.

Personally, if it supports inheritance, encapsulation, and polymorphism then your good to go. Another key here is having nice keywords like class and object tend to help...

Examples of real object oriented languages (not conclusive) are: Smalltalk, Java, c#, Python, Ruby, C++..

Also, it's possible to have extensions to provide OO features like VB.net, PHP, Perl...

Chris Lively
Please explain ... I don't want just plain no for an answer.
Brian T Hannan
If those are ["real"](http://stackoverflow.com/questions/3222316/what-is-a-real-programming-language/3226182#3226182) OO languages, are there fake ones?
Roger Pate
@Roger Pate: the "fake" ones are those that have an OO veneer like vb.net and php. They aren't built up from an OO perspective, but do provide at least some facility for OO development.
Chris Lively
Can you clarify what you mean about the OO veneer for VB.NET?
Jordan
@roger fake OO Language = Mod Perl
Michael Mullany
A: 

No, it is not, your friend is wrong.

Ed Swangren
Please explain ... I don't want just plain no for an answer.
Brian T Hannan
Well, the language does not directly object oriented programming, so it seems pretty obvious to me.
Ed Swangren
"Is the C programming language object-oriented?" "No" "Why?" "Because it is not"
devoured elysium
Well, it's not. Even a cursory understanding of what object oriented programming is about would tell you that,
Ed Swangren
C is no OO since it lacks syntactic sugar and runtime support for OOP? (maybe can be said this way); if you want, you can implement a runtime and use "normal" syntax (or macros or helper functions)... so maybe it can be said it is OO capable, ... but currently no OO features are builtin-embedded.
ShinTakezou
Yup, and if you want, you can pound in nails with a screw driver, but that would just be stupid.
Ed Swangren
+16  A: 

If by "is C object oriented?" you mean "is C designed with facilities specifically to support object oriented programming?" then, no, C is clearly not object oriented.

James McNellis
+1 The only way to say a language is "insert paradigm" is to assert if it facilitates or enforces the paradigm with language constructs.
pmr
+2  A: 

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

But no, C is not an 'object-oriented' language. It has no concept of classes, objects, polymorphism, inheritance.

Roddy
I argee - its possible to write OO code in any language. I recently refactored a legacy C program to divide the modules into namespaces, and put all relevant code and variables together. Its amazing how it increased the maintainability of the program - it had to be seen to be believed (admittedly it required a C++ compiler to support namespaces, but I believe namespaces should be part of C in any case).
Gravitas
+7  A: 

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:

Tim Robinson
I remember a colleague once reading about OO Perl. I skimmed the book, and my soul shuddered
johnc
Re my function pointers comment: I challenge anyone to program OO in BASIC (the original, not this Visual stuff)
Tim Robinson
+1  A: 

The confusion may be that C can be used to implement object oriented concepts like polymorphism, encapsulation, etc. which may lead your friend to believe that C is object oriented. The problem is that to be considered an object oriented programming language, these features would need to be built into the language. Which they are not.

Graphics Noob
A: 

Unless your friend was talking about Objective C (an OO superset of C) then no, C isn't an OO language. You can implement OO concepts using C (that's what the old cfront C++ compiler did, it translated C++ into C) but that doesn't make C an OO language as it doesn't specifically offer support for standard OO techniques like polymorphism or encapsulation.

Yes, you can write software OO style in C, especially with liberal (ab-)use of macros but as someone who has seen the results of some of those attempts, I'd strongly suggest to use a better suited language.

Timo Geusch
A: 

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"?

newbie007
I'm a fake programmer. I don't actually exist and even when I think I might exist my code is fake anyways. Sometimes I wish I was real.
Brian T Hannan
+1  A: 
  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.

ttchong