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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1635583/
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-27 20:40:54  来源:igfitidea点击:

Is C++ completely object oriented language?

c++oop

提问by Vaibhav

I read about small talk being completely object oriented.. is C++ also completely object oriented? if no.. then why so??

我读到关于完全面向对象的闲聊……C++ 也是完全面向对象的吗?如果没有..那为什么会这样??

回答by JXG

No, it isn't. You can write a valid, well-coded, excellently-styled C++ program without using an object even once.

不,不是。您甚至可以在不使用对象的情况下编写有效的、编码良好的、样式精美的 C++ 程序。

C++ supports object-oriented programming, but OO is not intrinsic to the language. In fact, the main function isn't a member of an object.

C++ 支持面向对象编程,但 OO 不是该语言固有的。实际上,main 函数不是对象的成员。

In smalltalk or Java, you can't tie your shoes (or write "Hello, world") without at least one class.

在 smalltalk 或 Java 中,如果没有至少一个类,您就无法系鞋带(或写“Hello, world”)。

(Of course, one can argue about Java being a completely object-oriented language too, because its primitives (say, int) are not objects.)

(当然,人们也可以争论 Java 是一种完全面向对象的语言,因为它的原语(例如 int)不是对象。)

回答by Steve Gilham

C++ contains a 'C' dialect as a subset, permitting a purely procedural style of code.

C++ 包含“C”方言作为子集,允许使用纯程序风格的代码。

回答by paxdiablo

The big arguments people have against declaring C++ as "pure" OO is that it still requires at least onenon-OO bit, main(), and that not everything is an object (int, longet al).

大论据人反对,宣布C ++作为“纯” OO是,它仍然需要至少一个非OO位main(),而不是一切都是对象(intlong等)。

It also exposes the state of an object for manipulation without using the message-passing paradigm (public members). This breaks the encapsulation of objects.

它还公开对象的状态以进行操作,而无需使用消息传递范式(公共成员)。这打破了对象的封装。

Java, on the other hand, has main()as just a static method of a class so it's closer but it still has non-object things in it.

另一方面,Javamain()只是一个类的静态方法,所以它更接近但它仍然包含非对象的东西。

Smalltalk is the lingua franca normally held up as the purest of the pure, but I don't know enough about it to comment.

Smalltalk 是通常被认为是最纯粹的通用语言,但我对此知之甚少,无法发表评论。

Me, I tend to leave those sort of arguments for the intelligentsia while I get on with developing code and delivering to my clients :-)

我,当我继续开发代码并交付给我的客户时,我倾向于将这些论点留给知识分子:-)

回答by dirkgently

Define OOL. If you mean using classes etc, then C++ supports OO-style of programming among others. There's nothing that stops you from not using classes. Java OTOH, does not allow for but classes. (Yes, I do know Java supports FP.)

定义 OOL。如果您的意思是使用类等,那么 C++ 支持 OO 风格的编程等。没有什么可以阻止您不使用类。Java OTOH,不允许但类。(是的,我知道 Java 支持 FP。)

回答by Dror Helper

The short answer is no - C++ is not entirely OO language. You can write "not exactly" OOP using C++ even without resorting to using the C subset. One such example is your main method - which is not contained in any class.

简短的回答是否定的 - C++ 不完全是面向对象的语言。即使不使用 C 子集,您也可以使用 C++ 编写“不完全”的 OOP。一个这样的例子是你的 main 方法——它不包含在任何类中。

The main reason is the fact that C++ originated from C - when Stroustrup created the language he was aiming to create a new version of C (with classes). in fact he have tried to submit his creation as the new flavor of C (C84).

主要原因是 C++ 起源于 C——当 Stroustrup 创建语言时,他的目标是创建 C 的新版本(带有类)。事实上,他曾试图将他的创作提交为 C 的新风味 (C84)。

回答by reko_t

C++ is not a pure object oriented language, and as already mentioned nothing forces you to use OOP concepts in C++. C++ is what you call a hybrid object oriented language, as it's based on C which is purely a procedural language.

C++ 不是纯粹的面向对象语言,正如前面提到的,没有什么会强迫您在 C++ 中使用 OOP 概念。C++ 是您所说的混合面向对象语言,因为它基于纯粹的过程语言 C。

Examples of pure object oriented languages are C# and JAVA.

纯面向对象语言的例子是 C# 和 JAVA。

回答by Omnifarious

No, it is not a purely object oriented language. In particular primitive datatypes in C++ have rules that are frequently different from datatypes that aren't primitive. Additionally it is possible to have functions that are not associated with any datatype at all. There are a myriad of other ways in which C++ is not a pure object oriented language, but those are two of the biggest reasons.

不,它不是一种纯粹的面向对象语言。特别是 C++ 中的原始数据类型具有与非原始数据类型经常不同的规则。此外,还可以使用根本不与任何数据类型相关联的函数。C++ 不是纯面向对象语言的原因还有很多,但这是两个最大的原因。

Neither Java nor C# are pure object oriented languages either because they have primitive datatypes that do not obey the same semantics as 'object' datatypes.

Java 和 C# 都不是纯面向对象的语言,因为它们的原始数据类型不遵循与“对象”数据类型相同的语义。

回答by Prashant Rane

of course not!! It supports intrinsic data types.

当然不是!!它支持内部数据类型。

回答by Rahul Goyal

C++ is nothing but "C with classes". I can still write a C program and save it as .cpp file. So, Proof by implication says "C++ is not a purely object oriented language."

C++ 只不过是“带有类的 C”。我仍然可以编写一个 C 程序并将其保存为 .cpp 文件。因此,暗示证明说“C++ 不是一种纯粹的面向对象语言。”

回答by user3111783

The reason why C++ is not a OOP language is mainly because it is missing the concept of encapsulation. You can't define an interface/contract to your object because of the pointers which give you total control on everything.

C++之所以不是OOP语言,主要是因为缺少封装的概念。由于指针可以让您完全控制一切,因此您无法为对象定义接口/合同。