Beginning C++

Started by
7 comments, last by Stroppy Katamari 10 years, 9 months ago

I am a fairly competent Java programmer wanting to transition into C++. I find myself back at the beginner stage wondering "Where do I go from here?" My question is: What is the best path I can take to becoming a competent C++ programmer?

Advertisement

I am a fairly competent Java programmer wanting to transition into C++. I find myself back at the beginner stage wondering "Where do I go from here?" My question is: What is the best path I can take to becoming a competent C++ programmer?

Pick a reasonably sized project, write it using C++, get feedback on your code from more experienced programmers, rinse and repeat.

Java is a bit more forgiving if you write bad code and C++ does allow you to write non OO code as well(without completely abusing the language like you would have to do in Java to write non-OO code) but for good OO code the differences aren't that big.

The main thing you need to pay attention to in C++ for OO code that you didn't in Java is the rule of three (or the rule of 3, 4 or 5 if you use C++11). Basically if a C++ class has one of the following:

Destructor

Copy constructor

Copy assignment operator

then it most likely needs all three of them to function correctly. (With C++11 you might also need a move constructor and a move assignment operator)

Also, any memory you allocate has to be explicitly freed, for most classes this is done in the destructor which gets called when the object goes out of scope(if its on the stack) or when its deleted (if its allocated using new) (smart pointers can simplify this if you use C++11)

Templates in C++ are also a bit more advanced than generics in Java but starting out you can treat templates as generics if you want without any real problems.

The probably bigger obstacle to getting started with C++ coming from Java is the lack of functionality in the standard library, C++ only support the absolute basics, to create windows, render graphics, play sound effects, access input devices, etc you need to interact with the operating system either directly through the OS or driver APIs (Win32, X11, OpenGL, DirectX, etc) or indirectly through a higher level framework (SDL, SFML, etc).

and finally, if you are learning from tutorials on the web, make sure they are up to date, there is a lot of really bad C++ tutorials out there that push the old C way of doing things on you far too early. There are times when that crap is useful(it sometimes has less overhead) but any tutorial that teaches you "string" manipulation using character arrays and strcat/strcpy/etc before std::string or raw dynamically allocated arrays before std::vector should probably be avoided completely. (I'd recommend getting a modern book instead)

[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

Practice! No amount of reading can make up for the time it takes just coding in that language before you master it. Make sure to check out other people's code to see how they are using the language, but most of all, just code!

You'll also find there are other things you have to learn besides just the language, such as makefiles/project building, code organization, etc.

Want to get to know my work and I better? See my website: Au 79 Games

I wrote General Tips on the Process of Solo Game Development

Coming from Java?

The most important advice: DO NOT USE "new" -- it doesn't do what you think it does!

On its own (i.e., raw / not in conjunction with unique_ptr or shared_ptr) it opts into using manual memory management, which is not as often necessary as it may seem at first.

Don't:

std::string * horrible = new std::string; // NEVER DO THIS

Do:

std::string good;

That is, prefer automatic memory management (using automatic variables, allocated on the stack) by default.

Only in certain circumstances, when you're *absolutely* sure you _have_ to allocate on the heap (also called "free store" in C++), consider the alternatives (but remember that the cases where you actually _need_ to do so are far rarer than in Java).

The #0 alternative being:

Derived allocatedEarlierDerived; // yes, still on the stack

// ...

Base & refBase = allocatedEarlierDerived; // yes, you can absolutely use references for dynamic polymorphism (so this, on its own, is NOT a reason to use pointers)

// need to store in a container? consider std::reference_wrapper: http://en.cppreference.com/w/cpp/utility/functional/reference_wrapper

The #1 alternative being:

std::unique_ptr<Base> ptrBase(new Derived);

// In C++14 this will become: std::unique_ptr<Base> ptrBase = std::make_unique<Derived>();

The #2 almost last resort (only use after you have carefully considered the preferable alternatives -- assuming that you need to share from the start is often a sign of a sloppy design) alternative being:

std::shared_ptr<Base> ptrBase = std::make_shared<Derived>();

// NEVER: std::shared_ptr<Base> ptrBase(new Derived);

The absolute last resort:

-no, still don't use raw "new", at least not before you thoroughly familiarize yourself with the following:

http://www.informit.com/articles/article.aspx?p=1944072

http://en.cppreference.com/w/cpp/memory/unique_ptr

http://en.cppreference.com/w/cpp/memory/shared_ptr

Not to copy paste, see also, perhaps you'll find some of the links helpful:

http://www.gamedev.net/topic/643070-having-a-hard-time-learning-c/#entry5061820

HTH! :-)

Begin by learning the differences between pointers, references and instances of classes/objects. Then move onto learning how templates work, and then go to learn the standard template library. Once you have those things under your belt, start writing simple programs like a tic-tac-toe game. Run it and measure it to see if you have any memory leaks.

The interesting thing is that the transition from Java to C++ is smooth. There is still some funkiness that makes C++ stands out but the basic concepts early on in the beginning are identical if not similar. Since you are competent in Java, then the object oriented concepts in C++ should be easier to understand. It might sounds scary when you learn the object oriented concepts in C++ but it is the nature of C++. Stick with it and you will enjoy what it has to offer.

#include in C++ is the equivalent of importing statements in Java

cout << in C++ performs the equivalent of System.out.println in Java

cin >> in C++ performs the equivalent of the Scanner object in Java

fstream in C++ performs the similar function of FileInputStream object in Java

the object oriented stuff in C++ is definitely similar in Java along with some new features that is not included in Java.

C++ also introduced a feature called "friends" which you should also take a look at.

There is also using namespace std, something C++ and C# and I think other languages also might have.

For the most part, whenever you transition to a new language there is still plenty of new stuff to learn even by leveraging the concepts from the previous language you have learned. I will let you find the other mysteries during your journey.

Start by doing what you have been doing to be competent in Java: read some and code a lot. That is when the understanding really comes. Start with the basics of C++. It is still necessary but the early concepts are easy to grasp since you have experience with Java. Then start working on the object-oriented material like what other members pointed out.

Good luck!

Any books I should get? Or should I just read online tutorials?

Also, any project suggestions?

Any books I should get? Or should I just read online tutorials?

Also, any project suggestions?

http://www.gamedev.net/topic/643070-having-a-hard-time-learning-c/#entry5061820

One thing I think really helps is zero tolerance for quality problems from the start. It seems like you spend more time up front, but it comes back many-fold because you avoid massive and demoralizing debug sessions later when you have written 3000 new lines of code and have no idea where the bugs are. Even if you wouldn't save any time, it'd still be worth it because you end up understanding more. You'll have some degree of trust in your code and feel good about that instead of feeling like you are walking on quicksand. You'll learn faster to not write those bugs in the first place.

Turn all warnings on in the compiler, and tell it to treat warnings as errors, so your code won't compile as long as there's any warning or error. Of course, when you do get a warning, don't just try to mindlessly suppress it, but figure out what it is and why you are seeing it. If it turns out to be a real problem that could lead into trouble, you fix it. If not, only then you suppress it.

Also, often run the program under a memory checker like Valgrind with all memory leak checks etc. turned on. Again, if anything comes up, immediately stop adding new code and new features. Even if it seems to work, treat the program as "no good" until you have completely fixed the problem and get a clean bill from the checker.

I've been involved in teaching C and C++. We require the above (zero compiler warnings, zero detected memory issues) for any returned assignment work, starting from the very first assignment. Compiler warning = fail, memory issues = massive point deduction.

This topic is closed to new replies.

Advertisement