# C++ As First Language

This topic is 2011 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

[quote name='boogyman19946' timestamp='1342024379' post='4958086']
I figure it must be quite hard to show people how the overall structure of C++ is a mess when discussing programming concepts instead of the language itself.

Perhaps it would be ok if I share an example which I stumbled upon just a few minutes ago. If I was programming in Java, my code would compile on the first try without an error or would at least have a very high chance of doing so. In C++, apparently I forgot a "couple" of things.

For one thing, we have the whole inclusion garbage. I was creating a Vector class and a Matrix class in order to do some rotations, but I didn't feel like making separate functions to facilitate multiplication between a vector and a matrix, so instead I added functions into the Vector and Matrix class that would turn them into a Matrix or a Vector respectively. This of course requires me to #include "Matrix.h" in Vector.h and #include "Vector.h" in Matrix.h so that I may work with those objects. Anyone who has experience with this already knows what horrendous things are going to start to happen. If we compile Matrix.h first, it's going to execute the #include statement, and try to compile Vector.h, but before that finishes, it encounters #include "Matrix.h" which goes back to Matrix.h. Now, the inclusion guards do their job, so the preprocessor skips over the contents and class Matrix is not declared for class Vector, so when Vector encounters Matrix types, it's going to complain they weren't defined, and class Vector is not going to get defined, and when we finally return to Matrix.h, class Vector has not been defined so Matrix can't use it, and alas, nothing gets defined properly and the code doesn't compile.

Unfortunately I can't use pointers (well I could but the resulting code would be horrendous), because that would suggest I'd have to allocate memory inside the function and remember to deallocate it later outside. I can't use references because the local variables will go out of scope and I don't want to modify the object I'm converting. I have no choice then, I have to move the functions in a utility header and define them there so I can use both Matrix.h and Vector.h without complications.

Of course I could put it into a constructor where the class itself will take care of the memory, but that will only work for turning a vector into a matrix. I still have to hack about to get a single row or column of a matrix into a vector.

So there is your simple C++ deal. In Java, you write import Matrix; and import Vector; in their respective files, and magic happens [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] No need to "set your code up right."

If you've got a whole lot of experience dealing with this kind of stuff or you're really good at solving cryptic problems, you'll know where to progress. If you're a beginner and you get your shirt caught here, goodbye! And preprocessor troubles aren't the only "gotcha's" there are [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] Oh man, there are so many fun problems to solve dealing with invalid memory ^.^ !
[/quote]

For the record, what you are looking for is [url="http://www-subatech.in2p3.fr/~photons/subatech/soft/carnac/CPP-INC-1.shtml"]forward declarations.[/url]

It doesn't invalidate your example in the least, again the preprocessor is a big part of what sucks about C++ for new developers, I just felt I should give you the proper solution to your problem.

##### Share on other sites
Thank you Serapth and rnlf for taking tha time to answer to my thead. Serapth with all that you said above it seems that C++ is not a very smart choice for begginer language.

I will have to choose between Java and C#. Maybe i should give C# one more chance [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]! I dont think that Head First C# is my type of book got any others to recommend me? something more "traditional"

Boogyman even though i am a begginer i think i got your point there [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img]

Again thank you all for you answers!

PS. The thing that i liked about C++ Primer Plus was that it just explained everything and it was very analytical, where you had no questions to make why is like this or like that.
[/quote]

Anders Hejlsberg's ( the father of C# ) book [url="http://www.amazon.com/Programming-Language-Covering-Microsoft-Development/dp/0321741765/ref=dp_ob_title_bk"]C# Programming Language[/url] is probably as good a place to start as anywhere. If you like analytical, its a good book. Additionally its annotated by other developers with discussions about WHY this was done, or why that was done, and in some cases, the annotations are quite critical. It makes the book a bit less dry.

I can understand not liking the Head First series, that is why I recommended people preview it before purchasing. Personally it's not my bag, but then, I am very technically minded, so I dont really like a lot of anecdotes in my texts. I also find the page layout chaotic and loud.

One last piece of advice, and something new ( and experienced ) developers fall for all the time ( and goes completely contrary to everything I have said in this thread).

Don't let people push you around. If you really want to learn C++, learn C++. If you really want to use SFML instead of SDL, use SFML. Of course, disregarding other peoples opinions and learning experiences is a stupid thing to do, but remember... opinions are like a______s, everybodies got one. At the end of the day, experience is the best teacher, and you can't really make an unfixable mistakes at this point. The most important thing really is to pick something and stick with it. Someone with an opinion contrary to mine will come along and try to sway you to go a different way. Ultimately you will get nowhere if you spend a week learning one language, then switch to a different one, then a different one, then a different one. Learning lots of languages is a good thing of course, just not initially. In the programming world there is always a shiny distraction just over the hill, or a compelling reason to switch technologies... most of the time, it is a bad idea. Most of the time. It is hard, really hard, to keep focused in this profession, but you will have to if you want to get anywhere!

That said, choosing a language other than C++ was a good call. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] Edited by Serapth

##### Share on other sites
EDIT3:
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]Vector Matrix::getRowVector(int row);[/background][/left][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]Vector Matrix::getColVector(int col);[/background][/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]Matrix Vector::getRowMatrix(); // returns a row vector[/background][/left][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)]Matrix Vector::getColMatrix(); // returns a column vector[/background][/left][/size][/font][/color]

Alright, so I've done an actual test to see if those functions above would cause a problem in my classes, and they do not, so then Serapth was, of course, right. I made a mistake thinking the return value had to be a reference or a pointer. But I digress now. Edited by boogyman19946

##### Share on other sites
You're welcome.

I recommend you take a look at Python before settling with C#. It has a very easy syntax, a comparably large standard library and supports lots of different paradigms (you don't need to know what that means right know, but it will allow you to learn different approaches to the whole problem solving part, once you are ready for it). It has the added benefit of being used as an integrated scripting language in some major products, so you get scripting in those for free. Blender is a nice example for this, as it features a full fledged game engine.

It has one feature, which is at the same time a blessing and a curse (depending on whom you ask): It uses dynamic typing. This means, you have an easier time writing down your programs because you don't have to type in the right type for every object, but at the same time, this may lead to confusion in very large programs. And it will require you to learn the idea of static typing once you decide to learn one of the C style languages. But for you first programs, it won't matter that much and later, you will have learned how to deal with it.

I can't actually make a valid statement about which language is easier to learn for a beginner, because I learned both languages after several years of C and C++. But considering the facts that C# felt quite familiar to me as a C++ programmer and I still felt more comfortable with Python after a shorter time, I personally prefer it.

I would strongly discourage you from using Java for mostly the same reasons Serpth points out in the "Java cons" part of his article. Java is really annoying.

##### Share on other sites
[quote name='MoroS84' timestamp='1342003748' post='4957978']

C++ isn't so bad as a first language, but I would suggest that you concentrate on pure C first. It's much more simple to learn and understand. It's also a good starting point to learning C++. Think of C++ as an extension to C, that adds object-oriented programming (why do I have a feeling that some people are going to try to stab me with a rusty fork for this sentence? [img]http://public.gamedev.net//public/style_emoticons/default/wink.png[/img] ).

[quote name='SimonForsman' timestamp='1342003322' post='4957973']Many of the worst issues will be avoided if you write modern proper C++ rather than the bastardized mix between C and C++ that some online tutorials teach[/quote]
A big +1 on that one Simon. And the things I wrote above have a bit of a flaw: you're risking in creating those "bestardized" mixes when learining C first and then C++. C is a lot better for understanding basic language syntax (functions, conditional instructions, loop instructions, basic code layout, etc.). C++ adds more to that.

PS. Hello GameDev Community. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]

First off, Hi! Welcome to the gamedev community.

I also wanted to take a moment to explain why ( I think... I didn't personally downvote you ) you are taking a shelacking reputation wise. It isn't an attack on you personally, it's just your advice is outdated, and by modern standards, wrong.

The C subsystems within C++ exist for legacy ( and mostly political ones at that ) reasons alone, and certainly should not be the focus for a beginner. In general, they should be avoided by people starting out with C++ today.

I just wanted to clarify why you were getting downvoted, so you didn't take it personally and get put off by the Gamedev community.

Again welcome, and don't worry, we are a generally agreeable sort in these parts.

##### Share on other sites
Serapth, actually taking a glance at the book [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][background=rgb(250, 251, 252)] [/background][/left][/size][/font][/color][url="http://www.amazon.com/Programming-Language-Covering-Microsoft-Development/dp/0321741765/ref=dp_ob_title_bk"]C# Programming Language[/url] I think the book is more of a reference than an actual tutorial book for novice begginers like me. Also it does not have any exercices seeing it from the preview contents...

##### Share on other sites
Other than that what should i watch out? Where i might be tricked?
[/quote]

As for the C++11 style -- read this:
[url="http://herbsutter.com/elements-of-modern-c-style/"]http://herbsutter.co...modern-c-style/[/url] // "Elements of Modern C++ Style" by Herb Sutter
[url="http://msdn.microsoft.com/en-us/library/hh279654.aspx"]http://msdn.microsof...y/hh279654.aspx[/url] // Welcome Back to C++ (Modern C++)

And watch this:
[url="http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-835T"]http://channel9.msdn...D2011/TOOL-835T[/url] // Writing modern C++ code: how C++ has evolved over the years

Should be good for starters :-)

// If you need more, this is pretty good (but not free \$): [url="http://www.artima.com/shop/overview_of_the_new_cpp"]http://www.artima.co..._of_the_new_cpp[/url] // Presentation Materials: Overview of the New C++ (C++11) by Scott Meyers

*The* C++11 book I can *strongly* recommend is coming out soon:
[url="http://www.informit.com/store/product.aspx?isbn=0321714113"]http://www.informit....isbn=0321714113[/url] // "C++ Primer, 5th Edition" by Stanley B. Lippman, Josée LaJoie, Barbara E. Moo
Incidentally, Barbara E. Moo happens to be one of the "Accelerated C++" co-authors -- nuff said :-)

// EDIT: Serapth, perhaps you might be interested in taking a look, this one introduces smart pointers immediately, in the very same section it introduces dynamic memory -- as it should be :-) Edited by Matt-D

##### Share on other sites
I honestly don't think C++ is that hard. It's just very broad and flexible so it's easy to overlook certain features. Or if I rephrase you can do so much with it that it can be a little confusing for someone who's new to understand why the language is so detailed and let's you do certain things until you understand more advanced concepts.

Personally if I had to learn a first language I would choose Java because it's syntax is similar to C++ and it's easier ; pretty much every problem is documented on the web making it easy to move on. It also renders the same results no matter your OS.

Then again, my two friends learned C as their first language and they rock. It's all about how much effort you are willing to put into learning. They are both in electrical engineering though and rather strong in arithmetic's. Edited by DZee

##### Share on other sites
C++ is the founding father of the world. Learn it.

Good luck

##### Share on other sites
[quote name='DZee' timestamp='1342059632' post='4958234']
It's all about how much effort you are willing to put into learning.
[/quote]

Only if you are doing it wrong. It *should* be all about minimizing the amount of effort you need to put into it.

##### Share on other sites
It depends. I got no programming experiences but I started out C++ as my first language because I want to program games. So far, it took me.....2 or 3 weeks to understand the first chapter. XD LOL

##### Share on other sites
[quote]Ok guys i have been hearing a lot about how bad C++ is as a first language[/quote]
C++ is by no means a bad language to start with, sometimes transitioning from one language to another is harder.
[quote]Is really C++ that hard for a begginer?[/quote]
No, C++ is fairly easy if you take your time to learn it, just like any other language.
[quote]Is it impossible to learn C++ as first language? or it just requires some more dedication than other languages? or maybe i just havent got to the difficult things yet....[/quote]
It is by no means impossible to learn C++ as your first language, its a great language. It is extremely versatile and quite frankly can do a lot of things most other languages cannot. If you eventually master C++ you will find learning other languages to be significantly easier.

I personally found this book to be pretty good when I checked it over: [url="http://www.amazon.com/Without-Fear-Beginners-Guide-Edition/dp/0132673266"]C++ Without Fear: A Beginner's Guide That Makes You Feel Smart[/url] Edited by DevLiquidKnight

##### Share on other sites
C++ is not easy. You can, however learn it as your first language. But you will most likely pick up some very bad practices along the way.

The book you recommend, DevLiquidKnight, seems to be another example of a bad C++ book. It teaches the wrong topics at the wrong time (considering the table of contents and index), it refuses to teach very important topics in an adequate depth (two pages for smart pointers), heck, it doesn't even mention any STL containers beyond std::list. This seems to be a terrible book. And even this terrible book's title implies that C++ is hard. If you worked through this book and you believe you know C++, you're wrong. Edited by rnlf

##### Share on other sites
Thank you for trying to help me [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img] , but Guys now i start getting a little comfused here, cause many say stick with C++, others start with C# or Java or Python.

From what i see in Amazon, Python has some really well made introductary books like this one [url="http://www.amazon.com/Invent-Computer-Games-Python-Edition/product-reviews/0982106017/ref=dp_top_cm_cr_acr_txt?ie=UTF8&showViewpoints=1"]http://www.amazon.co...howViewpoints=1[/url] (very positive reviews) and this [url="http://www.amazon.com/Python-Programming-Absolute-Beginner-3rd/product-reviews/1435455002/ref=sr_1_3_cm_cr_acr_txt?ie=UTF8&showViewpoints=1"]http://www.amazon.co...howViewpoints=1[/url] (great reviews too)

While in C# i still havent found a good introductory book. (dont like Head First neither C# programming language cause its not really a tutoral than more of a reference)

In C++ I am a little comfused, first because some peoeple say it is not such a bad first language when others disagree (Well we have discussed this theme already and you dont have to start giving arguments why its better or not cause I have already seen the two sides of the opinion)What I would like you to do to help me is to tell me if there is really a good book to start with this language cause all we have mentioned so far are not good (by your opinions neither C++ Primer Plus nor C++ Without Fear).

In Java i found this [url="http://www.amazon.com/Building-Java-Programs-Basics-Approach/dp/0136091814/ref=sr_1_9?s=books&ie=UTF8&qid=1342077944&sr=1-9&keywords=java"]http://www.amazon.co...9&keywords=java[/url] and [url="http://www.amazon.com/Introduction-Java-Programming-Comprehensive-8th/dp/0132130807/ref=sr_1_8?s=books&ie=UTF8&qid=1342077944&sr=1-8&keywords=java"]http://www.amazon.co...8&keywords=java[/url] if you have any other good books to recomend me better that these two tell me to check them out.

[b]So what I really want from you is to tell me which language you prefer most and a good introductory book with it. I will check all the books reviews and see what is better for me. [/b]Thanks Again!

P.S. I forgot something important.. if i am about to learn a diffrent launguage from C++ I want to learn it and be ready to move on C++, not learn a second launguage again and then go to C++. Edited by shadowstep00

##### Share on other sites
P.S. I forgot something important.. if i am about to learn a diffrent launguage from C++ I want to learn iT and be ready to move on C++ not make another launguage again first and then go to C++.
[/quote]

You probably shouldn't think that way, you will most likely never "move on" to a new language, languages are just tools so rather than moving between languages in some sort of progression from bad but easy to hard but good you just keep adding new languages to your toolbox, almost every language out there is good for something. (And while most programmers have a favorite language a good programmer will choose language based on the task at hand)

For me the languages that i currently(as in, on a near daily basis) have a real use for are: C#, Java, C++, Python, PHP, JavaScript and Lua, Of those PHP and JavaScript are primarily for the Web and Lua primarily as a scripting language (and thus generally not used exclusively on a single project).

Learning C# or Python first is very unlikely to be a waste of time as those are languages that can be used for a very wide variety of things and you are very unlikely to abandon either of them completely even if you become a C++ expert.

##### Share on other sites
[b]So what I really want from you is to tell me which language you prefer most and a good introductory book with it. I will check all the books reviews and see what is better for me.[/b][/quote]
I prefer C++ and this book looks recent and decent enough:
Programming: Principles and Practice Using C++ by Bjarne Stroustrup[url="http://www.amazon.com/dp/0321543726/?tag=stackoverfl08-20"] http://www.amazon.com/dp/0321543726/?tag=stackoverfl08-20[/url]

Also all the best practices and intermediate books from (the beginner books are published around 2000 and might not be that good):
[url="http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list"]http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list[/url]

##### Share on other sites
While Stroustrup writes very good books, I would never recommend one of his books to a beginning programmer. He usually assumes you already know how to program (in any language) and builds upon this knowledge. Also this book seems to focus on "classic" C++. It uses some things that are outdated by today's standards and has actually no C++11 content.

This book is outdated and not suitable for beginning programmers. Edited by rnlf

##### Share on other sites
[quote name='rnlf' timestamp='1342082532' post='4958308']
While Stroustrup writes very good books, I would never recommend one of his books to a beginning programmer. He usually assumes you already know how to program (in any language) and builds upon this knowledge.
[/quote]
Overall picture I got from the description, excert, and couple of reviews is, that this book is aimed at non-programmers and using modern C++ as the language to teach different aspects of programming. I haven't read it so maybe I'm wrong.

[quote name='rnlf' timestamp='1342082532' post='4958308']
Also this book seems to focus on "classic" C++. It uses some things that are outdated by today's standards and has actually no C++11 content.
[/quote]
Not all C++11 content is aimed at beginners. I'm willing to bet there is heavy use RAII and other best practices.

[quote name='rnlf' timestamp='1342082532' post='4958308']
This book is outdated and not suitable for beginning programmers.
[/quote]
This is published at 2008. Many other suggested books here are from 2000 or so. Can you provide better alternative, which is recent enough to use modern techniques, and aimed at beginners?

##### Share on other sites
I'm afraid I don't know any. I used the old "C++ Programming Language" by Stroustrup, but I knew how to program back then. Maybe the book is better suitable for beginners, than I thought, it's in any case much better then this no-fear-book. I guess, if someone really wanted to learn C++ as a first language, this book might be a nice start, but one would have to read up on C++11 and try to forget some of the old C++03 clutter the book teaches.

##### Share on other sites
Maybe I've been a little to harsh on the Without-Fear book as well. After looking at it again, it does at least seem to introduce important C++11 concepts. Maybe in an order I wouldn't recommend, but then I am no teacher and have no training in didactics.

And then, my standards may be a bit to high. Still, don't start learning how to program with C++.

##### Share on other sites
If i learn Python well will I be able to go on C++ without a problem? or i should learn another language again ?

##### Share on other sites
If you really try to understand the concecpts behind python, you will automatically understand the concepts behind a lot of C++.

You will still have to learn the new syntax, you will have to make the move from dynamically to statically typed languages and you will have to understand the conecpt of manual memory management (and how to avoid it in C++) and you will have to learn the new standard library.

That's all you need to know to apply your python knowledge to C++. After you mastered that, you may find it much easier to move on to the interesting features of C++ that let it stand out from the rest of the languages. But you may perhaps get there faster, if you learn python at first, because you can learn the basics in a more forgiving, easier language.

As someone pointed out above: You will not move from python to C++, you will keep python and take C++ as a second language.

##### Share on other sites
Just buy a "C++ How to program C++" by Deitel &Deitel. That's the book I'm reading so far.

##### Share on other sites
[quote name='rnlf' timestamp='1342088871' post='4958341']
Maybe I've been a little to harsh on the Without-Fear book as well. After looking at it again, it does at least seem to introduce important C++11 concepts. Maybe in an order I wouldn't recommend, but then I am no teacher and have no training in didactics.

And then, my standards may be a bit to high. Still, don't start learning how to program with C++.
[/quote]

Yeah, it isn't specifically about C++11, expect those books to be showing up with more regularity.

That said, WIthout Fear is probably the most accessible beginners C++ book, while still teaching pretty good style. At least from my recollections when I researched this subject for the guide. It's hard to judge a book from a beginners perspective when you've already learned the subject.

##### Share on other sites
Well I think I have decided to go with C# but again i cant find a good introductary book. The book C# Programming Language that Serapth recommended me is more of a reference or for experienced programmers than a begginers language tutorial (Head First series is not my type) . [img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img] If i cant find a good book on C# I will go for C++ no other choice.

Yeah I know this whole thing really sucks cause there are many like me that spend so much time thinking on which language they should start with and in the end they end up with the same and spending so much time choosing than actually working!. So sorry for being a pain in the ass. Just give me a good book to start with and end this thread at last.