简体   繁体   English

Why C language isn't Object Oriented language even though it has (Structures) struct keyword which is same as Class keyword of C++?

[英]Why C language isn't Object Oriented language even though it has (Structures) struct keyword which is same as Class keyword of C++?

In a recent interview I was asked a question that what is the difference between Class and Structures which led me to think that if the difference is just about access specifier and we can use struct keyword in C then why isn't C object oriented. In a recent interview I was asked a question that what is the difference between Class and Structures which led me to think that if the difference is just about access specifier and we can use struct keyword in C then why isn't C object oriented. Can anyone solve my doubt?谁能解决我的疑惑? This doubt might be naive but curiosity has it's own way.这种怀疑可能很天真,但好奇心有它自己的方式。

A problem with this question is that in order to give an answer, we first need to define "what is an object oriented language".这个问题的一个问题是,为了给出答案,我们首先需要定义“什么是面向object的语言”。

An OO language like C++ has many features mapped into the language but that doesn't mean that any language need to have all thoses feature to be considered object-oriented.像 C++ 这样的 OO 语言具有映射到该语言的许多特性,但这并不意味着任何语言都需要具有所有这些特性才能被视为面向对象。

What is required?需要什么?

  • Encapsulation?封装?
  • Inheritance? Inheritance?
  • Polymorfism?多态性?
  • ... and so on... ... 等等...

To me one of the most important features is encapsulation.对我来说,最重要的特性之一是封装。 With encapsulation you tie together the data that describes the object and the operations you can do with the object.通过封装,您可以将描述 object 的数据和您可以使用 object 执行的操作联系在一起。

For instance a car object.例如汽车 object。 It will have data members to describe its color, type of gear, number of gears, number of wheels, current speed and so on.它将有数据成员来描述它的颜色、齿轮类型、齿轮数量、车轮数量、当前速度等。 Then it will have (member) functions to operate the car.然后它将具有(成员)功能来操作汽车。 You can accelerate, break, turn left and so on.你可以加速,打破,左转等等。 The operations and the data are encapsulated, ie when an operation is carried out on an object by calling a function, all data describing the object is available to the function. The operations and the data are encapsulated, ie when an operation is carried out on an object by calling a function, all data describing the object is available to the function.

This encapsulation is a key feature of object oriented programming.这种封装是 object 面向编程的一个关键特性。

Encapsulation can't be handle by C. C 无法处理封装。

We can make a struct with data members.我们可以用数据成员制作一个struct We can even add (member) functions (aka function pointers) for the operations.我们甚至可以为操作添加(成员)函数(又名 function 指针)。 So using proper initialization of a C struct object, we can write OO-style code like因此,使用 C 结构 object 的正确初始化,我们可以编写 OO 风格的代码,如

mycar.getSpeed();

but...但...

C has no automatic relation between the members. C 成员之间没有自动关系。 We can't call a member function and automatically have access to the data of that object.我们不能调用成员 function 并自动访问该 object 的数据。 C has no concept of encapsulation. C 没有封装的概念。

In C when calling a member function that needs access to data members of the same object, we need to pass (the address of) the object itself in order to get access to the data. In C when calling a member function that needs access to data members of the same object, we need to pass (the address of) the object itself in order to get access to the data. Like:喜欢:

mycar.getSpeed(&mycar);
\------------/ \^^^^^/
   oo style      but explicit passing pointer to
    call         the object violates the encapsulation principle

In a object oriented language this is often called the this pointer, and it is available implicit - you don't need to pass it.在面向 object 的语言中,这通常被称为this指针,它是隐式可用的——你不需要传递它。 But in C you will have to pass it explicit.但是在 C 中,您必须明确传递它。 So the benefit of using function pointer members to get OO-style operations is (typically) small - we can just as well do:因此,使用 function 指针成员来获得 OO 样式操作的好处(通常)很小 - 我们也可以这样做:

getSpeed(&mycar);

The lack of encapsulation is one reason that C isn't object oriented.缺乏封装是 C 不是面向 object 的原因之一。

In C++, the main difference between a struct and a class is that members of a struct are public by default, while members of a class are private by default.在 C++ 中, structclass的主要区别在于struct的成员默认是公共的,而class的成员默认是私有的。

A struct in C is different because it cannot contain member functions nor does it support inheritance. struct中的结构不同,因为它不能包含成员函数,也不支持 inheritance。 It is only a means of aggregating variables of differing types.它只是一种聚合不同类型变量的方法。 While it can contain function pointers, they are not inherently tied to a given object like member functions in C++.虽然它可以包含 function 指针,但它们本身并不与给定的 object 相关联,就像 C++ 中的成员函数一样。

While the C language defines an "object" as "region of data storage in the execution environment, the contents of which can represent values" (C11 3.15), a struct is not an object in terms of being object-oriented, ie an encapsulated set of data and the functions that operate on them.虽然 C 语言将“对象”定义为“执行环境中的数据存储区域,其内容可以表示值”(C11 3.15),但就面向对象而言, struct不是 object,即封装一组数据和对它们进行操作的函数。

In C++ class and struct are keywords that can be used to declare a class.在 C++ classstruct是可用于声明 class 的关键字。 The fact that C is missing the keyword class does not make it less object oriented. C 缺少关键字class的事实并没有减少 object 导向。 What does it make "less object oriented" is that C structures cannot have member methods, in particular they do not have constructors nor destructors.是什么使得“较少面向 object”是 C 结构不能有成员方法,特别是它们没有构造函数或析构函数。

However, there is no clear cut between object oriented and non-object oriented languages.但是,面向 object 的语言和非面向对象的语言之间并没有明确的界限。 Object orientation is a paradigm, and languages can natively support it more or less. Object 方向是一种范式,语言本身或多或少地支持它。 For example, is Python not object oriented because it has no "real" encapsulation?例如,Python 是否不是面向 object 的,因为它没有“真正的”封装? Certainly not.当然不是。 Moreover, consider that C++ started as C with classes.此外,考虑到 C++ 以 C 和类开头。 Many object oriented features that are built-in in C++ can be emulated in C. C++ 中内置的许多面向 object 的功能可以在 C 中进行仿真。

why isn't C object oriented为什么不面向 C object

Short answer - C wasn't designed with object-oriented programming in mind.简短的回答 - C 在设计时并未考虑到面向对象的编程。 C++ was. C++ 是。

The chief difference between a struct in C and a class or struct in C++ is that you can't (easily) associate code with a struct in C. The chief difference between a struct in C and a class or struct in C++ is that you can't (easily) associate code with a struct in C. You can't execute a method on a struct instance in C, such as不能对 C 中的struct实例执行方法,例如

list.sort(); 

or或者

sequence.average();

Instead, you have to pass those struct instances as arguments to functions, like相反,您必须将这些struct实例作为 arguments 传递给函数,例如

sort( &list );        // have to pass a pointer since we're going to modify list
average( sequence );

C just doesn't provide the kinds of tools that make object-oriented programming easy - no encapsulation, no inheritance, no polymorphism (well, there's _Generic , but it's not quite the same). C 只是没有提供使面向对象编程变得容易的工具种类 - 没有封装,没有 inheritance,没有多态性(嗯,有_Generic ,但并不完全相同)。

You can do object-oriented programming in C, but it is a lot of work for arguably little benefit.可以在 C 中进行面向对象的编程,但可以说是做了大量工作而没有什么好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM