开发者

what are the major differences between C and Objective C. Is abstraction possible in C?

开发者 https://www.devze.com 2023-01-18 07:53 出处:网络
what are the major differences bet开发者_Python百科ween C and Objective C. Is abstraction possible in C?Of course abstraction is possible in C. It\'s possible if you\'re writing code by setting bits w

what are the major differences bet开发者_Python百科ween C and Objective C. Is abstraction possible in C?


Of course abstraction is possible in C. It's possible if you're writing code by setting bits with a "magnetized needle and a steady hand".

A function is a unit of abstraction. If I am representing a deck of cards as an array of char[52], then I can write a function to shuffle it. After I've written that function, I don't ever have to think about how to shuffle it again. I can just say, shuffle(deck);

Note that I could do that writing in assembly, a hex editor or with a magnetized needle.


If anyone ever asks you, "Can A done by language B be done in C?" The answer is always "yes", because all programs are really C programs. Allow me to illustrate.

Objective-C is C, with a library, written in C, to handle message passing that make abstraction easy.

Ruby, Python, Perl? They are all C programs interpreting large text files that define how to process other text files. Ergo, all Perl, Python and Ruby programs are C programs with insanely large data sets.

LISP started off as a C program to count parentheses that went to college to get a PhD in Abstract Math, then shacked up with a plus-sized editor and now distracts programmers from writing C programs with in-editor Tetris, e-mail and IRC.

Java is an Abomination. Written in C. But written by multiple groups of people who didn't bother talking to each other and thus came up with several C programs all named Java to processes the same Java data sets into different, incompatible results. Web browsers, fearing a "write-once, test-everywehre" gap, created JavaScript (in C) to ensure no project could ever be simple ever again.

Yacc is a C program to create more C programs. Some scientists believe Yacc to be a proto living organism, as evidenced by its ability to recreate itself. Others believe Yacc to be the natural results of a C program spun at 5400 RPM until it gets dizzy and makes a mess on the carpet.

Of course, C itself is written in C to convert C source to assembly which is assembled into executable machine code by an assembler.

Written in C.


Briefly, Objective-C adds object-oriented features to C.

Abstraction is possible in pretty much every programming language in common use. All that abstraction really means is "Hiding the details of how something is implemented".

Programming is all about creating abstractions. When you design a function, that's an abstraction. For example, if the function adds up all of the elements of the array, the abstraction is letting you think "Hey, I'm adding up the elements of this array" instead of thinking "Ok, let me create a counter variable, and then set it to the first element in the array, and then grab that value, and add it to the sum, now increment the counter and keep going if I haven't gotten to the end of the array".

Barry's answer is an example of a slightly more complicated abstraction, where you create an abstract data type. For example, struct _myADT might be struct circle and you might have functions to create a circle, measure its area, resize it, etc., etc. In reality, you're passing around a pointer to a location in memory that contains the circle's center, radius, etc and so on, but the abstraction is that you're passing around a representation of a circle.

The difference between Objective C and C is that C isn't very good at creating abstract data types. You have to go through all kinds of trouble to hide the details of what's going on inside a struct, and someone can always decide "I don't really need your abstractions, let me reach in and twiddle these bits because I know better than you". In object-oriented languages like Objective-C, you can actually enforce your abstractions by deciding what members of your objects can be accessed, and how they can be accessed.


Yes, abstraction is certainly possible in C (see below), as it is in virtually any programming language. The difference between C and Objective-C is not about abstraction but about dependencies. The explicit purpose of Objective-C is to decouple dependent modules so that large, complex systems don't become so unweildy for developers. In particular, Objective-C's runtime-bound message passing (instead of compile/link-time bound function calls) allows modules to be less tightly coupled. The calling module needs to know only the name, not the binary representation of a method to call it. Modules can thus be updated individually, replaced, or even modified at runtime without breaking all of the dependencies in the system.

For completeness, the classic approach to creating encapsulation and abstraction in C is the "abstract data type" pattern. In applying this pattern, C's declaration vs. definition split allows you to both encapsulate and abstract the implementation of a data type.

For example, in the public header

// MyADT.h
typedef struct _myADT *myADT;

myADT myadt_create();
void myadt_perform(myADT adt);
void myadt_free(myADT);

Clients could use the API without knowlege of the implementation of my data type. Definition of the struct and of the functions is in a separate (and potentially private) module:

// MyADT.c

typedef struct _myADT {
 ...
};

void myadt_create()
{
   struct _myADT adt = malloc(sizeof(struct _myADT));
   ...
   return adt;
}

//etc...


C++ Versus Objective-C

Short answer is that there is a huge difference. That link is an okay starting point.

0

精彩评论

暂无评论...
验证码 换一张
取 消