tags:

views:

1006

answers:

16

I have always heard that C++ is not Object Oriented but rather "C with Classes". So, when I mentioned to an interviewer that C++ was not really object oriented, he asked me why I didn't consider it an OO language. I haven't done any C++ since University, and I didn't have much of an answer. Is C++ Object Oriented or not? and why?

+2  A: 

C++ is object oriented. c++ is c with classes is another way to say that c++ is c with oop added (and of course, there is more than that on top of c).

Jeffrey Aylesworth
+14  A: 

C++ is an object oriented language. The problem is that some language zealots have their own, sometimes conflicting definition of OOP. For example, some Java people say that C++ is not an OOP language because you can define functions outside of a class.

Just ignore them.

Jacob
It's not necessarily the "zealots", they guy(s) who invented the term OOP had a definition for it (maybe it wasn't ever formalized), and C++ doesn't fit that definition. Many people have since jumped on the OOP wagon and now it seems that all forms and definitions of OOP are equally "valid" because most people don't know the original definition.
hasen j
+33  A: 

C++ is usually considered a "multi-paradigm" language. That is, you can use it for object-oriented, procedural, and even functional programming.

Those who would deny that C++ is OO generally have beef with the fact that the primitive types are not objects themselves. By this standard, Java would also not be considered OO.

It is certainly true that C++ isn't OO to the same extent as Smalltalk, Ruby, Self, etc. are, but it is definitely an effective OO language by most standards.

Drew Hall
Primitive types are often called objects by the standard, and surprising things like `typedef int INT; 5 .INT::~INT();` work just fine. (There must be a space before the period so it is not a decimal, and you must indirectly name the type `int` for syntax sake.) The language is defined to behave as if `operator` functions are defined for the native types, for the sake of looking up what to do with an expression. The syntax is somewhat nonuniform, but less so than "they" would have you believe, and member functions of primitive objects are overrated anyway.
Potatoswatter
I'm fairly sure that the standard doesn't confuse types and objects. An _instance of_ a primitive type is an object, but the type is not. Also, biggest mistake IMO: You can't take the address of operator+(int, int) and pass it to algorithms.
MSalters
+8  A: 

The hallmarks of object-orientation are abstraction, encapsulation, polymorphism, and inheritance.

I'd say that C++ exhibits all four, so it qualifies as an object-oriented language.

It's possible write C++ as "a better C" and use a purely procedural style. Objects aren't mandated. Maybe that's what you're thinking.

duffymo
That's just one definition of OO (and actually conflates OO with more general modular/ADT principles of abstraction and encapsulation). Talk to people in another vein of OO programming (Smalltalk, anyone?) and you'll find that the core hallmark of object-orientation is passing messages around between objects. It's hard to pin down one definition of OO.
Michael E
"conflates"? I don't know what you mean. Are you saying that priciples of modularity and encapsulation aren't part of object-orientation? I can pass messages in a functional style, too.
duffymo
+17  A: 

C++ is a multi-paradigm programming language supporting

programming styles. You can choose (and mix them) freely to meet the needs for your project.

tux21b
I initially read that as "geriatric" and thought "Only compared to some!" I think I need a nap.
Brian Postow
+4  A: 

C++ is object oriented, because classes provide abstraction and inheritance and all that jazz. It's not always consider object oriented because code doesn't need to be object oriented. It's like saying Scheme isn't functional because it has set!.

Greg
+6  A: 

C++ is an OO language.
But that is not the only style of coding that C++ can be used in. n As such C++ is technically a multiparadigm language of which OO is just one paradigm.

The term "C with classes" has a couple of meanings.

  • It can refer to the fact the C++ is OO (as classes give it the OO capabilities).
  • It can refer to the original version of "cfront"
    • Which was basically C with the extension of classes and little else.
  • It can refer (derogatorily) to a style of programming that does not utilize the full power of C++ but only uses a small subset of the language.
Martin York
The first thing Stroustrup developed was called "C with Classes", according to his "Design and Evolution of C++" (a wonderful book itself). "C with Classes" isn't just a description of the beginnings of C++, it's a name.
David Thornley
+10  A: 

Bah! The people who say C++ isn't object oriented are the same ones that would say Spam isn't food :-)

The OO "religious nutter" crowd will say that you can only have a true OO language if absolutely everything is an object. That's fine, they can sit in their ivory towers and believe what they want. Some of us have actual jobs to do.

Provided you use the object mindset, C++ (and even C if you use all sorts of tricks with function pointers within structures) is more than enough to be considered object oriented.

paxdiablo
ivory towers?? how's python an ivory tower? how's ruby an ivory tower?
hasen j
@hasen, neither Python not Ruby are inherently ivory towers. People who use Python or Ruby while insisting that C++ cannot be OO, are _using_ them as ivory towers.
paxdiablo
Ok, Spam is CLEARLY not a food. It is a food-type product. This says nothing about C++ though...
Brian Postow
@paxdiablo: alluding to "ivory towers" implies your opponent not knowing anything about the real world. here's some news from the real world: python and ruby are much more productive and pleasant to work with than C++. and guess what, everything is an object; even functions. now, try to produce a web 2.0 app in C++ :P
hasen j
Python and Ruby are indeed great, but the merits of other languages aren't relevant to whether C++ is OO or not.
Jeremy Friesner
@hasen j: http://www.webtoolkit.eu/Anyway, it seems you're missing the point. Since when was "the ability to produce a web2.0 app" a relevant requirement for whether something is an OOP language or not? The mention of ivory towers was about people who insist that a language cannot be OOP unless everything is an object. It has nothing to do with whether Python is a nice language, and nothing to do with the ease of making web2.0 apps.
jalf
+3  A: 

The idea is that C++ is not just an object oriented language.

Max Lybbert
+1  A: 

The term "object oriented" is too hazy to give a definite yes or no answer. I think you'll find the majority view is that C++ is an OO language, or at least that you can write in an OO way in C++. If you want a more definite answer, you'll have to ask a better defined question, such as:

Q: Does C++ have "object" (i.e. data fields + associated member functions) data types?
A: Yes.

Q: Does C++ have non-object data types?
A: Yes.

Q: Does C++ have non-member functions?
A: Yes.

Tom Dalling
+1  A: 

It is indeed object oriented but not strictly though.

Say for example, we can have just

int main()
{
 return 1;
}

which is nothing in the name of Object oriented and on the other hand we can have Classes, inheritance, polymorphism etc., that corresponds to Object Oriented.

It is up to us unleash the power of the language.

To the interviewer who asked you the question show a class a from C++ and ask him whether it is structured or procedured.. He will show you the same main() function I guess :)

So I guess it is based on what you have implemented that lies. But it has features that can make it to be considered as an OOP.

liaK
+4  A: 

Meh. Everybody has their own deinition of OOP. Alan Key who invented the term OOP said: http://www.noulakaz.net/weblog/2007/02/12/true-meaning-of-oop/

OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I’m not aware of them.

By that definition even Java, C#, Python etc are not OO languages.

IMHO, these discussions are pointless.

Nemanja Trifunovic
+2  A: 

If I am an interviewer asking you this question, I'm probably not caring about the yes/no answer - I really want to know what you understand about programming, and C++ or other languages. Are you just throwing out terms that you don't think about or understand, or do you actually consider carefully what you are doing and saying. So in this situation a good answer is

"I define Object oriented coding to be a, b, c, and d. I define an Object Oriented language as one which supports that definition, ie. permits me to (easily / uniformly / rigorously / other adjective ) develop code that fulfills those requirements. C++ delivers a,b,c, and partially on d. So I do ( don't ) consider C++ to be OO for those reasons."

For my personal definition, C++ is object-oriented enough, plus it supports other approaches.

jdu.sg
A: 

C++ is not an object-orientated language. The language is not any paradigm. This is in constrast to Java, which is religiously object orientated (no friend statement, for example). C++ offers object orientation, but isn't inherently object-orientated.

DeadMG
+2  A: 

As other have said, C++ is not a PURE OO language. Then again, the only Pure OO language I know is smalltalk. The only pure functional language I know is the Lambda Calculus. I don't know ANY pure structured languages (They all have goto and/or multiple return statements)

Most people don't like writing in pure programming languages. It cramps their style.

Brian Postow
A: 

Hi,

Object-oriented programming (OOP) has become the preferrd programming approach by the software industries, as it offers a powerfull way to cope up with the cpmlexity of real world probleams. Among the OOP languages available today, c++ is far the most widely used language.

The languages should support several of the OOP concepts to claim that they are object oriented. depending on the fetures they support , they can be classified in to two categories.

  1. Object-Based programming languages.

  2. Object-Oriented programming languages.

    1. Object-Based programming languages. if it supports

         A. DATA ENCAPSULATION
         B. DATA HIDING AND ACCESS MECHANISAMS
         C. AUTOMATIC INITIALIZATION& CLEAR-UP OF OBJECTS
         D. OPERATOR OVER LOADING
      

      2.Object-Oriented programming languages.

      It supports all object-based programming features along with two additional features

        E. INHERITANCE
        F. DYNAMIC BINDING
      

hence Object-Oriented programming languages means

**Object-based features+ inheritance+ dynamic binding.**

Examples : C++, SMALLTALK,OBJECT PASCAL,JAVA

So , C++ is an OBJECT-ORIENTED PROGRAMMING LANGUAGE>

still you got some doubts in object oriented programming concepts refer the book of E.BalaguruSwamy.

ksrao