Is C++ an Object Oriented language?

2019-02-01 18:31发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.



回答4:

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.



回答5:

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.



回答6:

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.



回答7:

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.


回答8:

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



回答9:

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



回答10:

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.



回答11:

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.



回答12:

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



回答13:

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.



回答14:

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.



回答15:

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.



回答16:

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.



回答17:

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.



回答18:

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>

If you should still have some doubts in object oriented programming concepts refer the book of E.Balaguruswamy.



标签: c++ oop