Is C++ Object Oriented?

Red Squirrel

No Lifer
May 24, 2003
68,969
12,870
126
www.anyf.ca
I was arguing with someone who was saying that C++ is not object oriented. I think he was getting confused between managed and unmanaged. C++ is not managed, but AFAIK it's still object oriented. Ints, bools etc may not be objects but just because C++ has non OOP aspects I can't see why it would render the whole language not OOP.

C on the other hand does not have OOP.
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
It's a multi-paradigm programming language and it supports the object oriented paradigm.. so yes.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
I was arguing with someone who was saying that C++ is not object oriented. I think he was getting confused between managed and unmanaged. C++ is not managed, but AFAIK it's still object oriented. Ints, bools etc may not be objects but just because C++ has non OOP aspects I can't see why it would render the whole language not OOP.

C on the other hand does not have OOP.

C++ is considered by most to be object oriented. It doesn't HAVE to be object oriented, and that is what some people get hung up on. There is a difference between a pure object oriented language and an object oriented language, C++ is definitely not a pure object oriented language.

The only somewhat main stream language that is purely object oriented, is ruby. Useful languages usually mix it up a bit.
 

Woosta

Platinum Member
Mar 23, 2008
2,978
0
71
Simula/Smalltalk are examples of pure OO languages, which influenced Ruby, Python, Objective-C, Java, C#.. you can do imperative programming in pretty much all of the influenced languages, some of which are more OO based than others.
 

brandonb

Diamond Member
Oct 17, 2006
3,731
2
0
Object orientation on most languages is not required, and optional. Languages followed a certain evolution just adding to what has existed, but old styles are still options.

Languages of today like C# can opt out of object orientation, and be programmed entirely procedural (or less), not that I would understand why you would want to do such things. But since it has the ability to be entirely object oriented and not at all, I would still classify it as an object oriented language. The language itself is object oriented, but it doesn't force the programmer to write object oriented code.

C++ falls into the same boat, sure you don't have to program object oriented code, but it allows for it, so I'd classify it as such. The only thing in C++ non object oriented would be the main function that gets called when starting a program:

void main(void)
{

Class *myClass;
myClass = new Class;
myClass->Run();

}

That is the enterance to the program, and it's not an object, but it creates the class and runs the code and now you can be entirely object oriented.

Your friend may be talking about Runtime libraries, such as the .Net Framework, in which you have a bunch of objects to program with. In C++, alot of the functionality would be static or procedural functions, such as the WinAPI, you just have a huge lump of functions to call but none of it is in a class. .Net framework everything is divided into classes. All disk functions are in "System.IO", etc. But does that mean C++ is not object oriented? Nope. Nothing is stopping a programmer from adding those straight C procedures from the WinAPI into a class and "wrapping" the windows functions into classes much like the .Net framework.

And anybody who has any programming knowledge will realize the .Net Framework is really just C++.dll's that call those WinAPI's and are organized into classes with alot of extra error handling and standardization.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
The only somewhat main stream language that is purely object oriented, is ruby. Useful languages usually mix it up a bit.

Object-orientation is not so much a property of a language as it is an abstraction. OOP is, in theory, independent of language. In practice, some languages make OOP easier than others, e.g., with classes, dynamic typing, functional programming features, templates, whatever. But OOP as a practice itself is doable in lots of languages in which you don't commonly see it.

Imperative vs. functional, dynamic vs. static types, interpreted vs. compiled, these are the properties of the underlying language. E.g., I disagree with Cogman's observation about Ruby -- its easy to write procedural code in Ruby, python, etc., just as it is (arguably) easy to write OOP in C.

So, back to the OP's question, NO, C++ isn't Object-Oriented. Neither is any other language. Its like asking if a fiddle can play a jig -- of course it can't! -- but a fiddle player can use the fiddle to play a jig. The same fiddle could be used to play other kinds of music, too.

EDIT: My post was roughly concurrent to brandonb's... so if it agrees without sufficient citation, thats why.
 
Last edited:

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Object-orientation is not so much a property of a language as it is an abstraction. OOP is, in theory, independent of language. In practice, some languages make OOP easier than others, e.g., with classes, dynamic typing, functional programming features, templates, whatever. But OOP as a practice itself is doable in lots of languages in which you don't commonly see it.

Imperative vs. functional, dynamic vs. static types, interpreted vs. compiled, these are the properties of the underlying language. E.g., I disagree with Cogman's observation about Ruby -- its easy to write procedural code in Ruby, python, etc., just as it is (arguably) easy to write OOP in C.

So, back to the OP's question, NO, C++ isn't Object-Oriented. Neither is any other language. Its like asking if a fiddle can play a jig -- of course it can't! -- but a fiddle player can use the fiddle to play a jig. The same fiddle could be used to play other kinds of music, too.

EDIT: My post was roughly concurrent to brandonb's... so if it agrees without sufficient citation, thats why.

There are certain traits for a language to be considered OO (in academia at least). I agree, that saying C can't be OO is pointless since structs really are just classes without the this pointer. However, based on the purely academic definition of OOP languages, C cannot be considered one because it doesn't provided polymorphism, or inheritance.

Why is ruby considered a "Pure" OO language? You can't use ruby without using objects, you can program with a procedural paradigm with ruby, but you still can't escape the use of objects. From the wiki
Languages called "pure" OO languages, because everything in them is treated consistently as an object, from primitives such as characters and punctuation, all the way up to whole classes, prototypes, blocks, modules, etc. They were designed specifically to facilitate, even enforce, OO methods.

That is what I am refering to when I say something is a pure OO language.

I do agree with the intent of your post, saying one language is OO and another isn't is somewhat silly considering that you can still treat things like objects even if the language doesn't.

brandonb, While what you say is true, You should realize the .Net code is interpreted (like Java) Hence there is an extra layer of an interpreter reading the byte code and converting it to native machine code. The idea being that some day if the x86 dies, all our .Net programs won't be worthless. Microsoft "simply" has to rewrite their interpreter for whatever architecture the new windows runs on (Or, that you could take compiled code from one machine to another without recompiling it).

One reason C# and Java aren't considered "Pure" OO languages is because some of their primitive data types are not objects (IE integers)
 

iCyborg

Golden Member
Aug 8, 2008
1,332
56
91
One reason C# and Java aren't considered "Pure" OO languages is because some of their primitive data types are not objects (IE integers)
And afaik, classes also aren't objects in C#/Java like in 'Pure OO' languages.
I remember thinking they went a bit too far with this "everything is an object" approach. One of the courses I took used Smalltalk, and I had some problems figuring out what the heck was the professor talking about. It was something about all objects being instances of classes, and these also had to be objects and they were instances of metaclasses. And then came some convoluted part where all metaclasses were instances of a class called Metaclass, and this one also had a metaclass which was an instance of itself or something. I know I was like FFS, we have objects and classes, let's keep it at that... :p
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
There are certain traits for a language to be considered OO (in academia at least). I agree, that saying C can't be OO is pointless since structs really are just classes without the this pointer. However, based on the purely academic definition of OOP languages, C cannot be considered one because it doesn't provided polymorphism, or inheritance.

Polymorhism in C:

enum PolyTypes = { Type_String, Type_Int }
struct Polymorph {
PolyTypes type;
union {
char * p_str;
int n;
} u;
};

void Set( Polymorph* pp, char* p_str ) {
pp->type = Type_String;
pp->u.p_str = p_str;
}

int Get( Polymorph* pp ) {
if( p->type == Type_Int ) return pp->u.n;
else return itoa( pp->pp_str; );
}

etc.

Inheritance in C:

struct Parent {
int parentInt;
double parentDouble;
};

/* Put the Parent declaration first, and anything expecting a Parent* can use a Child* */
struct Child {
Parent parent;
int childInt;
double childDouble;
}

-- So whats different from C++?
Namespaces are manually managed instead of automatically (aka, syntax is different).
No compiler support for data privacy - would have to have something like this, if you wanted parentDouble to be private:


struct Parent {
int parentInt;
double parentDouble;
};

struct ParentInheritable {
int parentInt;
char privateData[sizeof(PARENT_PRIVATE_DATA)];
};

struct Child {
int childInt;
double childDouble;
ParentInhertable p;
};

This is almost exactly what is going on under the hood in the C++ compiler and runtime anyway, but without the syntax. I'll admit the C syntax is nastier, but it is just a syntactic difference.

If you wanted more exotic inheritance, for instance, you could implement virtual function call tables in C. Check out all the cool stuff implementing C++ features in C here: http://www.w3.org/Library/User/Style/Cpp.html
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
And afaik, classes also aren't objects in C#/Java like in 'Pure OO' languages.
I remember thinking they went a bit too far with this "everything is an object" approach. One of the courses I took used Smalltalk, and I had some problems figuring out what the heck was the professor talking about. It was something about all objects being instances of classes, and these also had to be objects and they were instances of metaclasses. And then came some convoluted part where all metaclasses were instances of a class called Metaclass, and this one also had a metaclass which was an instance of itself or something. I know I was like FFS, we have objects and classes, let's keep it at that... :p

I'm not sure about Java, but classes are objects in C# as well. They are instances of the Type class, and have their own properties and methods that allow access to all the metadata about the type, such as what properties it supports, what attributes are declared on properties, what constructors, methods, fields, etc. are supported.
 

Fox5

Diamond Member
Jan 31, 2005
5,957
7
81
Bleh, I hate object oriented programming.
It can be a huge win for code maintainability, and I guess it's relatively easy to teach to programmers (as opposed to creating well-structured code in any other paradigm), but it's use is just too overblown.

Most people don't use it correctly, and just turn everything into classes and think that makes for good object oriented code.
And besides that, it needlessly complicates most small projects (which is what every project in my undergrad was, longest project was only 40 or 50 pages of code) and doesn't always lend itself well to the optimal algorithm for whatever you're doing.
Then again, besides database and data structure code, almost everything I did in undergrad was math programming or used OpenGL.
 

Red Squirrel

No Lifer
May 24, 2003
68,969
12,870
126
www.anyf.ca
Thanks everyone for the input. I showed my coworker this thread. He still denies C++ to being a OOP language though but w/e. Guess if you look at it degibson's point of view then it makes sense if you see it that way. OOP being more or less a way of doing things, not an actual language feature.

Bleh, I hate object oriented programming.
It can be a huge win for code maintainability, and I guess it's relatively easy to teach to programmers (as opposed to creating well-structured code in any other paradigm), but it's use is just too overblown.

Most people don't use it correctly, and just turn everything into classes and think that makes for good object oriented code.
And besides that, it needlessly complicates most small projects (which is what every project in my undergrad was, longest project was only 40 or 50 pages of code) and doesn't always lend itself well to the optimal algorithm for whatever you're doing.
Then again, besides database and data structure code, almost everything I did in undergrad was math programming or used OpenGL.

I'll agree for small projects OOP is overdoing it. Though I'm bad for turning everything into classes, don't know if that's really bad or good. What I often do though is start with less classes then as I find myself repeating certain tasks I then ask myself "can I maybe turn this into an class and treat as an object?" and then make a class for it and spend more time redoing things, but it then speeds things up going forward as I'll make base functions to do repeated tasks etc.

For very small projects I don't bother but for big projects, or small projects that I think will get big, I really enjoy OOP. I find the hard part is figuring out how to properly structure things though. Sometimes it takes some very deep thinking before I come up with the proper classes.
 

Cogman

Lifer
Sep 19, 2000
10,283
134
106
Thanks everyone for the input. I showed my coworker this thread. He still denies C++ to being a OOP language though but w/e. Guess if you look at it degibson's point of view then it makes sense if you see it that way. OOP being more or less a way of doing things, not an actual language feature.

Whats to deny about it? What is his reason for saying that c++ isn't object oriented. Is it just because he says so, or does he have some more technical issue with why he thinks c++ isn't object oriented?
 

Red Squirrel

No Lifer
May 24, 2003
68,969
12,870
126
www.anyf.ca
Whats to deny about it? What is his reason for saying that c++ isn't object oriented. Is it just because he says so, or does he have some more technical issue with why he thinks c++ isn't object oriented?

His mentality is that if it's possible to have "bare" data types such as ints (ex: you can't do int.ToStr() or w/e like in C#) then it's not OOP. So the fact that you can code non OOP in C++ makes the whole language not a OOP language. Part of it is also "because he says so". He always sticks to a point and can never be wrong.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
His mentality is that if it's possible to have "bare" data types such as ints (ex: you can't do int.ToStr() or w/e like in C#) then it's not OOP. So the fact that you can code non OOP in C++ makes the whole language not a OOP language. Part of it is also "because he says so". He always sticks to a point and can never be wrong.

Your friend isn't wrong about his assertions about OOP or not OOP -- after all, whether something "is OOP" is a matter of opinion and interpretation. But he is wrong about what OOP is in the first place. What your friend describes is a language free of static types (and in this case, primitive types). Lack of primitive types does not imply OOP, nor does OOP imply lack of primitives. As I said before, OOP is an organizational style, not a language construct.

Aside:
Primitives are just abstractions anyway! Primitives are objects, they just have a crapton of built-in operators on them.

Anyway, tell him he's been misinformed, and move on. ;-)
 

erwos

Diamond Member
Apr 7, 2005
4,778
0
76
His mentality is that if it's possible to have "bare" data types such as ints (ex: you can't do int.ToStr() or w/e like in C#) then it's not OOP. So the fact that you can code non OOP in C++ makes the whole language not a OOP language. Part of it is also "because he says so". He always sticks to a point and can never be wrong.
According to this brilliant logic, Java is not an object oriented language, because it has primitives. While I greatly appreciate how C# handles primitives in comparison to Java, they have no bearing on whether a language is truly object-oriented.

The rule of thumb I always learned in school (and, to be fair, they have held up fairly well in the real world so far) was that for a language to be object-oriented, it had to have NATIVE support for:
1. Abstraction
2. Polymorphism
3. Inheritance

Someone in this thread was trying to show that you could essentially fake these things in C. This is true, but the language itself does not have native support for these paradigms, and the sheer ugliness of the examples given reveals that quite clearly. The fact that a language DOES have elegant, intuitive support for OO paradigms is what makes it OO in the first place.

I would further submit that it is pretty damn apparent when trying to do OO coding that C and Perl are not OO languages, and that C++, Java, and C# definitely are. I have the misfortune of having to maintain some pseudo-OO code in Perl, and it's a complete nightmare almost completely because the language lacks many of these niceties.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
I would further submit that it is pretty damn apparent when trying to do OO coding that C and Perl are not OO languages, and that C++, Java, and C# definitely are. I have the misfortune of having to maintain some pseudo-OO code in Perl, and it's a complete nightmare almost completely because the language lacks many of these niceties.

I feel for you. That maintenance sounds terrible. Languages that lack the syntactic sugar to make OO code probably should not be used for OOP -- at least, I wasn't advocating it. But the abstractions of OOP are independent of the language. Of course, the language sure can make it ugly with syntax (i.e., lack of native support).
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
I have the misfortune of having to maintain some pseudo-OO code in Perl, and it's a complete nightmare almost completely because the language lacks many of these niceties.

I feel for you. :\ I once tried to write OO-code for Perl to work with Eclipse, and in the end, I gave up. IMO, Perl is for quick and dirty coding.

-chronodekar
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
C++ = C + possible object orientation;

java = C++ minus some stuff (multiple inheritance, etc.) + always object orientated;

Don't let the semantics dissuade from its usage. OOP is sometimes considered to be the purview of the high tower folks. Using just parts of it can be very useful.