Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I read somewhere in one of the answers to a question here (can't remember which) that C++ is not suitable for object-oriented programming. There was some mentioning that you could make use of its feature or something like that, but not in a purely OOP sense (I actually didn't really understand what the person meant).

Is there some truth in this; if so, why?

share|improve this question

closed as not constructive by gnat, Jarrod Roberson, Yannis Rizos Jun 6 '12 at 3:35

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.If this question can be reworded to fit the rules in the help center, please edit the question.

    
5  
OOP is not a well-defined term, so discussing whether C++ is or is not suitable is rather pointless. –  zvrba Feb 16 '11 at 7:40
add comment

12 Answers

OOP is not just about making sure everything is of or is in a class. It's perfectly possible to write non-OO code in a "purely OO" language. For example, "main" is often pointed out as being a global function, but inventing a class solely to contain a static main method is just as non-OO.

C++ works best with a mix of various things; this shouldn't be surprising, as that's how most good things work best. Often OOP is one of those very useful tools.

share|improve this answer
add comment

As described at So what *did* Alan Kay really mean by the term "object-oriented"?, Alan Kay thought that message-passing was the important bit of OOP, but it's the bit that "C with classes" (which later became C++) lacks. C++ is just structs with a bit of behaviour, whereas objects in Smalltalk or Objective-C are "intelligent" in that they can decide what they do with the messages they're sent. If a Smalltalk-esque object receives a message it doesn't have an implementation for, it could lazily add one, forward the message to another object, or do any arbitrary thing.

What C++ offers in the way of object-orientation is virtual methods and polymorphism involving the way those methods are invoked. When the compiler sees a data type (or class) that has virtual methods, it constructs a vtable with a slot for each virtual method. Subclasses that implement the virtual methods will put their implementations in the correct slots, so client code merely needs to know where in the virtual table to look for the code to run rather than resolving it all the way down to the specific function. What this means is that C++ effectively does have a form of multiple dispatch, though it's all implemented in the compiler and isn't as capable as a Smalltalk-esque system.

If you take message-passing as fundamental to OOP, then while you can do it with C++ it's far from easy. OTOH if you take OOP to mean associating data with functions that act on that data, C++ is fine.

share|improve this answer
8  
+1 - but I always thought a function call was a reasonable way to pass a message anyway. True, it's a low level foundation, rather than a high level fix for everything - but the active objects pattern shows that it's doable to build up from that foundation. –  Steve314 Feb 15 '11 at 23:14
6  
so this applies not only to C++, but to Java, C#, Object Pascal, etc. And it ends up being that Windows API messaging system is what Alan meant as the most important thing in OOP, specially the way it is handled by Delphi –  Trinidad Feb 16 '11 at 2:01
    
@trinidad correct, except C# actually does support dynamic resolution. I expect it's fair to say that our idea of OOP has changed over time to work around the lack of message-passing. Helped no doubt by vendors saying that their technology is definitely OOP in the face of evidence... –  user4051 Feb 16 '11 at 9:06
    
@steve314 yes it is. Indeed that's how ObjC works, a message send is translated into a function call which looks up and calls the method function. As I understand message-passing, it's the double dispatch that's important. –  user4051 Feb 16 '11 at 9:07
1  
@Paul: Having some limited experience with the Win32 API, I don't understand. Any API where objects are of variable size, and where it's necessary to first call a routine to determine how big the object will be, allocate memory, and call it again, fails my test for beauty. –  David Thornley Feb 16 '11 at 20:46
show 9 more comments

C++ can be used for OOP but it is not as 'pure' as something like Smalltalk. C++ also lets you do non-OOP which is what people may be talking about.

share|improve this answer
add comment

C++ borrowed OOP features from Simula. One or more of the Simula developers IIRC commented that C++ isn't what they had in mind.

C++ has good tools for abstraction, but it's more a mixed-paradigm language than an object oriented language. The object oriented features are there, but you have choices that aren't "strict OOP".

One of the naughty "opt-outs" you get in C++ is to use early rather than late binding for methods. Not only is this possible - it's the default. In Java, "final" is related, but cleaner in some ways (it specifies intent in a way that isn't just about avoiding a trivial performance overhead), and it's not the default.

In some ways, C++ shows signs of being an early experiment that's still here. Even so, it's still a good tool, with a lot of advantages that you don't get in other OOP languages.

share|improve this answer
2  
C++ allows non-OOP and C++ allows OOP, for some language to be OO it has to allow OOP, therefore C++ is an OO language. –  Trinidad Feb 16 '11 at 2:05
1  
I believe Alan Kay, of Smalltalk fame, said C++ wasn't what he had in mind when he coined the term "object-oriented". Since C++ started out as "C with Classes", a grafting of Simula classes onto C, and never made a clean break, it's no wonder that it looks like an early experiment. –  David Thornley Feb 16 '11 at 14:40
1  
@Trinidad: ANY language allows OOP. I've seen quite a lot of nice OO code in plain old C. Yes, it is pain defining all the virtual method tables by hand, but the language clearly allows it. –  Jan Hudec Jun 5 '12 at 13:03
add comment

Although I disagree with the sentiment, it is true that C++'s type system is not pure OOP -- not "everything is an object." Numbers (in particular) cannot be extended as readily as they can in, say, Smalltalk. You cannot redefine the meaning of "2+2" for instance (although you could redefine the meaning of "two + two").

But what most people probably mean is that many people write non-object oriented code in C++ but believe that because they're using an "OOP" language, they're object-oriented. That's not true. But in my opinion, you can write hideous imperative code in Smalltalk and not be any superior to a decent OOP design in C++.

share|improve this answer
add comment

C++ supports OOP, if you define OOP to mean encapsulation, inheritance and polymorphism.

However, C++ doesn't really excel at OOP. One reason is that polymorphism often depends on heap-allocated objects, which, (notwithstanding the use of smart pointers), are more natural to work with in a garbage-collected language.

Where C++ excels, however, is in generic-programming. C++ allows you to easily create highly-efficient, generic code through template-based functional programming techniques.

share|improve this answer
add comment

This kind of discussion bothers me because it sounds like exegesis, people debating the meaning of Holy Scriptute, or the American Constitution, and what the original author(s) meant, as if what we think doesn't matter.

Look, Alan Kay was/is a smart guy, and he had a good idea, that rubbed up against a bunch of other good ideas, and found its realization in Smalltalk and other languages.

He is not the Messiah, and OOP is not the One True Programming Paradigm.

It is a good idea, among many. Does C++ have good ideas in it, coming from the OOP mindset? Of course it does.

share|improve this answer
add comment

Steve Yegge said it best:

C++ is the dumbest language on earth, in the very real sense of being the least sentient. It doesn't know about itself.

The object system in C++ is so hard-wired and fixed at compile time, that it's very distant from the original notion of OOP that involves message-passing, introspection, reflection, dynamic dispatch, and late binding, among other things. The only thing C++ and Smalltalk have in common are a bit of vocabulary.

share|improve this answer
    
In what way is C++ the least sentient language? What does it mean for a language to be sentient? If you mean it lacks reflection capabilities, that's quite common, and certainly doesn't pick C++ out of a crowd. –  David Thornley Feb 16 '11 at 20:50
2  
How on earth could you say that he said it "best"? I have no idea what that random quote means. –  user16764 Jun 4 '12 at 23:31
    
+1 saying this kind of thing will get a lot of flak from the C++ basher bashers, but it has to be said - you can't really OOP without reflection, because you don't have the generics to take care of horizontal stuff (aspects) - lifecycle (activation, disposal), generic error handling, generic proxying, generic serialization, generic task parallelism - these end up polluting your code and breaking up SoC. –  vski Jun 5 '12 at 11:10
    
Thanks. I can't believe I got to -3 for saying that a language without reflection is not a good example of OOP. –  John Cromartie Jun 5 '12 at 17:00
1  
@JohnCromartie, Could you elaborate on that point in your answer? –  Jeremy Heiler Jun 5 '12 at 17:50
add comment

Forcing everything to be part of a class does not necessarily yield great OO code.

Ask a poor procedural programmer to program in Java and they will possibly take a class somewhere, give it a static main method and stick 1000 lines of code in it. I know I have seen it.

Java has a switch statement. I have seen switch( type ) { case typeA: bundles_of_code; break; case typeB: bundles_of_other_code; break } etc. in both C++ and Java code.

C++ supports many OO concepts but its standard is not defined by it, however I guess a lot depends on what your objective is.

The main "poor" semantic in C++ is allowing copy-construction of classes whereby an object transmogrifies into another one. You can disable this but then you cannot return one from a function. Fortunately this is addressed in C++0x.

share|improve this answer
add comment

There is a reason Graham Lee had the most upvotes here. To reiterate, it appears that a C++ class is not really an object in the sense that it does not perform message passing. I think this is what trips people up a great deal when they are learning C++ or oop. People are told object oriented is 'this' and then told that C++ does it differently. Well C++ never did OOP differently. If you think this way you'll never appreciate C++ classes for what they are meant for and that is that they are merely an improvement upon the procedural paradigm by incorporating abstraction, and dynamic behavior. So C++ classes are fundementally procedural, they just improve upon the procedural paradigm, or rather they are a more advanced version of a C struct.

share|improve this answer
    
Do you have any actual supporting reasons for your arguments? You seem to be making assertions and claiming that those who disagree must be mistaken, or would change their minds if they looked at it differently, or perhaps if they shared your exact definition of OO. –  David Thornley Jun 5 '12 at 14:13
    
Fair enough. You can read this article. I guess what I was saying is that c++ is not "object-oriented programming" in a strict Alan Kay sense. However if you define OOP as being a data structure with behavior then you can regard c++ as OOP. In my mind though it's more accurate to view a c++ class as a higher level procedural programming abstraction. A c++ class is much more efficient than an Kay style object, but worse for concurrency. Personally I think the c++ class is a great design. –  annoying_squid Jun 6 '12 at 21:11
1  
Thanks for the link, but it only explains what Alan Kay meant. Moreover, I disagree that Smalltalk is generally considered the first OO language, and Wikipedia agrees with me that that was Simula, one of the two languages Stroustrup combined to form C with Classes. I'm interested in your claim that a C++ class is more a higher level procedural programming abstraction than an object template, but I still don't understand why you think that way. –  David Thornley Jun 8 '12 at 14:44
    
Object Orientation is probably a subjective term, if we can agree on that. But I see a Kay object as a more natural way to decouple code and introduce concurrent modularity in the sense that each object serves a role like mini-computers interacting through message passing. Under this model there needs to be little to no code 'in between' b/c the entire logic of the program can be expressed as cells and messages. By comparison the use of 'classes' typically requires some procedural glue code in between (lacks true modularity) but the advantage is that classes are far more efficient. –  annoying_squid Jun 11 '12 at 15:16
add comment

In my view, it's not so much a definitional issue as a usability issue.

Objects are an abstraction intended to make it easier to read, write, and reason about complex programs. For a practical programmer, whether a language meets all the criteria of a particular formal definition of "object-oriented" (there seem to be several competing ones!) isn't really as important as whether the tools it offers are suitable for thinking about your program in terms of said objects -- i.e., actually reaping the supposed productivity benefits of OOP.

In C++, objects are a terribly leaky abstractions, often forcing programmers to wrangle with nasty issues related to how those objects are structured in memory -- issues that are more reminiscent of coding in straight C than other OOP languages. For example, C++ Frequently Questioned Answers offers this criticism (among others):

It is very beneficial for a practitioner to gain familiarity with OO systems other than C++, and with OO definitions other than the "encapsulation, inheritance, polymorphism" trinity interpreted in special ways allowing C++ to be considered "OO". For example, a claim that an environment lacking boundary checking or garbage collection is not an OO environment sounds outrageous to people accustomed to C++. But from many perspectives, it makes a lot of sense. If anyone can overwrite an object, where's the "encapsulation"? If disposing an object can lead to dangling references or memory leaks, how is the system "object-oriented"? What about the ability to tell what kind of object is located at a given place and time? You say the software works with objects - where are they? And if one can't find out, how is one supposed to debug the software?

C++ is object-oriented, but unpleasantly and incompletely: its users have to devote a lot of effort to making sure their data actually behaves like "real" objects rather than errant bits. That said, lots of code has been written in C++ over its lifespan, most of it making use of classes and dynamic dispatch, so it's self-evidently something that you can use for practical OOP.

share|improve this answer
    
-1 for referencing the FQA in what should be a serious answer. The FQA is a nest of distortions, half-truths, and misunderstandings. –  David Thornley Jun 5 '12 at 14:05
    
@DavidThornley Is the particular quote a distortion, half-truth, or misunderstanding? –  Alex P Jun 5 '12 at 14:43
    
Somewhere in there. The claim that an OO language must have boundary checking (sometimes built into C++ standard containers anyway) and garbage collection (smart pointers are primitive garbage collection) is forced and disingenuous. The sentence about a language that allows dangling references not being object-oriented is clearly Proof by Blatant Assertion. I am puzzled by the "ability to tell what kind of object"; the pointer type gives it away for objects without virtual behavior, and RTTI handles it for objects with virtual behavior. –  David Thornley Jun 5 '12 at 14:57
    
@DavidThornley The FQA's claim is that a useful OO language should have these things -- which is in line with the question being asked (is C++ "suitable"?). I think a claim about bare-bones definitions doesn't really address that... "Ability to tell": a common error behavior accessing an object that's not really there, by following a pointer to some uninitialized or previously-overwritten data; and your program will happily take that garbage and interpret it as data, and then follow garbage pointers to other garbage data until it hits an out-of-bounds address or some massive error occurs. –  Alex P Jun 5 '12 at 15:36
    
However, the quote at least strongly suggested the claim that a language without boundary checking or garbage collection isn't OO, and ignored the fact that you have those things in C++ if you like. The question of whether a language prevents certain classes of errors or not (for whatever reason) is orthogonal to whether it is OO. –  David Thornley Jun 5 '12 at 21:06
show 1 more comment

Alan Kay's perfectly valid objection to C++ was that it was a macro language on top of C.

The notion of "message passing" is simply the idea that instances of classes are held in memory and that they expose methods which can be called. Message passing is *simulated" in C++ using vtables holding pointers to functions.

To say that message passing does not exist in C++ is inaccurate, what's more accurate to say is that message passing is an integral part of other languages likes smalltalk and Java because the language is not preprocessing a foreign construct and grafting it on C directly.

This is a highly semantic language design argument which I suspect is a little beyond the experience level of the questioner.

That being said there are a thousand reasons to hate C++, and very few reasons to love it.

Rather than looking for the perfect hammer and the perfect nail, find the perfect house to build and then find the right tools...that takes experience.

Its also important to remember that in systems programming what Alan Kay fears is not "pure OOP" is actually a strength of C++. To each his own...

share|improve this answer
1  
Objective C also started as macro language on top of C, but is an object oriented language. –  Jan Hudec Jun 5 '12 at 13:16
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.