Is C++ an Object Oriented language? - c++

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?

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.

C++ is a multi-paradigm programming language supporting
imperative
object-oriented (class-based)
generic (template metaprogramming)
programming styles. You can choose (and mix them) freely to meet the needs for your project.

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.

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.

Meh. Everybody has their own deinition of OOP. Alan Kay 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.

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.

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.

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

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

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.

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.

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).

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.

Stroustrup, who designed C with classes, viz. the original implementation of the ISO C++ standard, answered the same question in ACM's OOPSLA '95. The paper's softcopy is available at http://www.stroustrup.com/oopsla.pdf.

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.

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.

C++ is partially OO because the code can be written without the class and it has also multiple inheritance concept, i.e one child can have more than one parent.

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.
Object-Based programming languages.
Object-Oriented programming languages.
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>
If you should still have some doubts in object oriented programming concepts refer the book of E.Balaguruswamy.

Related

Why is C not OOP if it has structs

Having done some C++ I have noticed that C also has structs - surely C should be considered OOP if it has them?
Because it does not have some of the basic OOPs features of:
Inheritance
Polymorphism and so on
From Wikipedia:
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their interactions.
In C, data and methods (functions) are separated from each other. There aren't any "objects" in C like described above.
In e.g. C++ and Objective-C they aren't separated.
Because structs only allow for structured programming. For real OO programming, you need for the language to at least support encapsulation, inheritance and member functions.

C to C++ : Transitioning from one language to the other [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
C++ tutorial for experienced C programmer.
I program in a number of languages frequently and have been using C++ lately. Basically my classes are just wrappers around pure C code. Almost like a struct with associated methods. This gives me the encapsulation and privacy that I want for my data. I have a small hierarchy of classes and am just barely using inheritance.
I am familiar with OO concepts and know what search terms to use when i need to find out about a particular concept in this regard. However, as I have discovered in my foray in the programming world, often the language features that are really helpful are hidden to the newcomer or novice, and the useful bits that I need have already been written and are in a library somewhere that is freely available (most times part of the framework - like in .NET).
What path would you suggest to gain this vital knowledge in C++ and stop myself reinventing the wheel (poorly).
This is the wrong way to use C++. You would be better served grabbing a copy of Accelerated C++ and reading it. Yes, it's a beginner book but unless you want to continue treating C++ as just C with objects then you need to focus on how C++ programmers do things instead of just sticking with what you already know. You need to start from the beginning and build a good foundation in C++.
Learn the STL if you're going to really plan to use C++ in the future. Though opinions will vary widely, particularly among the die-hards, I think there is absolutely no problem using C++ as "C with objects."
Boost is also pretty awesome.
EDIT: Note the downvotes already coming in from the die-hards. C++ acolytes really don't like to hear people advocate using the language as "C with objects." I stand by my statement. You can write quite shippable and commercially viable code without going crazy with an RTTI-enabled, templatized, multiply-inherited set of classes. Remember KISS.
Scott Meyers' books are a great place for C programer to begin with C++.
I recommend Thinking In C++ by Bruce Eckle. Normally it's available for free online, or as a book.
I suggest you read the books:
"C++ Coding Standards: 101 Rules, Guidelines and Best Practices" --Sutter & Alexandrescu
"Modern C++ Design: Generic Programming and Design Patterns Applied" --Alexandrescu
And probably anything else by Andrei Alexandrescu that you can get your hands on.
Then, there are a number of design patterns and programming idioms that make it very clear why "C with objects" is extremely reductionist. Just to name a few: RAII (Resource Allocation Is Initialization), PImpl (or Cheshire Cat), Factory functions, Smart Pointers, Singleton, Type Traits, Expression Templates, etc. When you know about these, you are no longer programming in C++, but in ++C (because you get a result that actually reflects the increment over C).
As for not reinventing the wheel, like many have said already, make sure to first explore the possibilities in the Standard Template Library (STL) (which is much richer than you might think) and then look at Boost (www.boost.org) which has libraries for a lot of diverse purposes and they are extremely high quality (and some are just works of art, like Spirit, Proto, Lambda and MPL). After that, there is of course a large amount of open-source software in C++ out there, but use it with caution: sometimes it is better to reinvent a wheel that fits perfectly to your application than to use one that might not be appropriate or powerful enough, or worse, full of bugs!
I suggest the book The C++ Programming Language for filling in the gaps in your basic C++ knowledge, and BOOST as the first place to look for existing libraries supporting your programming.
Have you thought about getting the C++ Primer Plus? It's a really good book.
The C++ Standard Library: A Tutorial and Reference
Read Meyers for specific tips, but also Stroustrup's Design and Evolution. The latter gets into the motivation as to why C++ is what it is, and very much comes from a "how to improve C" viewpoint.
As for "The C++ Programming Language", 3rd edition is very long. If you can find the 2nd edition, its much more digestible, although of course occasionally out of date (but mostly just less complete).

Why is programming with objects not considered procedural?

Even though OOP uses objects and data encapsulation, the code still writes out like a procedure. So what makes OOP loose the procedural label? Is it just because it is considered "high-level"?
Thank You.
It's not that Object-orient Programming is "non-Procedural"; it's just that the code we call "Procedural" is not Object-oriented (and not Functional and probably not a couple others)
It's not so much an either-or case, but a slow gradiate:
Spaghetti code -> Structured Code -> Object-oriented code -> Component code.
(UPDATE: Removed "Procedural" from the chart above, since it refers to all of the right 3/4rds of it)
In theory OOP and procedural programming are orthogonal concepts. The fact that they so intertwined in practice is probably more coincidence than anything else. Because it is so familiar, procedural syntax is the most human readable format around. Message-passing, functional computation expressions, and various other formats -- because of their unfamiliarity -- are simply not as easy for most programmers to work with. Couple this with the fact that most OOP systems are based on extensions to procedural languages, and it becomes pragmatically difficult to separate the two paradigms. (As a side note: That's one of the things I like about F#; as a multi-paradigm language, it helps conceptually separate the various aspects of OOP, imperative programming, functional programming, while making all of them available.)
I would say object-oriented and procedural are orthogonal concepts. Many popular object-oriented systems are extensions of procedural languages, but not all. For example, Dylan reads like a blend of functional and object-oriented programming.
This is just a 'convention' thing. When people say 'procedural', it is implied that it isn't OO, and vice-versa.
OOP does not loose procedural label. Procedural programming is imperative programming. OOP extends procedural programming. C++ and Objective C are OO extensions of C. Functional programming usually is declarative - opposite of imperative.
From Wiki (well explained):
The focus of procedural programming is
to break down a programming task into
a collection of variables, data
structures, and subroutines, whereas
in object-oriented programming it is
to break down a programming task into
objects with each "object"
encapsulating its own data and methods
(subroutines). The most important
distinction is whereas procedural
programming uses procedures to operate
on data structures, object-oriented
programming bundles the two together
so an "object" operates on its "own"
data structure.
More can be found here.
The Wikipedia article at http://en.wikipedia.org/wiki/Procedural_programming provides a decent explanation of the differences between object-oriented programming and procedural programming, but in short, object-oriented programming is about the exchange of messages between collaborating objects rather than stringing procedures together to operate upon loose data structures.
Internally, objects do resemble little procedural programs, but their data isn't publically exposed and operated upon by other objects. The "Tell, Don't Ask principle" is an object-oriented design principle that perscribes this interaction between objects. The study of this principle may help to shed further light on the nature and intent of object-oriented design over procedural design.
It never loses procedural label. Its a mis-conception. OOP is much more than encapsulation and objects. Click here for more info.
I think one of the distinctions is that virtual properties and methods are used much more heavily in object-oriented languages than are function pointers in languages like C. In C, if I say foo(x), it's pretty clear that I'm doing one of two things, and the declaration of foo(x) will tell me which one. I'm either calling a function named foo(), or I'm calling a function pointed to by a function pointer named foo(). In an object oriented language, when I write foo(x), that may get implicitly mapped to invoke code which didn't even exist when my module was compiled.
Depends on your definition of 'oriented'.
If 51% of the code is O-O, does it qualify?
OOP is not just encapsulation.
Polymorphism is one of its most powerful feature.

Why does OpenGL have global functions?

Why isn't openGL object-orientied? Everybody teaches Object Orientated Programming + Design Patterns, but OpenGL has many global functions. Isn't this bad style?
The whole point of a low-level API is to make it as minimal and portable as possible. Giving it an object-oriented architecture would not allow this:
Polymorphism adds unnecessary function call overhead.
It forces you to use some relatively difficult calling convention, which reduces portability.
You cannot wrap an object-oriented architecture to make it procedural, but you can do the reverse; so, it makes sense to make things as flexible as possible. It's trivial to write an object-oriented wrapper around OpenGL if you want.
Finally, you should really question what you've been taught about OOP. Despite what your college or university may tell you, OOP is not a panacea of program design. There are very good reasons why there is absolutely no object-orientation in the C++ STL (and most of Boost for that matter).
Object-orientation is useful in some cases, but you should learn when it is useful, and when it is not, and under no circumstances should you believe that anything that is not OOP is "bad style".
OpenGL
OpenGL should support all platforms -- there's nothing near to C in this regard - thanks to that almost every device can use the same API
OpenGL should support all languages -- there's also nothing near to C in this regard - thanks to that, each language that supports calling C libraries (and almost all do) can use OpenGL
OpenGL is an API, not a engine -- it intends to provide low level interface to the graphics harware, yet enough high level, to be an abstraction to different hardwares -- C is a lot more low level than C++, OOP is not low level
OpenGL is a framework to build upon, not a complete solution -- there is no one and true way to write graphics code, and OpenGL isn't supposed to force us to anything - by being OOP it would force us to their "colution
OpenGL is not tied to any specific programming paradigm -- hence we can wrap OpenGL into a functional, logical or OOP language -- or use it procedurally
OpenGL is about efficiency -- and you can't get more efficient than by direct function calls. OOP is as efficient as it is suited for a particular task.
In general -- OpenGL is designed to allow us to have all the freedom, and don't make any choices for us. And by freedom I mean freedom to choose a platform, a language, a programming paradigm, a engine design, a methodology, and a level of efficiency vs. readability.
And for that I praise OpenGL, and for that I hate Direct X.
Amen.
Sidenote: Everybody teaches Object Orientated programming because it's the easiest to grasp. It's not the one and only true paradigm. There's functional programming, logical programming, contract programming, and even a object oriented way to write in C. There's no one truth in computer science. As for Design Patterns, I could name more than a few that are used in OpenGL's architecture. Bad Style? I've seen beautiful C programs that had aaaaallll global functions...
In general, OpenGL is object oriented. It is just implemented in a language that doesn't directly support OOP. But the API is object-oriented: It consists of a number of different object types, and a set of operations defined on each. And the internals of each object type are hidden from the user. It fulfills all the requirements for OOP. It just so happens to be implemented in C, which doesn't have a convenient class or member method syntax.
Apart from this, there is absolutely nothing wrong with global functions. In C++, a common recommendation is to prefer them over member methods whenever possible. In functional programmming, global functions are the default.
OpenGL was created for and in C, and none of that stuff existed then. Even now, they still want to keep a C interface, because C is still a widely used language.
Should they maintain both C interfaces and C++ wrappers, ditch C and just use C++, or keep a C interface? I'd argue the latter is the best solution: easy on them, not too hard for us.
That said, the OpenGL interface is admittedly gross. Lot's of stuff was "suppose" to be deprecated, but alas that got moved to a later date.
Well, there are a few reasons.
You should think of an OpenGL context as a state machine. There is only one of them active at any time. There is no difference between putting a little Opengl.whatever in front of everything.
Speed, OpenGL was designed to be a minimal API
If it was object oriented, what language would you write it in? C++? Then everybody would ahve to write complex bindings. C is WAY easier to wrap.

Is C++ not a fully OOP Language?

I know that in OOP we have to declare everything e.g. variables, functions, etc. inside a class like in Java, but in C++ we can declare outside the class too.
Is this the reason that, C++ is not fully OOP? Or is there anything else?
Huh? C++ is a hybrid, multi-paradigm language. It is certainly not a "pure" object-oriented language, where "everything is an object" holds true. C++ supports classes, objects, encapsulation, and so on, but since it's also (more or less) backwards-compatible with a lot of C code, it cannot be "fully object-oriented".
Object-Oriented Programming is not definition of the language, it's definition of the programming, a program. I.e. one program in C++ can be OOP, and other can be not OOP.
What you can say is that C++ fully supports means of programming in OOP paradigm.
Define fully OOP? There are as many opinions as people probably
Note as far as purity goes, IIRC all languages with valuetypes are not "pure" in the strict sense. No, boxing doesn't count.
Over the years, in discussion I've tried to go back to the core OOP features:
identity
Classification
polymorphism (not inheritance, since some OOP have no inheritance)
encapsulation
So if you can tell if two classes are not the same class (identity), you can make the classic "duck quacks" and "dog barks" example (to demonstrate inheritance/polymorphism and classification) and you can hide fields, you are pretty much there.
Applying it to all the languages is more difficult though. While I do get functional programming roughly, I'm not trained enough in the their near infinite jargon to judge all those functional-oop-imperative hybrids that are springing up,
In C++ you don't HAVE to code using OOP, you can choose not to use it. Having said that, it's "fully OOP", OOP is just just not a requirement.
not even java is a full OOP language.
in real OOP languages everything is an object, conditionals, loops, etc.
Even though the question is somewhat ugly phrased I'm not really satisfied with all the answers provdided yet.
I preffer to think of languages as "supporting a paradigm" and not "being in a paradigm".
So, when does a language support a paradigm? When it is easy to write code that satisifies the requirements of the paradigm.
How one comes to this conclusion? Consider the style linux filesystems are implemented. It is C-Code that clearly has OO properties. So, would you not consider this code to be OOP because C is not a OOP-language? I don't think so. (I guess some people will rightfully disagree as this seems to be amtter of opinion.)
What does this imply for C++? Well, C++ has a lot of facilities for making it easier to program in a OO-Style, but it also provides you with a lot of means to rape the paradigm and write code that looks OO (because you use classes, inheritance private variables) but completly violates some other OO-principles (e.g. single responsibility, open-closed, uniform access).
I would conclude that C++ supports the OO-paradigm to some extent but is clearly inferior to some of the modern OO languages.
The main concept of OOP is that every member in an object oriented programming language should be defined inside of the class, whereas in c++ the main function is defined outside of a class. That is why c++ is not fully object oriented programming language.
The main() function is not inside a class, so for this reason, one could argue that C++ is not fully OOP.
One of the reason C++ is not fully OOP is the requirement of backward compatibility with a lot of C code. Built in types are not Objects in C++ as it is less efficient if they were. Remember, C++ first target audience were existing C programmers and efficiency was (is) a great concern.
However, C++ supports all the important features of OOP.
Related Link : www.research.att.com/~bs/oopsla.pdf