C++ As First Language

Started by
71 comments, last by Fredericvo 11 years, 9 months ago

A good indicator of a bad way to teach C++ could be if you stumble across pure pointers or the keyword "new" in code example quite early (like earlier than halfway into the book). This *could* mean the book might still use some old fashioned (and thus error-prone and dangerous) ways to achieve certain things.
Even pure arrays might be an indicator of such a problem.

I don't know this book, but I don't think a C++ book can easily be adopted to cover C++11, because a lot of the basic workings of the language have been improved substantially, thus rendering section relying on the older standard obsolete.

Maybe somebody knows this book and can give a more facts based assessment of it.


I just quickly skimmed through C++ Primer Plus 6th edition ( love my Safari membership ), I have to say your guess is exactly right.

DO NOT use this book to learn modern C++. At all, period.

First off, it starts off by saying:
C++ Primer Plus[url=""][/url] approaches C++ by teaching both its C basis and its new components, so it assumes that you have no prior knowledge of C. You’ll start by learning the features C++ shares with C. Even if you know C, you may find this part of the book a good review. Also it points out concepts that will become important later, and it indicates where C++ differs from C.

This is troubling, and arguably the wrong way to teach C++, especially in the C++11 world. If you come from a C background, sure, but teaching a user an obsolete language construct is worrying.

Even worse, guess when the first mention of smart pointers is... Chapter 12. How many chapters in this book? 12.

Basically they've bolted on the C++ 11 features as an additional chapter, instead of making them intrinsic to the book.

It's not a horrible book, but certainly not an ideal one.
Advertisement
Bill Door: I guess, if a beginner has an experienced instructor, she may well start with C++. But most people start learning programming on their own. If they have to do homework on which the instructor comments, even better. The instructor may guide them in the right direction. But people learning on their own get little to no feedback. This way, the jump on the first train that *seems* to work but nobody shows them the problems that may arise. Years later their programming technique stabs their backs.

I find it interesting how people seem to overstate how difficult it is to learn programming from C++. I teach high school students programming using Stroustrup's Programming: Principles and Practice Using C++. This is a text book for an introductory course on computer programming.

The students have found the book to be readable, engaging, and useful. There are practical exercise in learning to solve real problems in programming. The language choice is naturally C++.

I find C++ to be fine as an introductory programming language.


This goes completely contrary to my University experience. This was about the point the drop out rate went through the roof.

Granted, my prof may have sucked and it was 95, so the langauge has certainly changed since. I went into the course with a decade of programming experience, so i was not typical. But the students new to programming certainly struggled. Also, we moved beyond non-trivial examples, which is the point that C++ clubs you over the head with a mallet.
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 biggrin.png 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 :D Oh man, there are so many fun problems to solve dealing with invalid memory ^.^ !

Yo dawg, don't even trip.

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 biggrin.png! 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 laugh.png

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 this thing like this or like that.

Failure is not an option...


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 biggrin.png 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 biggrin.png Oh man, there are so many fun problems to solve dealing with invalid memory ^.^ !



For the record, what you are looking for is forward declarations.

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

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 biggrin.png! 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 laugh.png

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.


Anders Hejlsberg's ( the father of C# ) book C# Programming Language 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. smile.png
EDIT3:
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Vector Matrix::getRowVector(int row);[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Vector Matrix::getColVector(int col);[/background]

[/font]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Matrix Vector::getRowMatrix(); // returns a row vector[/background]

[/font]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif]

[background=rgb(250, 251, 252)]Matrix Vector::getColMatrix(); // returns a column vector[/background]

[/font]


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.

Yo dawg, don't even trip.

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.

Hi ShadowStep.

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? wink.png ).


[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

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. smile.png
[/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.

This topic is closed to new replies.

Advertisement