PDA

View Full Version : Is There A Difference...




Darkroom
Sep 12, 2008, 09:57 AM
... between C++ and Objective-C? if so, what are their differences?



robbieduncan
Sep 12, 2008, 10:05 AM
Yes. They are entirely different languages with different syntax, compilers, "standard" libraries and capabilities. I can't even thing where to start listing the differences as they are not related to each other by much more than both being based off C (although Objective-C is a strict C superset and C++ isn't) and both being object oriented.

Cromulent
Sep 12, 2008, 10:08 AM
One of the major ones is that C++ supports multiple inheritance while Objective-C does not.

robbieduncan
Sep 12, 2008, 10:11 AM
One of the major ones is that C++ supports multiple inheritance while Objective-C does not.

And Objective-C has categories which C++ doesn't.

Edit to add: This question is a bit like saying "I'm an English speaker. Are French and German the same?"

sushi
Sep 12, 2008, 10:14 AM
Edit to add: This question is a bit like saying "I'm an English speaker. Are French and German the same?"
Good analogy.

Cromulent
Sep 12, 2008, 10:25 AM
Edit to add: This question is a bit like saying "I'm an English speaker. Are French and German the same?"

I could be quite pedantic with that but I'll save it for myself :).

sushi
Sep 12, 2008, 10:28 AM
I could be quite pedantic with that but I'll save it for myself :).
You are most kind! :)

robbieduncan
Sep 12, 2008, 10:30 AM
I could be quite pedantic with that but I'll save it for myself :).

What? And deprive of us of a tiny amount of amusement or interest late on a Friday afternoon? I say post it...

lee1210
Sep 12, 2008, 10:34 AM
To confuse things further, you can mix Objective-C and C++ (this is generally referred to as Objective-C++).

We have had many (sometimes heated) discussions about Objective-C and C++ here, the most prolific of which is here:
http://forums.macrumors.com/showthread.php?t=246143

I won't have too much to add to what people have already stated, but at the basest level the languages are related only in-so-far as they were based (to different degrees) on C. If you look at a routine dealing with primitives (and structs, which I would consider primitive) in C++ and Objective-C they will look similar, but if you look at something dealing with either language's object models, they will look totally different (although now, with the ability to access properties in Objective-C 2.0 using dot notation, that would look a little more similar).

I guess to speak in broad terms, the main differences are the object models. Most of the major differences come from how objects are defined, how their properties are accessed, how their behaviors/functions/methods are invoked, inheritance models, abstract objects vs. protocols, etc. Other "smaller" differences like operator precedence are really differences in C and C++, and that difference was just inherited from C by Objective-C.

-Lee

lee1210
Sep 12, 2008, 10:35 AM
What? And deprive of us of a tiny amount of amusement or interest late on a Friday afternoon? I say post it...

Hey, for some of us it's the middle of the morning!

robbieduncan
Sep 12, 2008, 10:54 AM
Hey, for some of us it's the middle of the morning!

Sorry, I'm going home in 5 minutes :p

Darkroom
Sep 12, 2008, 10:58 AM
so there's no benefit to studying C++ for mac programming? advancing from studying C to Objective-C is a good path to follow?

robbieduncan
Sep 12, 2008, 11:00 AM
so there's no benefit to studying C++ for mac programming? advancing from studying C to Objective-C is a good path to follow?

That's basically correct yes. Although if you are looking at the wider world C++ is use a lot more than Objective-C.

lee1210
Sep 12, 2008, 11:23 AM
so there's no benefit to studying C++ for mac programming? advancing from studying C to Objective-C is a good path to follow?

Cocoa is the chosen path going forward, so Objective-C it is. If you want to branch out Apple seems to have blessed python and ruby by providing a Cocoa bridge in Leopard, making them "first class" languages on OS X. If you just interested in diversifying and getting experience with a different language, one of those might be good.

-Lee

Darkroom
Sep 12, 2008, 12:47 PM
do ruby and python have any advantages over cocoa?

gnasher729
Sep 12, 2008, 12:52 PM
so there's no benefit to studying C++ for mac programming? advancing from studying C to Objective-C is a good path to follow?

There isn't "no benefit". Let's say you learned C. Most things you learned in C are useful in C++ and Objective-C as well. Then at some point you'd need to learn about object-oriented programming. You could learn Objective-C, or C++, or Java (with Java, the C knowledge is less helpful). You learn two things: The syntax of the language, and the concepts of object-oriented programming. If you know object-oriented programming, then learning to use either C++ or Objective-C in a useful way is much easier.

There is also the question whether you are going to share code between a Macintosh and a non-Macintosh application. In that case it is likely that the user interface on the Mac is written with Cocoa in Objective-C, and the Linux or Windows user interface is written with different libraries in a different language. However, a lot of the code that does the actual work could be written in a different language, for example C++.

lee1210
Sep 12, 2008, 12:54 PM
do ruby and python have any advantages over cocoa?

Cocoa is an Objective-C API, not a language. As of Leopard you can now access the Cocoa API from python or ruby.

I don't consider any language superior to any other. The only comparison is for the task at hand. If you're doing something that is well-suited to Objective-C, use that. If you're doing something well-suited to python or ruby, or you are more comfortable performing the task with those tools, go for it.

-Lee

kainjow
Sep 12, 2008, 12:55 PM
do ruby and python have any advantages over cocoa?

Ruby/Python are languages. Cocoa is a library of code accessible via Objective-C, but you can also access it via Ruby and Python (and others) in Leopard (and in Tiger via third-parties).


C++ is still useful if you're doing Mac programming. A lot of open source code you may want to use is written in C++. Also a lot of older programs for the Mac still use C++ underneath. Also it's great for writing cross platform code. Put most of your logic in C++ and write native UI wrappers around it.

I will say though, I see more C code than C++. I'm not really sure why that is. Anyone want to give some insight into that?

lazydog
Sep 12, 2008, 12:56 PM
so there's no benefit to studying C++ for mac programming? advancing from studying C to Objective-C is a good path to follow?

I think I have to disagree with the others on this… if you know C++ then you'll appreciate how powerful Objective-C++ is/can be. But if you don't already know C++ then I would steer clear, at least until you've learned Objective-C.

b e n

robbieduncan
Sep 12, 2008, 01:41 PM
I will say though, I see more C code than C++. I'm not really sure why that is. Anyone want to give some insight into that?

In library code or self-contained in applications. It makes sense for libraries as C is a kind of lowest common denominator: you can call C libraries from almost any language. Not really so of C++ libraries.

Self contained in apps is harder to explain. I think it's possible because a lot of the more old-school programmers learnt C and then picked up C++ afterwards and the old C habits die hard. So you end up seeing C++ that is really more like procedural C.

Darkroom
Sep 12, 2008, 02:04 PM
Cocoa is an Objective-C API, not a language. As of Leopard you can now access the Cocoa API from python or ruby.

I don't consider any language superior to any other. The only comparison is for the task at hand. If you're doing something that is well-suited to Objective-C, use that. If you're doing something well-suited to python or ruby, or you are more comfortable performing the task with those tools, go for it.

-Lee

Ruby/Python are languages. Cocoa is a library of code accessible via Objective-C, but you can also access it via Ruby and Python (and others) in Leopard (and in Tiger via third-parties).


C++ is still useful if you're doing Mac programming. A lot of open source code you may want to use is written in C++. Also a lot of older programs for the Mac still use C++ underneath. Also it's great for writing cross platform code. Put most of your logic in C++ and write native UI wrappers around it.

I will say though, I see more C code than C++. I'm not really sure why that is. Anyone want to give some insight into that?

that actually explains a lot... thanks...

and thanks for all the comments everyone.