Coding for X in Objective C and C++

Posted:
in Genius Bar edited January 2014
Apple seems to use Objective C religiously for coding Cocoa apps, and all their examples are in Objective C.



But, I am learning C++ and want to be able to code in C++. I know Project Builder can compile C++ (it's what I do my assignments in).



My question is, is it possible, and if so, how, can I incorporate C++ with the Objective files that Interface Builder creates.



Any help, or links to somewhere I can learn about this would be greatly appreciated.



Code Master (in training )
«13

Comments

  • Reply 1 of 44
    airslufairsluf Posts: 1,861member
  • Reply 2 of 44
    you can use the terminal to compile your c++ code that way you can write it normally if you don't want to have to be confined to osx. just go to the terminal and type "man cc" and it will give you the instructions on how to use it. the cc command is actually just what project builder uses. and i actually suggest to look carefuly at the objective c code. the basics in your c++ class can be better applied in an objective format. email me at [email protected] and i can give you more info. i am a senior in highschool currently taking AP Computer Science so i know where you are coming from.
  • Reply 3 of 44
    applenutapplenut Posts: 5,768member
    [quote]Originally posted by myahmac:

    <strong>you can use the terminal to compile your c++ code that way you can write it normally if you don't want to have to be confined to osx. just go to the terminal and type "man cc" and it will give you the instructions on how to use it. the cc command is actually just what project builder uses. and i actually suggest to look carefuly at the objective c code. the basics in your c++ class can be better applied in an objective format. email me at [email protected] and i can give you more info. i am a senior in highschool currently taking AP Computer Science so i know where you are coming from.</strong><hr></blockquote>





    how is that course?



    my school just killed it due to lack of interest right before I was about to take it

    <img src="graemlins/hmmm.gif" border="0" alt="[Hmmm]" />



    Despite that, the tech guy at my school gave me the C++ textbook to try and due it on my own over the summer and next year.



    What else does the AP CS course entail?
  • Reply 4 of 44
    I don't want to start a language debate BUT C++ sucks hard. Using it as programming language when you have to is murder, using it to learn programming is madness - the bad habits you learn will haunt you for the rest of your career. I am currently learning / perfecting / dabbling with C, Objective-C, Java, Ruby, Eiffel and Haskell amongst others. Any of these languages will teach you the fundamentals of programming far better than C++.



    If you want to become a darwin/unix guru then learn a scripting language (Ruby is my choice) as well as Java or Objective-C for application development. In my opinion 'programmers' who only know one language in depth don't deserve the title.



    Finally, if you are interested in becoming a professional (or commited hobbyist)programmer then I would highly recommned the best book for learning the craft of software development: <a href="http://www.amazon.com/exec/obidos/ASIN/020161622X/qid=1014375213/sr=8-1/ref=sr_8_3_1/103-9898468-0627822"; target="_blank">the pragmatic programmer</a>. It changed my life - seriously (though it is aimed at those who already program rather than newbies).
  • Reply 5 of 44
    Interesting...



    Can you give a couple examples as to why C++ sucks so much?



    My C++ prof told me to not bother with Objective C if I didn't need to, but it seems that Objective is the more superior form. Just from the few examples that I'm given, I can see how Objective C works and it isn't that hard, but it looks the same as C++ with different syntax to me. How is this wrong?
  • Reply 6 of 44
    i think your original question was can you mix obj-c and c++ in your ProjBuilder projects. Yes. i'm almost certain. Obj-C I think gotta have ".m" or something like that as the extension, regular c is ".c", c++ is ".cpp", java is ".java". Add them to a project like adding any file (drag+drop, or project-&gt;add files...). if you put the wrong extension, project builder will get confused and try to treat it as the wrong type of source.



    terminal is well and good for compiling, but if your not familiar with unix/linux then your probably not familiar with gnu cc. the man page tells you to chk the developer docs, which then tell you to use the project builder, which you should use anyway.



    as far as languages go, c and cpp are the best. learning is tough on 'em though. i haven't yet tried obj-c much yet, looked at it and the syntax seemed phuqd. Java sux bug fricken donkey [explitive deleted]. It's so slow and disgusting (cip: LimeWire). I agree with bawjaws though, in that a programmer shouldn't be limited to any language. once u learned a harder one [and really know it], new ones are easy. the biggest prob i can think of with cpp is that with classes, and inheritance and that sh!t, theres overhead that you can't get around.



    some1 asked about ap cs, i'm takin it now (in college) and its basic stuff: the syntax, how to make fns, they tell u what an array is, a little bit about recursion, etc. you can chk out <a href="http://www.cs.rit.edu/~cs1"; target="_blank">www.cs.rit.edu/~cs1</a> for info on cs1, from Roch Inst. of Tech. I think AP cs is cs1&cs2, but it might just be cs1. @rit they're using java, but the same basic curriculum applies to all languages, just diff syntax.
  • Reply 7 of 44
    why C++ sucks:

    the main reason that C++ sucks is (surprisingly) culture. The people who choose to program in it over other languages are making the choice based on raw power and machismo. This is all very well if you are writing an OS kernel (but if you are then why not use C and apply OO concepts) but 99.99% percent of the time the wrong decision is made for the wrong reasons.



    It's like opening a pizza delivery business and equipping your delivery boys with scooters or strapping them to booster rockets. The extra power is just going to get you into trouble.



    The same people who make a decison like that also overemphasise low-level speed tuning over modularity, understandability and (v. important) high level optimization. Remember the golden rule of optimisation is: don't do it. Ask a C++ programmer about it and they will bore you for hours with pointless second guessing of compiler optimisations and old wives tales that they have never tested in real-life benchmarks - just crappy microbenchmarks. Smalltalkers or Java gurus say 'make it right, then make it fast'.



    NB This line of thinking did have some validity 15-20 years ago but Moore's law is making a mockery of it now and only mental inertia lets it hang on.



    real example:

    You can tell Java books written by C++ refugees. They are the ones that tell you the uppercase method of String is inefficent (because it supports unicode languages with strange casing rules) and you should roll your own for speed that only does ascii. The staggering idiocy of doing something like that flabbergasts me. The 80/20 rule states that 80% of your execution time is spent in 20% of your code. Even thinking about optimising before you find out which 20% is at fault is idiotic. Discussing it in a textbook before you have written a program (or learned the language) is disgusting.



    bottom line:

    it's horses for courses but unless your specifically aiming at a field where C++ is hanging on then avoid it like the plague. This goes double for people learning programming and developing the skills and habits of mind that will shape your future. C++ is the past, Java (and its evil twin C#) are a stepping stone to the future.



    PS the main advantage of Objective-C is that it does not enforce type safety and has a great application library (Cocoa). It is these two elements that make building GUI apps a joy in Objective-C compared with Java or C++ *shudder*.
  • Reply 8 of 44
    Freak:

    I know that you can add files of multiple types, but how do you make the classes interact with each other? Do I use Obj notation in the .m file when referring to a class in a .cpp?



    bawjaws:

    I have already noticed the advantage to the dynamic binding that Obj does and I like that flexibility. I guess I'll learn both and decide for myself which is better for my purposes. The fact that Apple uses Obj is a pretty good cue



    I'm vaguely familiar with the command line tools. I used cc to compile my C programs last term via the command line and that worked well, but then I figured out how to make terminal based apps using Project Builder and I use that now.



    Thanks for all your inputs.



    The Code Master
  • Reply 9 of 44
    pardon me, but it seems ridiculous to assume that if a prog'r uses cpp that s/he feels compelled to optimize everything. I'm in the middle of writing a game engine (in cpp), which is a case where optimization is excessively important (gotta get more fps), but when I write other things, if time is not a matter, i don't look to optimize. i realize that where speed isn't important its okay to be slow (and your practically guarantted to b slow in java). it seems kind of silly to me to read that you shouldn't optimize. prog'in time of course, shouldn't b wasted on speeding up everything (net effect is usually nil), but profiling first, and seekign the time hoggers.



    i dont know much about obj-c, or c#, but i dont like the idea of a proprietary language. i dont know if there are unix/linux/windoze compilers for obj-c, but i'm pretty sure that c# is ms' disgusting windows-only language.
  • Reply 10 of 44
    Objective C is not a propriety language, gcc will compile it as will most other C compilers.
  • Reply 11 of 44
    Objective C++ is a hack to deal with legacy C++ code being ported to Cocoa.For OSX Objective C is a much better choice.C++ is a very messy,poorly designed language.I've heard Brian Greenstone,the founder of Pangea Software describe it as "the language of the devil".

    A couple of elegant and powerful languages worth learning are PHP,and steve,my current

    favorite among the "little languages".
  • Reply 12 of 44
    applenutapplenut Posts: 5,768member
    Isn't C++ pretty much required these days though? it seems all the Computer Science curriculum in colleges and such are based around C++.



    It also seems like it is by far the most commonly used so wouldn't it be best to learn the standard?



    It's not the first time I have heard to stay away from C++. Several have told me to learn C and then move to Objective C and possibly Java as C++ would teach me bad habits.



    Bit confused at the reasons though
  • Reply 13 of 44
    airslufairsluf Posts: 1,861member
  • Reply 14 of 44
    Objective C is a tad slower than C++, it can be sped up somewhat by using static typing and binding,but doing that would miss the point of the language.Maybe I was a little harsh on C++,but there are a lot of weird inheritance problems that crop up,mysterious bugs that have no obvious cause.For Cocoa,Objective C is by far the better choice.One thing I don't like about Objective C though is the lack of dot syntax,it isn't possible to access a variable without writing accessor functions,which is a pain in the ass.Of course,Objective C is flexible enough that it is possible to write a single accessor function for every variable.
  • Reply 15 of 44
    applenutapplenut Posts: 5,768member
    wow guys. that was great and really helped a lot. Really appreciate it.



    So, considering I have no programming experience but definitely want to get into it deeply what would you guys reccomend as a starting language?
  • Reply 16 of 44
    Looks like pure C is a good start and from there head out to Objective C or Java if you're doing Cocoa



    I still don't know what all these 'bad habits' formed by C++ are.. it would be nice to avoid them while learning C++ for school.
  • Reply 17 of 44
    It seems you all want to/have to programm in C++ and hey, it's your call, but even so, Java is obviously the language to learn first. (In fact, I was under the impression that introductory CS courses were defecting in droves. Maybe the US is behind the times.)



    C is a bad choice as it is not OO. Many people think that there is a progression C -&gt; C++ or Java cos that's how they did it but they are fundamentally different paradigms and OO is easier to learn.



    reasons Java is better than C++ as a learning language:

    It is more OO (though not 100%)

    No explicit memory managment == no impossible bugs

    No crazy pointer arithmetic, *p++ = *q++ WTF?

    Junit is a great learning/programming tool

    relatively easy GUIs

    Popularity, (C++ has an installed base but Java has all the cool projects/buzz/mindshare and is more likely to get you chicks)

    Exceptions

    Interface based multiple inheritence

    Standard libraries by and large are examples of good practice and OO patterns.

    and many others...



    At the end of the day a well written Java program will look very similar to a well written C++ program. But a badly written C++ program will look a hell of a lot worse.



    For Code Master, C++ bad habits to avoid:

    writing procedural code cunningly disguised as OO

    lines of code shouldn't have side effects so don't use the variable++ or ++variable operator except on a line by itself.

    wanting to overload an operator is a sign that you don't completely understand OO

    remember, code that takes up less space on the screen doesn't necessarily run faster.

    Objects, polymorphism and dynamic dispatching slow down your programs. The guys who invented them knew this and still felt is was a worthwile trade-off over 20 years ago. They were far better programmers than any of us will ever be (no offense) and had far less processing power to play with. Meditate on that the next time you attempt to 'avoid the overhead' of a dynamic method call.

    avoid multiple implementation inheritence, it'll bite you in the ass when your not looking

    just because it compiles doesn't mean it does what you think it does. Static type checking isn't the end of the story.



    Again, (and I don't know how well this will go over with CS types) culture is important in programming. Unit testing, refactoring, code clarity, agile methodologies (like extreme programming) and advanced use of object orientation are not enforced by Java any more than they are in C++ but it is a fact that they are more prevelant in the culture and that makes it easier to learn good programming habits from the books, people, courses and source code examples that are available.



    one last thing, while i'm up on my high horse. Computer Science != Software Development. If you ever find yourself reimplementing a search algorithm then give yourself a slap and use one that is available off the shelf. As someone's sig recently said when everyone reinvents the wheel you end up with a lot of square wheels.



    Peace, love and sincere sympathy to any C++ programmers out there.
  • Reply 18 of 44
    airslufairsluf Posts: 1,861member
  • Reply 19 of 44
    Thanks bawjaws!

    I know the CS department at my university has switched to starting with Java, but I'm in engineering we still do C and C++, prolly because it's headed toward very hardware level applications, though I think they just use C for that anyways.
  • Reply 20 of 44
    applenutapplenut Posts: 5,768member
    [quote]Originally posted by Code Master:

    <strong>Thanks bawjaws!

    I know the CS department at my university has switched to starting with Java, but I'm in engineering we still do C and C++, prolly because it's headed toward very hardware level applications, though I think they just use C for that anyways.</strong><hr></blockquote>



    yea, that's what I have seen looking through what courses are in the various engineering majors.



    while I'm at it.. wat's the basic difference between Computer Science, Computers Systems Engineering and Electrical Engineering with a Computer Hardware Focus?
Sign In or Register to comment.