# Learning C++ getting stalled... better approach?

## Recommended Posts

Hello again. I've been working through the book "C++ Primer Plus" which seems to be very good but I'm now 7 chapters in and I keep hitting a wall. In this chapter and the last one, he started discussing "the subtleties" of things like pointers in functions. I'm sick to death of reading about the subtleties of the language, I want to know how to bloody use it, not how to make love to it. Can anyone suggest a less ball-busting approach to learning C++? I'm not stupid, I just can't seem to figure out what any of this stuff I've learned (from 3 books btw not just this one) has to do with anything I might actually use. Also for some bizzare reason the programming exercises in this book and the last one I tried don't "match" the material discussed in the chapter. WTH? Would I be better off setting my own "curriculum" of programming exercises and just use the books as references rather than textbooks? Hmm, that sounds like a good idea actually... Any suggestions would be greatly appreciated, thanks in advance!

##### Share on other sites
Almost everything I know about programming has been due to trial and error. If I come across something I know nothing about, Google usually helps. The benefits: I learn the language in my own way, and I learn the stuff I need to know to make things I want to make, rather than making things to teach me how to use the language. The downfall: I'm always learning something (not that it's a bad thing). If books aren't helping you, think of something you'd like to make, then make it. Chances are it's going to be more beneficial. Just a thought...

##### Share on other sites
When I learned C++ I did it from a book and then did my own sample programs on what each chapter covered. The key for me was actually using what I was learning. Reading a book is one thing but actually sittle in front of a computer and trying to get your stuff to compile and work is another.

Now the samples you do don't have to be boring. Find something small that would fit. I did stuff like screen savers and small casino type games. You could even do the games totally text based in the console so you don't have to worry about graphics.

As for the nuances of pointers it took me a while to really get inside pointers and it happened long after I had learned to code in C++. You can still do a lot with just basic pointer understanding.

##### Share on other sites
In addition to experimentation, the better of the two Es, another choice would be examination. Grab the sources from a project that is perhaps similar to a problem you're working on and read through to see how they solved it. Maybe it'll offer you insight to a solution - there's been many a time that it's just clicked when I saw it all written out in code.

But you're still probably going to become most comfortable from just trying things out. Books are like storehouses; they provide you the tools and the imprinted warning labels, but nothing of how they're going to solve your particular problem. From there it's dependent on research, imagination, and the occasional bit of 3:00 AM sleep-deprived enlightenment.

##### Share on other sites
I'm afraid the problems you face are not unique to you - when I started out, I was like "What's this? It makes no sense...", "I can't be bothered" etc and, truth be told, I'm still like that now to a certain degree.

But the more you learn, the easier you'll find the simpler things, and as those simple programs complete and run perfectly, you'll achieve a level of satisfaction, thus spurring you on further.

Unfortunately, though, the ins-and-outs are required when it comes to making games, especially more complicated ones with graphics. You mentioned function pointers - these save you a great deal of code in a big project, believe me.

As for your book, you're probably best following the sample code through in the book, picking up on how the author solved the problem. Once you're happy with his or her implementation, and know how to use the syntax of C++, you'll find yourself adapting the same techniques to your own applications, and that is how you will learn (well, I did it that way, anyway [lol]).

You can then build your own curriculum, like you say.

Best of luck! [smile]

ukdeveloper.

##### Share on other sites
my suggestion would be to start a simple project (space shooter for example) and use what you know to start building it.

the problems you run into will be much better teachers than any book. Also. anything you learn from a book or from google, you can apply it back to your project.

the goal is not to finnish the project, but to keep adding to it and making it better. i would suggest some sort of "game", because game programming will require you to learn more diverse topics than typical programs.

hope that helps

##### Share on other sites
Quote:
 Original post by Theodore FuhringerI've been working through the book "C++ Primer Plus" which seems to be very good but I'm now 7 chapters in and I keep hitting a wall. In this chapter and the last one, he started discussing "the subtleties" of things like pointers in functions. I'm sick to death of reading about the subtleties of the language, I want to know how to bloody use it, not how to make love to it.

C++ more than any other language it seems, really does place much of its power in said subtleties. For example, said pointers in functions:

In plain english:
A variable passed to a function cannot be changed by the function.
A variable passed to a function via pointer can be changed by the function.

[note: edited for accuracy]

This is a rather important subtlety if you have a problem where you want to change a variable with a function...

Also:
A variable declared in a function (int foo;) dies at end of the function.
A variable created via pointer in a function (int *foo=new int;) does not die at the end of the function.

If you don't know that, all of your applications will leak memory until they die. Again, a rather important distinction.

Quote:
 Can anyone suggest a less ball-busting approach to learning C++? I'm not stupid, I just can't seem to figure out what any of this stuff I've learned (from 3 books btw not just this one) has to do with anything I might actually use.

Don't worry, the problems will present themselves. With only a few exceptions, all of C++ is 'useful', and you will be required to use it.

Quote:
 Also for some bizzare reason the programming exercises in this book and the last one I tried don't "match" the material discussed in the chapter. WTH? Would I be better off setting my own "curriculum" of programming exercises and just use the books as references rather than textbooks? Hmm, that sounds like a good idea actually...

I wouldn't. Most student made curriculums involve simple things like... a text based adventure game. Even though such a game is 'simple' it's coding is not, and will only serve to frustrate you, or distract you from learning and finishing the book. You should finish the book before trying something of any size beyond the book's exercises. Most common problems are vastly simplified by knowing some of the stuff that's in the back of your book.

[Edited by - Telastyn on August 31, 2005 3:08:09 PM]

##### Share on other sites
Less ball busting? Make sure your targets are achievable, and when you think you've learned something then go apply it.

If possible work with someone in real life, there is no substitute for a decent conversation and bouncing ideas off a peers.

Get in on the ground floor for the agile style of development. Write code as if the functions and classes you need already exist, and write them as you need them. This helps prevent coding stuff that you never use.

If you want to do graphics think about using a helper library like the SDL, which has tons of examples around. Take apart other peoples stuff, fiddle, break, fix... learn by doing!

##### Share on other sites
Quote:
 Original post by TelastynA pointer to a variable passed to a function can be changed by the function.

Is that true?

A variable passed to a funtion as a pointer can be changed... the pointer itself can not though. (wouldn't you need to pass a pointer to a pointer?)

##### Share on other sites
Well I am reading right now, How to Programe in C++ fourth edtion By Detiel. I have found this book to be very helpful to me already. I am only on ch 2 of this book but how the explain what each line of code does and how it realtes to c++ is extermly hopefully. As for me I read the chars and then I will make my own little prog to see if I understnad what i leanred in that char. I will make more then one prog each time trying something different to see what I can get to work and what i cant get to work. They have excriese at the end of the char, I try them But I myself find it better to learn when you take what u have just read and make it in to your own prog I thin in my own opion that it is a easyer way for me to learn. This might now be tru for everone but it helps me alot. And also the people in this community are very nice and if they have the knowegel to help you they most likely will. Well I hope that you Have good luck in c++ programing and If u want pick up the book I mentioned it is a really good book helps out alot.

##### Share on other sites
Okay. I'm a C guy. I only started working with C++ about 8 months ago, after using C for almost 10 yesrs. Now I believed that there was NO NEED for anything object orientatied, or any other language, until I started using C++ or should i say learning it.

Now I beleive that it is so much more powerful. Inheritence on it's own is just so wonderful..with all the concepts of polymorhism..virtual functions...amazing. And then ou get onto templates.....how fantsistic. Generic programing, and that just the tip of everyting.

I'm not too sure if Java, or C# give you anthying as powerful, but the "subelties" make the language. Please just try them...just once and then again and then somethiung else will crop up and then you'll be hooked.

It might seem like "black magic"...just like TCP/IP or E=mc pow(2) did one time. but it's worth the effort.

Just my opinion.

##### Share on other sites
Quote:
Original post by swinchen
Quote:
 Original post by TelastynA pointer to a variable passed to a function can be changed by the function.

Is that true?

A variable passed to a funtion as a pointer can be changed... the pointer itself can not though. (wouldn't you need to pass a pointer to a pointer?)

[edit: I complete misread the statement.

No, that's not true. The variable pointed to can be changed, not the pointer itself. I went to all the trouble of doodling this example, so I'm leaving the post, and editing the previous. :]

foo.cc
#include <iostream>#include <sstream>#include <string>using namespace std;void    square(long     x){        x=x*x;}void    squareptr(long          *x){        *x=(*x)*(*x);}void    swapptrs(long   **a, long       **b){        long    *c=(*a);        (*a)=(*b);        (*b)=c;}int     main(){long    n,p;long    *ptrn, *ptrp;n=42;p=6;ptrp=&p;ptrn=&n;cout << "n before square: " << n << "\n";square(n);cout << "n after square: " << n << "\n";cout << "p before squareptr: " << p << "\n";squareptr(&p);cout << "p after squareptr: " << p << "\n";cout << "n: " << n << "   p: " << p << "\n";cout << "&n: " << &n << "   &p:" << &p << "\n";cout << "ptr n: " << ptrn << "   ptr p: " << ptrp << "\n";cout << "*ptr n: " << *ptrn << "   *ptr p: " << *ptrp << "\n";cout << "\nSWAP!\n\n";swapptrs(&ptrn, &ptrp);cout << "n: " << n << "   p: " << p << "\n";cout << "&n: " << &n << "   &p:" << &p << "\n";cout << "ptr n: " << ptrn << "   ptr p: " << ptrp << "\n";cout << "*ptr n: " << *ptrn << "   *ptr p: " << *ptrp << "\n";cout << "\n";}

results
n before square: 42n after square: 42p before squareptr: 6p after squareptr: 36n: 42   p: 36&n: 0xbfbff714   &p:0xbfbff710ptr n: 0xbfbff714   ptr p: 0xbfbff710*ptr n: 42   *ptr p: 36SWAP!n: 42   p: 36&n: 0xbfbff714   &p:0xbfbff710ptr n: 0xbfbff710   ptr p: 0xbfbff714*ptr n: 36   *ptr p: 42

##### Share on other sites
Check out my post here.

##### Share on other sites
Well, let me recommend a less ass ramming book. Since I started using C++, i have always like C++ books from Dietel & Dietel. They write books for students so its designed to be self taught. I used taht book from freshman to graduate school. Its has good examples and talks about each line of code. I would start there.

##### Share on other sites
Quote:
 Original post by Theodore FuhringerI've been working through the book "C++ Primer Plus" which seems to be very good but I'm now 7 chapters in and I keep hitting a wall. In this chapter and the last one, he started discussing "the subtleties" of things like pointers in functions. I'm sick to death of reading about the subtleties of the language, I want to know how to bloody use it, not how to make love to it.

What makes you think there is a difference? [lol]
No, seriously, the reason he tells you about the subtleties of the language is because you need to know about the subtleties of the language. How are you ever going to be able to use the language, if you don't know *exactly* what it does, and what your code means?

So a few suggestions:
1: Read about the said subtleties, but don't bother to understand all of it perfectly. Just make sure you know about it, so that when you encounter it in your programs, you'll be able to say "I read something about this. I think I'll go look it up".

Quote:
 Can anyone suggest a less ball-busting approach to learning C++? I'm not stupid, I just can't seem to figure out what any of this stuff I've learned (from 3 books btw not just this one) has to do with anything I might actually use.

I'm not dumb, I just don't understand, you mean? [wink]
No, I know what you mean, and most people when starting out, are wondering why they're always told to do completely irrelevant and pointless exercises.
I know I did, and almost everyone else I've talked to, or tried to teach programming to, have asked the same questions. "Why am I learning to write silly text-based programs that save a phonebook to a file, or add numbers together at the user's request? What does that have to do with anything?"

But it does have everything to do with learning programming. You'll understand that too (once you get to the point where you no longer need to wrestle with it, and so by then it's a bit late)

Quote:
 Would I be better off setting my own "curriculum" of programming exercises and just use the books as references rather than textbooks? Hmm, that sounds like a good idea actually...

Would you? How do you know you'd cover the entire language then?
No, I think you should stick to using books. Of course, you don't have to do every exercise in every book, and understand every single word. But you have to make damn sure you've gone through *everything* in the book, so that you at least know about it, even if you can't remember everything, or are having difficulty understanding how some of it is useful.
You still need to know which features the language has, so you can recognize them later on, or at least, realize when you run into a problem where they'll be useful.

That's the main reason why I don't trust online tutorials alone. They tend to be a lot more patchy, teaching specific things very well, but skipping over a lot of other things. And if you were to just decide on your own what you need to learn, then the result would be much the same.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627734
• Total Posts
2978846

• 10
• 10
• 21
• 14
• 12