C++是面向对象的语言吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3498730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:59:01  来源:igfitidea点击:

Is C++ an Object Oriented language?

c++oop

提问by David Laughlin

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++ 不是面向对象的,而是“带有类的 C”。所以,当我向面试官提到 C++ 并不是真正的面向对象时,他问我为什么我不认为它是一种面向对象的语言。自大学以来我没有做过任何 C++,我也没有太多答案。C++ 是否是面向对象的?为什么?

回答by Drew Hall

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

C++ 通常被认为是一种“多范式”语言。也就是说,您可以将它用于面向对象、过程甚至函数式编程。

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.

那些否认 C++ 是面向对象的人通常对原始类型不是对象本身这一事实感到不满。按照这个标准,Java 也不会被视为面向对象。

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++ 确实不像 Smalltalk、Ruby、Self 等那样是面向对象的,但按照大多数标准,它绝对是一种有效的面向对象语言。

回答by tux21b

C++ is a multi-paradigmprogramming language supporting

C++ 是一种多范式编程语言,支持

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

编程风格。您可以自由选择(和混合)以满足您的项目需求。

回答by Jacob

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.

C++是一种面向对象的语言。问题是一些语言狂热者有他们自己的,有时是相互冲突的 OOP 定义。例如,一些 Java 人说 C++ 不是 OOP 语言,因为您可以在类之外定义函数。

Just ignore them.

只是忽略它们。

回答by paxdiablo

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

呸! 说 C++ 不是面向对象的人和说垃圾邮件不是食物的人是一样的 :-)

The OO "religious nutter" crowd will say that you can only have a true OO language if absolutely everythingis 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.

如果您使用对象思维方式,C++(甚至 C,如果您在结构中使用函数指针的各种技巧)就足以被视为面向对象。

回答by Nemanja Trifunovic

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 都有自己的定义。发明 OOP 一词的 Alan Kay 说: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.

OOP 对我来说仅意味着消息传递、本地保留和保护以及状态过程的隐藏,以及所有事物的极端后期绑定。它可以在 Smalltalk 和 LISP 中完成。可能还有其他系统可以做到这一点,但我不知道它们。

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

根据该定义,甚至 Java、C#、Python 等都不是面向对象的语言。

IMHO, these discussions are pointless.

恕我直言,这些讨论毫无意义。

回答by duffymo

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.

我想说 C++ 展示了所有四个,所以它有资格作为一种面向对象的语言。

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++ 写成“更好的 C”并使用纯程序风格。对象不是强制性的。也许这就是你的想法。

回答by Martin York

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.

C++ 是一种面向对象的语言。
但这并不是 C++ 可以使用的唯一编码风格。因此,C++ 在技术上是一种多范式语言,其中 OO 只是其中的一种范式。

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

术语“C with classes”有几个含义。

  • 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.
  • 它可以指 C++ 是面向对象的这一事实(因为类赋予它面向对象的功能)。
  • 可以参考原版“cfront”
    • 这基本上是带有类扩展的 C 语言,其他的很少。
  • 它可以(贬义地)指代一种不利用 C++ 全部功能而仅使用该语言的一小部分的编程风格。

回答by Max Lybbert

The idea is that C++ is not justan object oriented language.

这个想法是 C++不仅仅是一种面向对象的语言。

回答by Greg

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 needto be object oriented. It's like saying Scheme isn't functional because it has set!.

C++ 是面向对象的,因为类提供抽象和继承以及所有爵士乐。它并不总是考虑面向对象,因为代码并不需要是面向对象的。这就像说 Scheme 不起作用,因为它已经设置了!。

回答by Brian Postow

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)

正如其他人所说,C++ 不是纯面向对象的语言。再说一次,我知道的唯一纯 OO 语言是 smalltalk。我所知道的唯一纯函数式语言是 Lambda 演算。我不知道任何纯结构化语言(它们都有 goto 和/或多个 return 语句)

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

大多数人不喜欢用纯编程语言编写。它限制了他们的风格。