-1


I'm starting C++ programming in my first job. I'm a CS student and I have learn programming in Java. What advice would you tell me to watch out so I don't cause trouble in my new job?
Would you have any advice or references would be appreciated.
(example: I know C++ is more likely to have memory problem than Java)

Thank you very much!

Nick
  • 605
  • 3
  • 11
  • 27

6 Answers6

6

Maybe you already know this, but one common mistake for folks used to Java and learning C++:

Don't use new unless you have to (and you probably don't really have to). In most cases where you want to create an object, you should just create it "on the stack", like ClassType obj;.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • 1
    Hell yes. I'd love a garbage collected language that still gives me the option for stack allocation (or any other way of deterministic object lifetime). –  Nov 15 '10 at 22:03
3

Two things:

  • Get yourself a copy of Effective C++ by Scott Meyers.
  • Lint your code. This will not only catch potential bugs early in the development process, but also teach you good coding habits (e.g. declaring a method's input arguments as const references). I use PC-Lint for this.
geschema
  • 2,464
  • 4
  • 30
  • 41
  • 1
    @John: true, but geschema didn't say, "get it on next day delivery". I would say that Items 1-4 of Effective C++ Third Edition (I'd say don't get 2nd Ed as in the link) will serve fine if read straight after learning basic syntax. Items 5-6 are certainly required before writing any real code, and the rest perhaps need only be read as relevant. There are other ways to encounter much of the same material, of course, but surely the goal of a newbie is to stop being a newbie, which means learning this stuff? – Steve Jessop Nov 16 '10 at 01:09
  • Thanks Steve. The link now points to the 3rd edition. – geschema Dec 20 '10 at 10:58
1

For me, the biggest difference between Java and C++ is pointers, so I would try to get pointers down. Just my opinion.

Chance
  • 2,653
  • 2
  • 26
  • 33
  • I agree. To program in C++ you need to understand pointers. Coming from Java, I think that's the main thing you would have to study to make the transition. – filipe Nov 15 '10 at 21:57
  • +1, you should really REALLY look into pointers, since they're practically nonexistent in Java and one of the cornerstones of C++. – suszterpatt Nov 15 '10 at 21:59
  • 2
    References in Java *are* pointers, only without the arithmetic. –  Nov 15 '10 at 22:04
  • Understanding pointers doesn't help you write C++ code. It just gives you more ways in which you can cause crashes. – jalf Dec 20 '10 at 11:45
  • @jalf, the idea of programming in C++ without pointers is an intriguing one and worth considering, but I don't understand why my answer deserves a '-1' because my mainstream idea conflicts with your unorthodox one. – Chance Dec 28 '10 at 17:35
  • @Chance: if you read the question, it is not about "how do I learn C++", but "how do I avoid shooting myself in the foot while programming C++". I think it's pretty much implied that the OP is going to need to learn C++ in the first place (and that includes pointers, since they're part of the language). The question is more what to do *on top of learning the language*. A more useful answer would be one that explains ways to deal with pointers without accidentally introducing bugs. Simply saying that the OP should know what pointers are doesn't help him write safe code. – jalf Dec 28 '10 at 18:48
  • 1
    I never said one shouldn't use pointers in C++ code. Just that "understand pointers" isn't the #1 thing on the list when trying to learn how to write *safe* C++ code. Beginners often obsess about pointers, thinking they're some kind of "Real Ultimate Power", and "if only I can understand pointers, the world is my oyster", whereas most of the idioms used by experienced C++ programmers to "tame" the language focus on entirely different aspects, and, if anything, downplay pointers as much as possible because they're just not that interesting. – jalf Dec 28 '10 at 18:49
  • and to use C++ without "causing troulbe", the OP is going to have to go down that path, not the "pointers are the key to everything" one often mistakenly taken by beginners. – jalf Dec 28 '10 at 18:53
  • @jalf, fair enough, I would completely agree with your statements, but the reason I brought up pointers is because it looked like the asker was familiar with java programming, so I assume the person knew about flow control and basics of object oriented programming. I just know from my personal experience that - and I learned java first, C second, C++ third - that pointers were the most difficult things in learning C and still is with C++. Sure, my answer was brief, "learn about pointers" but the asker can pick up a book or use Google, I'm not going to provide my own tutorial. – Chance Dec 28 '10 at 19:19
  • Ah, I don't have a problem with your brevity. Just that I feel it misses the mark. (Again, it's not the pointer that are important. There are other things you should focus on if you want to learn how to write C++ without "causing trouble". Pointers are just another part of the language that you should obviously know about, but they're really not that interesting, an they're not what you should focus on. – jalf Dec 29 '10 at 09:22
1

No two ways about it. You're going to be a menace until you've been bit enough by C++ gotchas to hate the language. I'd recommend trying to write functions and not classes. A lot of people think OOP is great, but really most users want your code to "do" something, not "be" something. Anyways, good luck. :-)

xscott
  • 2,350
  • 16
  • 18
  • I think OOP is great for a whole lot of stuff, more stuff than not. To anyone coming from C#/Java, be careful of multiple inheritance in C++. It can do great stuff, but it kind of violates encapsulation...many time you can use composition instead of inheritance. – kmarks2 Nov 15 '10 at 22:02
0

Definitely what Chance said. Memory management is explicit in C++. I had an excellent textbook in college called Deitel C++. It's a C++ Bible.

I would also learn up on the Standard Template Library and Boost.

kmarks2
  • 4,755
  • 10
  • 48
  • 77
0

This might give you some ideas.

Some important excerpts :

Researchers (Bloom (1985), Bryan & Harter (1899), Hayes (1989), Simmon & Chase (1973)) have shown it takes about ten years to develop expertise in any of a wide variety of areas, including chess playing, music composition, telegraph operation, painting, piano playing, swimming, tennis, and research in neuropsychology and topology. The key is deliberative practice: not just doing it again and again, but challenging yourself with a task that is just beyond your current ability, trying it, analyzing your performance while and after doing it, and correcting any mistakes. Then repeat. And repeat again.

And here is how to do :

  • Get interested in programming, and do some because it is fun. Make sure that it keeps being enough fun so that you will be willing to put in your ten years/10,000 hours.

  • Program. The best kind of learning is learning by doing. To put it more technically, "the maximal level of performance for individuals in a given domain is not attained automatically as a function of extended experience, but the level of performance can be increased even by highly experienced individuals as a result of deliberate efforts to improve." (p. 366) and "the most effective learning requires a well-defined task with an appropriate difficulty level for the particular individual, informative feedback, and opportunities for repetition and corrections of errors." (p. 20-21) The book Cognition in Practice: Mind, Mathematics, and Culture in Everyday Life is an interesting reference for this viewpoint.

  • Talk with other programmers; read other programs. This is more important than any book or training course.

  • If you want, put in four years at a college (or more at a graduate school). This will give you access to some jobs that require credentials, and it will give you a deeper understanding of the field, but if you don't enjoy school, you can (with some dedication) get similar experience on your own or on the job. In any case, book learning alone won't be enough. "Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter" says Eric Raymond, author of The New Hacker's Dictionary. One of the best programmers I ever hired had only a High School degree; he's produced a lot of great software, has his own news group, and made enough in stock options to buy his own nightclub.

  • Work on projects with other programmers. Be the best programmer on some projects; be the worst on some others. When you're the best, you get to test your abilities to lead a project, and to inspire others with your vision. When you're the worst, you learn what the masters do, and you learn what they don't like to do (because they make you do it for them).

  • Work on projects after other programmers. Understand a program written by someone else. See what it takes to understand and fix it when the original programmers are not around. Think about how to design your programs to make it easier for those who will maintain them after you.

  • Learn at least a half dozen programming languages. Include one language that supports class abstractions (like Java or C++), one that supports functional abstraction (like Lisp or ML), one that supports syntactic abstraction (like Lisp), one that supports declarative specifications (like Prolog or C++ templates), one that supports coroutines (like Icon or Scheme), and one that supports parallelism (like Sisal).

  • Remember that there is a "computer" in "computer science". Know how long it takes your computer to execute an instruction, fetch a word from memory (with and without a cache miss), read consecutive words from disk, and seek to a new location on disk. (Answers here.)

  • Get involved in a language standardization effort. It could be the ANSI C++ committee, or it could be deciding if your local coding style will have 2 or 4 space indentation levels. Either way, you learn about what other people like in a language, how deeply they feel so, and perhaps even a little about why they feel so.

  • Have the good sense to get off the language standardization effort as quickly as possible.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – MPelletier Aug 18 '12 at 16:16
  • 1
    @MPelletier Right. Although I think that link will never rot, I took out major parts – BЈовић Aug 18 '12 at 19:51