I'm learning C++ too.
what I do is:
1. read a section in the book
2. type codes (type type type... I believe that's the key)
3. while typing, put a lot of comments. like;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
...
int stack::pop(){
if(tos == 0){
cout << "stack is empty\n";
return 0; //you have to return an int, can't just write "return;"
}
tos--;
return stck[tos]; //return the top element
//here, we don't delete the element,
//just decrease tos.
//element still occupies memory
//but it's okay, cause the SIZE is fixed.
//the array is NOT a dynamic array. so it's okay to keep the element.
}
....
|
the reason I put many comments, it makes me think. in general, when I read something, I think I got it, but I actually don't. if I can explain it to somebody or myself, then it's okay., then I got it.
4. if I can't get it from the book, go online, find other explanations, code examples, and do type them.
5. in general, I can't stop myself chaning the original code in the book. I type the whole thing, run it, then, "hmm, what happens if I change this section like that.." I make lots of changes, play around, try to understand more, and have fun too.
6. if I feel tired, I give a break. walk around, go to supermarket etc. fresh air helps me a lot. walking couple blocks too.
7. I think the most important one, make some examples on up my own and try to make it work :)