Is There A Difference...

Discussion in 'Mac Programming' started by Darkroom, Sep 12, 2008.

  1. Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #1
    ... between C++ and Objective-C? if so, what are their differences?
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    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.
     
  3. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    One of the major ones is that C++ supports multiple inheritance while Objective-C does not.
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    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?"
     
  5. Moderator emeritus

    sushi

    Joined:
    Jul 19, 2002
    Location:
    キャンプスワ&#
    #5
    Good analogy.
     
  6. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #6
    I could be quite pedantic with that but I'll save it for myself :).
     
  7. Moderator emeritus

    sushi

    Joined:
    Jul 19, 2002
    Location:
    キャンプスワ&#
    #7
    You are most kind! :)
     
  8. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #8
    What? And deprive of us of a tiny amount of amusement or interest late on a Friday afternoon? I say post it...
     
  9. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    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
     
  10. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #10
    Hey, for some of us it's the middle of the morning!
     
  11. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #11
    Sorry, I'm going home in 5 minutes :p
     
  12. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #12
    so there's no benefit to studying C++ for mac programming? advancing from studying C to Objective-C is a good path to follow?
     
  13. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #13
    That's basically correct yes. Although if you are looking at the wider world C++ is use a lot more than Objective-C.
     
  14. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #14
    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
     
  15. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #15
    do ruby and python have any advantages over cocoa?
     
  16. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #16
    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++.
     
  17. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #17
    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
     
  18. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #18
    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?
     
  19. macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #19
    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
     
  20. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #20
    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.
     
  21. thread starter Guest

    Darkroom

    Joined:
    Dec 15, 2006
    Location:
    Montréal, Canada
    #21
    that actually explains a lot... thanks...

    and thanks for all the comments everyone.
     

Share This Page