How did you guys learn C++ ?

Started by
16 comments, last by HTML 20 years, 6 months ago
Wow, thanks for such quick replies.

Yes, I know algerabra. My mistake,
I understand variables like:

a = 5;
b = 3;
result = a + b;

and that kind of stuff, but right now,
I am struggleing with this tutorial:

http://cplusplus.com/doc/tutorial/tut1-2.html

I understand almost everything in the tutorial
up to identifiers, then I am lost. I understand
what it is, but not when to use it and where.

Then data types.....I don''t understand why you
can''t just use "long" all the time if it has almost
all the numbers you need.

Then I don''t understand "declaring variables"
ex: unsigned short NumberOfSons;
signed int MyAccountBalance;
Initialization of variables(dont get those)

Then the rest just loses me...It might be because
I am bad at math. I never understand it until
after the test....kind of sucks...

Then the first part of operators looks easy, like:
+ / - * and then doing stuff like:
a++=b.

I don''t understand other simple things like void main()
and int main() and return 0 looses me too...

As for me learning other languages, I know most of html,
except when I got to the end of the book for templates and
stuff..but html was boring so I stopped. I tried C, but previously
looking at C++, it started to confuse me. Besides that, I like how
C++ has classes.

Oh, and when I took that gametutorial.com thing for the text game,
it seemed like they made a lot of their own stuff up like:
strRoomNorth, STILL_PLAYING, void DisplayRoom,
GetRoomInfo...ect. How do you do stuff like that?

Thanks so much again for your help, sorry if I wrote too much...
Advertisement
quote:Original post by matt_j
I learned C++ from this awesome book by this awesome author:
Teach Yourself C++, 3rd ed. by Herbert Schildt.

Take a look before you buy Schildt books

No offense, but he has a pretty horrible reputation.

ld
No Excuses
First off, don''t get discouraged. Some people really get a handle on basic programming really fast, other people take longer. It took me a long time before I really got the hang of it. I think what works the best is if you can find someone to talk to about it. Then you can show him/her what you are trying, or even work through a chapter together. He/she can explain the stuff the book doesn''t cover well and expand on what they do cover well. That''s what I''ve found most helpful. If you can''t find someone, just try to work through some tutorials or books, and ask questions here. Anyway, good luck, don''t give up!
quote:Original post by HTML
I understand what it is, but not when to use it and where.

Variables are just used to store data. A variable''s name is its identifier. You manipulate variables to do what you want. You use them everwhere, because they''re like "handles" on pieces of data.
quote:
Then data types.....I don''t understand why you
can''t just use "long" all the time if it has almost
all the numbers you need.

You can use long most of the time. However, other types have built-in functionality that helps you deal with different types of data. Fundamentally speaking, there are only two types of data: floating-point and integral (and even that distinction is artificial).

The reason you have bools is to be able to easily and "safely" test the "truth" of an expression (if you don''t understand expressions and Boolean logic, google them up and have a read. That stuff''s all really important.

The reason for floats and doubles is to store fractional numbers. If you know something won''t be a whole number, use a float or double to represent it (btw, a double is basically just a bigger float, effectively. We''ll deal with size distinctions in a few moments).

If you know something will be whole, use an integer of some type. At this point, the only distinction between the different types of integers (besides signedness, which we''ll also get to in a minute) is their "capacity" - an int is the same thing as a long (on any compiler you''ll be using; it''s not rigidly defined that way by the Standard though)- they can each hold any number between -2.2 billion to 2.2 billion (roughly; the actual number is +/- 2^31).

If you want to store "strings" (all text counts in this area), use chars. A single char can store the ASCII (google it) value of any character, like ''a'', ''?'', or ''5'' (NOTE: ''5'' is not the same as the number 5). An array of chars can store a string like "My name is George." Of course, this is a little bit advanced, but you''ll learn in time how to use char*''s.

So, that''s a rundown of the basic types. Now, the two other issues: size distinctions and signedness.

Variable sizes are typically considered in either bits or bytes. A bit is a 1 or 0, the smallest meaningful piece of data. A byte is composed of 8 bits. A char, being the smallest basic data type, is a byte in size. A long (on any compiler you''ll be using) is 32 bits (4 bytes), and a short is 16 (2 bytes). As stated before, an int is also 32 bits. A bool could conceivably be 1 bit, but for the sake of reasons we can''t go into here (memory pipeline efficiency), it''s more often 1 byte or 4 bytes (seems like a waste of space? Don''t worry about it). The reason behind the different sizes of ints and floats is that memory usage used to be a concern. Now, more often, different sizes are used for clarity. Here''s a brief list of what a programmer thinks when they see a variable declared as a particular type:
short - "Hm, gonna be between -32000 and 32000, probably to indicate that this value won''t be too large at any point" - this might be an index into a string, which will rarely be more than 32000 bytes long.
long - "Could be a big number, probably indicates nothing special" - longs and ints are used interchangeably these days. They can indicate anything from the need to store a very large integer to sloppy programming. In other words, they''re a catch-all.
float - "Standard decimal number"
double - "Really big decimal number" - you''ll see floats more than doubles in game programming, and vice versa in academia

Now, signedness. This refers to whether a variable is signed or unsigned, which simply means that it can or cannot store negative values, respectively. Why?
1) For clarity: often, coders use unsigned variables to indicate that the value of the variable really should never be negative, like an array index or a number of objects
2) For the extra space. By declaring a variable as signed, you shift its capacity completely into the positive domain, giving you an extra bit (literally) of capacity. For example, both a signed int and unsigned int are 32 bits, but a signed int, because it uses one bit to store whether the number is positive or negative, can only store between -2.2 bil and 2.2 bil, whereas an unsigned integer can store between 0 and 4.28 bil.

quote:Then I don''t understand "declaring variables"
ex: unsigned short NumberOfSons;
signed int MyAccountBalance;

Declaring a variable is just telling the compiler that you''ll be using a particular indentifier to store something. You''re probably either overcomplicating it in your mind, or not thinking logically about how programming works. Both are fine; they just take time to break.
quote:
Initialization of variables(dont get those)

Initializing a variable is giving it an initial value. This matters because if you don''t initialize a variable, it contains meaningless data. For example:
int Foo;
Foo now contains a number. What number? I have no idea, because it''s just whatever was in memory where the variable was placed.
int Bar = 4;
Bar equals four, like a good variable that''s in my complete control.
This issue becomes much more important with pointers, but save that for another day.
quote:
Then the rest just loses me...It might be because
I am bad at math. I never understand it until
after the test....kind of sucks...

Take it slow. This is hard shit.
quote:
Then the first part of operators looks easy, like:
+ / - * and then doing stuff like:
a++=b.

That statement is impossible. This indicates that you''re trying to view the problem as a whole, which you really shouldn''t do. You''ll learn how to split things up mentally as you go, then evaluate them in much the same way computers do. A computer can only do one thing (an add, an increment, a multiply, a temporary variable creation, whatever) at a time, so learn about order of operations, and learn to think linearly and logically on a small scale.
quote:
I don''t understand other simple things like void main()
and int main() and return 0 looses me too...

It''s all just nomenclature. Don''t be overwhelmed by stuff that doesn''t directly apply to the input and output of the system at this point.
quote:
Oh, and when I took that gametutorial.com thing for the text game,
it seemed like they made a lot of their own stuff up like:
strRoomNorth, STILL_PLAYING, void DisplayRoom,
GetRoomInfo...ect. How do you do stuff like that?

That''s the coolest part of programming: taking this relatively simple set of rules, and building with them to create something complex and unique. What they''re doing there is just making functions and variables that define how the game code works. Once you realize how closely "what you type in" and "what happens" relate, programming becomes a matter of becoming a better problem solver, and learning the tools that the language offers to make that job easier. Until you reach that point, it''s crucial that you dick around with code and see what effect tiny changes to the code have on the program''s behavior. Shortly, you''ll get the hang of how this programming thing works.

I hope I haven''t been too ambitious in replying to all this. Hope it''s of some use. Feel free to email me or AIM@zealouselixir if you have more questions.

Peace,
ZE.

//email me.//zealouselixir software.//msdn.//n00biez.//
miscellaneous links

[twitter]warrenm[/twitter]

Well, I''ve been programming since I was 11 (about 8 years now..) but I reallllyy learned how to program when I took AP Computer Science classes in high school. Back then, they taught C++, but I believe they have moved to Java now (they were talking about that my senior year.)

I think that the endeavor should be to learn to "program," not to learn to a language. There are certain theories that must be understood. A person who can do something with C/C++ (or any other language) may not be a real programmer .. just like a guy who understands the theories of computer science may not know C/C++. But it is the latter that makes you a programmer.

daveandrews.org, soon to be home to a GAPI Sprite Engine.
well ok its like this:
#include<iostream.h> /*This includes input and outputfunctions into your program*/void main() // this is just the Main part of a program{/*when a computer runs the program it calls the function named Main()and executes the code inside of it*/      /*data types are simple thingsfor example say you have a bank accountyou need a name, amount of money in the account,intrest ratesand probably loads of other information. The point here thoughis that you need differnt kinds of information, thus differntdata types. integers and longs hold differnt amounts of data,if you know you wont need more than say 30,000 $ in a givenbank account you could use an integer, but if you need more moneyin the account you need a data type that can hold higher numbers.names and things like are Char data types, and have values from 0 - 255 that equate to letters or symbols when displayed.unless you intend on having names like 1234243 you are going toneed differnt data types :)*//*to declare variables of differnt types you use char,int,float,long,etc,etc.here we declare 3 integers and do pathagorean''s theorm*/      int a = 5;      int b = 3;       int cSQRD = (a*a) + (b*b);/*now we just output the CSQRD variable*/      cout<<cSQRD<<endl; }

it really easy it is all about catergorizing what kind of information you have and what kind of data type represents it the best.
Vaneger - Please correct the errors in your post :

* There is no standard C++ header called ''iostream.h''.
* main() returns int, not void.
* There is no cout object, but an std::cout.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Thanks. I''ll try your suggestions and tell you
if there''s anything I don''t get. I am starting to
understand more of it now. Not sure if taking
notes will help or not, but I am. Then I am going
to pick up a better C++ book.

This topic is closed to new replies.

Advertisement