View Full Version : C++ questions
Ludootje
03-06-2002, 02:29 PM
I'd really like to learn C++, but I don't think I'll be able to learn it well.
So first of all, I'd like opinions: is it hard to learn? How difficult is it? I heard something about "pointers" being incredibly hard...
Also, is it possible to learn with just a tutorial/book, or is it better to follow a course about it?
Dru Lee Parsec
03-06-2002, 02:38 PM
A very good programmer once told me that pointers were a barrier to learning C and C++. Having gone through the learning curve of pointer many years ago I would have to say that it's true, it is difficult to understand, however once you get it and have that AHA! moment you can't imagine why it use to seem so hard.
The conversion from C to C++ is tougher because it requires a whole different mind set and way of thinking. I've been coding Object Oriented Languages for so long that I don't think I could teach someone C any more. I just don't think in a linear fashion when writing code.
Any language you attemp to learn will give you a tough time. All languages require thought, study, and practice. Even though I'm a serious fan lf Java I'd have to say that even Java throws some people because it forces an object oriented approach. With C++ you can use it (as Bjarne Strousrtup calls it) as a "Better C". That is, you CAN write object oriented code using C++, but you don't have to. In the long run I'm not sure if that's good or bad.
So basically, just get a good book or 2 and dive in and do all the example programs and assignments from the book. It will be tough at times, but all good things require work. :-)
BTW, don't get frustrated, I've been coding for many many years and I've been fighting a single bug at work for the past 5 days. So don't think that the pros have it easy. We battle the same problems you do. :-)
I found C++ fairly easy to learn. It was the hardest language I took in school, but once you get the "idea" of OO and the way the files work with each other it is actually an exciting language. Grab a book....tlak to a C++ coder and practice..and you will learn it :smug:
G
sans-hubris
03-06-2002, 11:20 PM
In your learning process, please ask questions. Books often try to cater to the lowest common denominator that will read the book, which means that many people will be confused by some type of wording or code. Often they will just stop there because they think it's too hard and they don't understand.
Ludootje
03-08-2002, 04:48 PM
Originally posted by Dru Lee Parsec
BTW, don't get frustrated, I've been coding for many many years and I've been fighting a single bug at work for the past 5 days. So don't think that the pros have it easy. We battle the same problems you do. :-)
Yes I once tried to write a game in python (which still isn't done :() and no one could help me out, not on LNO or anything else. I spend 2 weeks or so trying to get it to work, and gave up then :)
In the second week I tried to get it to work, I sent an email to the writer of the tutorial of which I was learning python. I thought "such a simply program, of not even 30 lines, with 3 lines which could be the problem, he'll certainly know how to fix it". He replied me very quickly, telling me he couldn't find the problem :(
That was when I gave up.
Thanks everyone for your help, I'll go buy a book I suppose.
Anyone has an idea of a GOOD C++ book, which admits the read doesn't know ANYTHING but English? (I do know some programming, but I think it's better to start from the beginning)
I know I can search on google etc, but every author will tell his book is great, and every site which sells books will tell it's a good book, so I prefer to ask you people.
Thanks.
a13ean
03-10-2002, 10:19 PM
write a game in python? sounds like fun... too much for me
Pointers are not overly hard, and if you just want to learn "some C++" you realy wont ever need to know them
Toast
03-14-2002, 10:42 PM
When you really think about it, you have to learn pointers if you really want to code in C++. If you decide you dont want to learn pointers your stuck without effective dynamic memory allocation. I guess you could use container code that other people have written, but your not really going to get to far with that. Pointers really arent that hard to learn. If you want to learn C++, do it the right way and learn pointers. Youl thank yourself for doing it later.
While I'll agree that it is essential to understand how to use pointers when doing C(++) development, I do think you can get quite far using the Standard Template Library (STL), which takes care of most of that stuff for you.
Toast
03-15-2002, 01:40 PM
I agree that STL is very good for simplifying a lot of memory issues, but if you really want to program you need pointers. Sure, you can write some small limited apps without them, but you'd run into problems if you went much farther than that. Then again i'm a little biased. I've used pointers heavily ever since I learned them.
a13ean
03-16-2002, 11:32 AM
Pointers are definatly a must at later stages, but if someone just wants to learn enough c++ to write basic 'hello world' programs and other simple things the use of pointers can easily be avoided till later stages of learning.
sans-hubris
03-17-2002, 07:35 PM
In order to use STL, you need to understand pointers. However, once you understand how to use STL, STL does a very good job of abstracting memory management for you.
My personal opinion of all of this is that once you learn a specific language, you should try to get into the details of how languages work. That's what will make you flexible and able to learn any new language quickly, no matter what angle it comes from. The only language that deviates greatly from the norm is Prolog. When you first learn Prolog, it can seem fun, but it has a lot of side effects that can be rather annoying. Prolog is completely different from any other programming language.
JimCamel
03-18-2002, 03:10 AM
I agree completely with sans-hubris. Learning the fundimentals of programming languages allows for easy learning of new ones. I was stuck in the Basic style programming for ages. Then in first year university I learnt the fundimentals and Java. Today I wrote my first app in C (yes I know, late starter), and by just looking at other code examples, completed it in half the allocated time, with 1/3 the code my lecturer did.
That'll learn him.
Jim
Pointers aren't so arcane. If you are determined to learn it, you'll get it. They are a little hard to wrap your head around in the first few days, but once you get it, its like riding a bike... well sort of..
Here's a book I recommend to new C++ programmers:
Object-Oriented Programming in C++
by
Robert Lafore
Sams Publishing
It covers alot of the basics of using standard C++. Get yourself a good free and easy to use compiler - Borland gives one away for free, look around borland.com and you'll find it. The key to learning good strong C++ is to read and code, read and code.
Hope that helps some.
Jammer
03-21-2002, 02:21 PM
Pointes aren't that hard, in my opinon. Once you have the concept of a pointer, the code is quite simple. A pointer is simply a sign, telling the computer where to look for the data. I think the level of abstraction in the idea of a pointer confuses most people. Most people don't like trusting the compiler (Microsoft :D). I'm more wary of inheritance.
I think the biggest issue with pointers is that you have to clean up after yourself.
sans-hubris
03-22-2002, 12:49 AM
Originally posted by kmj
I think the biggest issue with pointers is that you have to clean up after yourself. kmj said it best. That's the best caveat you can take in regards to pointers.
MattD
03-30-2002, 11:08 AM
well, it really depends on what your planning on learning c++ for.
if you just want to work with OOP, then perhaps learning java would be a second option if your really worried about pointers?
anyway,if you really want to learn c++ there is a brilliant book called
'accellerated c++' or something similar. its purely c++, no c ansi stuff at all. its a different approach but one thatll make a lot more sense in the end
Matt D
Dru Lee Parsec
03-31-2002, 01:04 PM
Say this 50 times a day until you understand it ;)
A pointer is an address of an instance of a data type
"A pointer is an address . . ."
Yes, it is an address. int * is NOT an int, it is the address to an int.
". . . of an instance . . ."
You can have a pointer poiting into space, but they're really only usefull if it points to a constructed instance of something. Now, it more advanced uses you can have a pointer that not assigned to an instance, but it's almost always pointed at a NULL to make sure you don't write into random memory.
" . . . ofa data type" With the exception of void pointers (a far more advanced topic) the pointer is constructed to work with a specific data type. An int * points to an int. A Foo* points to a class called Foo.
Here's another tip. Look at this :
int * x;
Read it backwards from right to left. When you get to the * say "Is a pointer to". so int * x says:
"x is a pointer to an int"
Realize that you just said "x is a pointer to an int" not "x is an int"
Another good mental excersice is to put mental parenthesis around that declaration. If you mentally (don do this in your code, it won't work) do this:
(int *) x;
you are saying to your self "x is an int pointer"
But if you think of it as this:
int (* x)
Then you say to yourself "star x (or *x) is an int"
And sure enough, x is a pointer to an int, and *x is the int it's pointing to.
Hope that helps. That way of thinking about pointers really seemed to clear the air for me.
recluse
03-31-2002, 06:02 PM
Very good lesson Dru Lee Parsec! Thank you for sharing that with us. :)
Dru Lee Parsec
04-01-2002, 12:39 PM
Thanks for the kind words Recluse :goodjob:
Here's a little excercise that may help folks become more familier with pointers.
Make a simple Hello World type of program that has an int. Define it like this:
int x = 5;
Now pass that int to a method as an int (pass by value).
void byValue(int b) {
b = b + 3;
cout << b << endl;
}
(It's been a LONG time since I've written C or C++ code so my syntax may be a bit off ) ;)
Put a cout or printf before and after you make this call and see if the value of x changes by running the "byValue" method.
Now, write another method called byReference like this:
void byReference(int * c){
*c = *c + 10;
cout << *c << endl;
}
Now what would we pass to this method? It's taking an int* in the parameter list so we know that "c is a pointer to and int". And we know that this pointer is an address to an int not an int.
So our main program wouldn't pass it x it would need to pass it the address of x. Remember how to do that? It's
&x
So your main method would look something like this:
int x = 5;
cout << "In the main method x is " << x << endl;
byValue(x);
cout << "In the main method after byValue x is " << x << endl;
byReference(&x);
cout << "In the main method after byReference x is " << x << endl;
Notice, did the value of x in the main method change by passing it to either of the methods? Once you understand that, you'll understand pointers.
Good luck.
recluse
04-01-2002, 03:45 PM
Ahh very nice little example. I had fun doing it between History and Communications, among other things.
Dru Lee Parsec
04-01-2002, 05:27 PM
So now it's your turn to share with the group. What happened to our little int? Did it change back in the main program? If so when , and why?
I'll let you explain and then we'll take it to the next step.
recluse
04-02-2002, 01:09 AM
Hmm... ok this will mainly be a reiteration of what you have said but I'll give it a shot anyways.
As expected when I passed x in as a parameter for byValue(x), x did not change inside the main() function because a copy of it was given to byValue, not the actual variable.
Of course after passing in x as a parameter for byReference(), its value wasn't passed in but rather its address, as denoted by the '&', so if we apply changes to the memory address of x the value of x will be changed back in main() as opposed to staying the same as it did with byValue since that was a copy of x and not really x at all.
Sound correct? Make sure you correct me on the most iddy biddy of details since foundations are so important, heh.
Ludootje, I suspect that this might be over your heard right now but it maybe interesting for you type in this sample program and see the results. Of course you could put in many comments, save the file and use it for future reference.
tockert
05-31-2002, 12:45 PM
I started out taking classes for C++ programming. I did struggle a little in the class only because it has been 20 years since I have sat in a structured classroom enviroment. Being in the Navy, I have enough of a structured background in electronics, that did help me with most of the thought process in the class. I also had a really good instructor that loved the material, and was available to help all the time. We also had a really small class too. Check into the local community college and see what they have for programming classes. They may want to test your math background before allowing you into a programming class. Collumbia College where I went did. But I also know the registration lady, and she waived that. It helps that I work with her husband in the Navy.
But, other then that, just get some good books and jump in.
Here are the books I used as a reference during my two classes of C++
Sams Teach yourself C++ - Jessy Liberty
C++, weekend crash course - Stephan R. Davis
C++ from the ground up - Herbert Schildt
C++, How to program. - Dietel & Dietel ( this was the book we used for both classes in C++)
Todd
stuka
06-14-2002, 02:50 PM
Hehe...I gotta throw this in for fun:void byValue(int b) {
b += 3;
cout << b << endl;
}
void byReference(int&* c){
c += 10;
cout << c << endl;
}
int x = 5;
cout << "In the main method x is " << x << endl;
byValue(x);
cout << "In the main method after byValue x is " << x << endl;
byReference(x);
cout << "In the main method after byReference x is " << x << endl;
Mainly because in C++, you can use real 'references' instead of pointers - but ya gotta be careful, 'cuz you've really got a pointer. I know, I know....it's confusing - but it's cool too!
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.