Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Beginning C++


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 stein102   Members   -  Reputation: 485

Like
0Likes
Like

Posted 09 July 2013 - 03:00 PM

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?



Sponsor:

#2 SimonForsman   Crossbones+   -  Reputation: 6323

Like
5Likes
Like

Posted 09 July 2013 - 06:15 PM

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)


Edited by SimonForsman, 09 July 2013 - 06:17 PM.

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!

#3 makuto   Members   -  Reputation: 856

Like
1Likes
Like

Posted 10 July 2013 - 07:16 AM

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


#4 Matt-D   Crossbones+   -  Reputation: 1469

Like
7Likes
Like

Posted 10 July 2013 - 07:55 AM

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! :-)


Edited by Matt-D, 10 July 2013 - 08:04 AM.


#5 MarekKnows.com   Members   -  Reputation: 741

Like
0Likes
Like

Posted 10 July 2013 - 10:36 AM

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.


---
Free C++, OpenGL, and Game Development Video Tutorials @
www.MarekKnows.com
Play my free games: Ghost Toast, Zing, Jewel Thief


#6 warnexus   Prime Members   -  Reputation: 1504

Like
0Likes
Like

Posted 10 July 2013 - 02:02 PM

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!


Edited by warnexus, 10 July 2013 - 02:13 PM.


#7 stein102   Members   -  Reputation: 485

Like
0Likes
Like

Posted 10 July 2013 - 06:09 PM

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

 

Also, any project suggestions?



#8 Matt-D   Crossbones+   -  Reputation: 1469

Like
0Likes
Like

Posted 10 July 2013 - 08:54 PM

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



#9 Yrjö P.   Crossbones+   -  Reputation: 1412

Like
0Likes
Like

Posted 12 July 2013 - 04:49 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS