C++ / C / Objective-C

Posted:
in General Discussion edited January 2014
Heya,



I currently have experience with Java, and as far as programming experience goes, have only worked with object oriented languages.



I'm no expert Java programmer, and my expertise only runs to stuff like the basics of linked lists, b-trees, avl's, hash tables etc... (only know what i've learnt from my comp sci degree (starting 2nd year now)).



But I'd like to pickup either C++ / C / or Objective-C in the near future and here's my criteria:



I'm already enrolled in a C++ course next semester, but i'd also like to be able to write some small applications for OSX, however, it seems most people recommend\\prefer Obj-C (most supported by apple it seems). Or is this not true and you can do it in any language?



If I decided to learn C\\Obj-C (pick one) and C++ concurrently (have to take the class next sem so no choice), would this be unwise? I don't mind a syntax difference, but I realize that C is not object oriented like C++.



And I guess my last inquiry is, is it easy to pickup Objective-C after an intermediate level of C, and is it easy to transition from either of the former languages to C++?



The reason I ask these things is because I love working in OSX, and i'd love to develop for OSX as a hobby, but I'm also looking towards my future co-op (student company hiring opportunities) and also the jobs when I graduate w/ this comp sci bachelor degree.



Anyways, any thoughts would be greatly appreciated.



Thanks a ton.

Comments

  • Reply 1 of 17
    dfryerdfryer Posts: 140member
    Apple's "Carbon" framework (most similar to classic MacOS) is fully accessible from C or C++ (or for that matter, Objective-C). If you want to use Cocoa, their object-oriented framework from NeXT, you'll need to know Java or Objective-C.



    Objective-C isn't too hard to pick up if you know Java, C, and/or C++; it's mostly the Cocoa framework that will present the learning curve. Have you read Apple's primer on Objective-C?
  • Reply 2 of 17
    dfryer - Great, thanks for the reply. However, I have not read Apple's primer on Obj-C. Do you perhaps have a link to it? I think from my current standpoint, if I were developing for OSX, I'd be working with Cocoa only and not Carbon.



    Thanks again.
  • Reply 3 of 17
    dfryerdfryer Posts: 140member
    This URL http://developer.apple.com/cocoa/ has a lot of good resources, including a link to the primer I mentioned, somethink like "Object-Oriented Programming and the Objective-C language"



    By the way, I'm also a CS student, at the University of Victoria (just finishing 2nd year)
  • Reply 4 of 17
    amorphamorph Posts: 7,112member
    C -> Objective-C -> C++ is actually a pretty good route to go. C isn't OO, but once you've got it down the stranger corners of Objective-C and C++ won't seem strange to you at all. C has everything Java tries to insulate you from.



    Objective-C will give you clear, powerful OO capabilities, so once you've gotten comfortable with it you'll be better able to wrangle the beast that is C++ into some semblance of well-designed, well-structured code.
  • Reply 5 of 17
    ast3r3xast3r3x Posts: 5,012member
    Quote:

    Originally posted by Amorph

    C -> Objective-C -> C++ is actually a pretty good route to go. C isn't OO, but once you've got it down the stranger corners of Objective-C and C++ won't seem strange to you at all. C has everything Java tries to insulate you from.



    Objective-C will give you clear, powerful OO capabilities, so once you've gotten comfortable with it you'll be better able to wrangle the beast that is C++ into some semblance of well-designed, well-structured code.




    So C++ is still better/more-powerful then Obj-C? I know C++ (well command line stuff, nothing graphical) and I am starting to learn Cocoa. I never learned C because it seems like a dying language because it's not OO.



    Honestly I don't really want to learn C either, I can't imagine how you do things with a language that isn't OO.



    So can you make programs for OS X with C++ syntax just calling classes of Cocoa? If so, how/where can I look on how to do this?
  • Reply 6 of 17
    Great, thanks for all the info guys.



    As far as I know, Apple and I guess the developers for OSX, use Objective-C.



    But in the game industry, I know that C and C++ are both used concurrently since some time-efficient routines are written in C because of its ability and nature to work at a lower machine language level (and hence faster) than C++.



    However, it seems outside of these two things, C++ is the most widely used.



    Correct me if I'm wrong though. Most of this is from what I read around the net with no concrete backing of evidence.



    I think that the C --> Obj-C --> C++ approach is a good one too.
  • Reply 7 of 17
    dfryerdfryer Posts: 140member
    Technically both C++ and Obj-C completely or almost completely contain C - any valid C is valid C++. Things like 'virtual function calls' in C++ and 'messages' in Obj-C are slower than direct calling of a function because of the overhead involved in figuring out which function to call.



    C++ has things like templates and operator overloading, as well as more arcane funkiness that cause some people to consider the language ugly and bloated (from the perspective of trying to write a C++ compiler).



    Objective-C has very flexible message passing which enables cool things like being able to send a message to an object on a remote server without having to worry about whether it's remote or not. (Well, not sure if that's *entirely* true, but...)



    C++ is definitely more widely used than Objective-C, and any C library can be used without trouble from either Obj-C or C++. Either way, pretty well anyone learning C++ will automatically pick up C, and the Objective-C documentation pretty well assumes a working knowledge of C, since it is actually just a few powerful extensions to the language.



    Trickier than the syntax, however, is the design strategies involved. Things like memory management are often very subtle to the uninitiated; Java handles *most* of that by itself. C makes you do all of the work yourself; Cocoa provides reference counting facilities (through NSObject) and you can do funky tricks with C++ like "smart pointers" to make things easier on yourself.



    I learned Pascal, then some Object Pascal & C at the same time, then a little bit of C++, then some Objective-C (although I've never really used it, unfortunately) and then some more C++ while playing with an open source 3D engine. Learning Objective-C before C++ *might* make you a better C++ programmer in terms of design strategies, but every language has its own way of doing things. (Take a look at Perl. Sheesh.)



    Hm.. not sure if I actually had a point.
  • Reply 8 of 17
    dfryerdfryer Posts: 140member
    Quote:

    Ast3r3x:

    So C++ is still better/more-powerful then Obj-C? I know C++ (well command line stuff, nothing graphical) and I am starting to learn Cocoa. I never learned C because it seems like a dying language because it's not OO.





    C++ is different than Obj-C. It has some features that Objective-C doesn't have; some people would argue that those features are hacks so that it can do things that Obj-C can already do, but I don't think that's entirely true. Powerful is definitely not equal to better; "powerful" constructs can be easily abused



    You cannot interact with objective-C objects using ordinary C++ syntax. The only way to use the two together is using Objective-C++ which allows you to use C++ syntax with C++ objects and Obj-C syntax with Objective-C objects.
  • Reply 9 of 17
    amorphamorph Posts: 7,112member
    Actually, learning C++ won't necessarily teach you C, because in practice C++ programmers seem to avoid the almost functional idiom that developed with C - and besides, the language is so damn big that it's hard to keep C expression syntax, C++'s extensions, and the STL all in your head at once. Most of the C++ tutorials I've seen ignore C as much as possible, and a depressing number of them are flat-out wrong when they do try venturing into pure C. As such, I'd recommend learning C if you want to learn C.



    Objective-C is such an elegant extension of C that I've seen C used far more proficiently within Objective-C code - Objective-C just doesn't require that much extra, if you can already think in OO terms.



    C is not necessarily faster than C++ or Objective-C (ignoring, for a moment, that you can write C++ or Objective-C programs that are also legal C): For example, there are instances where Objective-C's aggressive message caching allows it to outrun a C function call, and for the many instances where that's not true, you can even resolve a message to a static function call e.g. for use in a tight loop.



    C++ isn't just complex for compiler writers either (although the fact that it is has seriously hampered its portability in practice). It's a pristine example of design by committee, and it has no governing paradigm at all. It's not an object oriented language. It has attributes that can be used that way, but I've also heard of a guy who implemented PROLOG style predicates by chaining objects together and writing some clever destructors. And, of course, you can write plain C, too, or some monstrous write-only contraption that doesn't fit neatly into any category. You have to impose a paradigm upon C++ to use it effectively.
  • Reply 10 of 17
    Thanks for all the information guys.



    Gathering from here, I'll put my time into C first.



    Can anyone recommend any good books perhaps?
  • Reply 11 of 17
    Try

    C Programming A Modern Approach by K.N.King ISBN 0-393-96945-2 which is a fanrtastic and expensive book. other wise check out

    http://www.accu.org/bookreviews/public/

    for their recommanded book.

    Jay
  • Reply 12 of 17
    ast3r3xast3r3x Posts: 5,012member
    Can someone please explain why learning a langauge that is dying (or it seems like it to me at least) and not OO (the way of the future i think) is worth while? I know people like C and alot...ALOT of languages kinda are close to it or based on it/can use it's classes. What are its advantages and what does it have over say C++ or Cocoa?



    I am not trying to put down C at all, just pure ignorance here.
  • Reply 13 of 17
    amorphamorph Posts: 7,112member
    Also, the definitive The C Programming Language by Brian Kernighan and Dennis Ritchie is a must have, although it's dense enough to be a better reference than tutorial. Ritchie designed and implemented C, so he knows a thing or two about it.



    Quote:

    Originally posted by ast3r3x

    Can someone please explain why learning a langauge that is dying (or it seems like it to me at least) and not OO (the way of the future i think) is worth while? I know people like C and alot...ALOT of languages kinda are close to it or based on it/can use it's classes. What are its advantages and what does it have over say C++ or Cocoa?



    OO is not the best solution to every problem. Sometimes you just want to buckle down and crunch numbers, which is why FORTRAN is alive and well in the sciences and in engineering. So, learning C teaches you some valuable things: How to use pointers to their full power; how to use C++ and Objective-C expression syntax (which is basically C expression syntax) to its full and remarkable power; and how to organize code into functions. From there, you can move to Objective-C and gain the vastly superior organizational abilities of OO when they are advantageous (which, granted, is almost all of the time), and still be comfortable slinging pointers around and writing non-trivial expressions.



    At that point, C++ will probably be less intimidating, if more frustrating.
  • Reply 14 of 17
    pyr3pyr3 Posts: 946member
    Too lazy to read all that, but I have looked into Obj-C as a C++ programmer. I can say that to me it seems like Obj-C and C++ are just two different forks of the C language. They each went off in a direction trying to take C and mold OO into it.
  • Reply 15 of 17
    yevgenyyevgeny Posts: 1,148member
    Amorph has doen a good job of saying this, but as a programmer myself, I want to say it again (and give some other thoughts):



    Although C is a subset of C++, the two are fundamentally different in terms of how they are used to write programs. C++ really should be used with the object oriented programming paradigm. C can't do OOP.



    C++ is considered to have a bloated syntax, and to be a pain from the perspective of the compiler writer. These are probably both true, but C++ also does quite a bit that isn't available in other more modern languages (e.g. templates).



    C++ is basically the accumulation of all the good programming ideas in the last 20 years, with some of the bad ideas. Want exception handling? It's there. Want templates? They're there. Want OOP? It's there. Want function pointers? They're there. Want to manage your own heap memory? That's there as well.



    Objective C from what I have heard is a good language, but limited to Apple development only. If you want to develop code for the Mac, then look into it, else don't.



    C is mostly a dying language, although it still has its uses in many applications (embedded computing, and most importantly, OS development where its portability is extremely useful). Nobody writes a large app in C.



    No, if you learn Java, you will not learn how to program (good) C++. The OOP ideas are the same, but you have to do memory management on your own in C++. Regrettably Java lacks some basic, useful things like typedefs. If you learn Java first, it will be conceptually difficult for you to go to C++, but if you learn C++ first, you will only have some unqueasiness about programming in Java (and a greater appreciation for what Java does give you).



    C++ gives you direct access to memory. Pointers are your friends and pointer arithmetic is your new girlfirend. People look down on such barbarisms, but you actually get a feel for how a computer works in C++ than in Java. (this isn't a slight against Java, just a complaint that some Java programmers are really ignorant about how computers work because they are so well abstracted from the CPU).



    Just some thoughts. Have fun, and remember to take a break and get a walk when your computer refuses to do what you think you told it to do.
  • Reply 16 of 17
    Ah, that's a lot of good information you guys have posted.



    The professor who taught my intro to programming class (using Java) was somewhat pro-Java He's always talking about how Java does this for you and that for you in an automated fashion, where C++ requires you to do it all yourself.



    He did bring up several disadvantages in both languages though, as some of you have mentioned.



    I'll be learning C++ next semester no matter how I look at it, but I do want to try and start C concurrently so as to migrate to Obj-C as soon as I can...



    Thanks again to everyone for the replies - definately learning a ton here.
  • Reply 17 of 17
    amorphamorph Posts: 7,112member
    Quote:

    Originally posted by Wayne Lau



    I'll be learning C++ next semester no matter how I look at it, but I do want to try and start C concurrently so as to migrate to Obj-C as soon as I can...




    Be aware that there is no way in the nine Hells that you'll learn C++ in a semester. If the teacher's good, you'll get a good sense for how to use its OO capabilities, but there'll be a lot left. C++ is enormous, and it's full of all kinds of wierd little corners. Now, if you give yourself two years, you'll have a pretty good command of it by then, if you work at it. But a lot of "C++ programmers" seem content to muddle along in their corner of the language and not worry about the rest.
Sign In or Register to comment.