Poll:
You do not have permission to vote in this poll.
Total 0 vote(s) 0%
* You voted for this item. [Show Results]

Thread Rating:
  • 2 Vote(s) - 4.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
C++ introduction
#1
Hello all,

I decided to create a "tutorial" for C++ (I don't like calling it a tutorial, but whatever). I wrote it kinda quickly, so please gimme feedback.

This part is a tutorial of the language syntax, without the standard library.

C++ is so close to the hardware it's not even funny. Some people really like this; some really hate it. C++ is a low-level high-level language; it doesn't have a garbage collector and you have to do many things manually. C++ code is compiled into what we call processor instructions, which are the small commands that the processor can read and execute. This, and the aggressive optimizations the compiler is allowed to do, give C++ its amazing speed and efficiency.

This tutorial assumes you have some basic programming knowledge, be it in a high-level scripting language or other.

VARIABLES

In C++, variables are keywords that refer to a space in memory. To declare a variable, you must specify its type so that the compiler knows how much space to allocate and what processor instructions to use when manipulating said variable. For example, [lua]int x;[/lua] will declare (create) a variable of size "int", which on most modern machines is 4 bytes. Please note that the variable x is declared but not initialized, so you can't rely on it being 0. [lua]int x = 0;[/lua] or [lua]int x(0);[/lua] initialize x to 0.

Variable sizes on most modern machines


[*] bool: 1 byte (but can only be 1 or 0)
[*] char: 1 byte
[*] short [int]: 2 bytes
[*] long [int]: 4 bytes
[*] int: 4 bytes
[*] long long: 8 bytes
[*] float: 4 bytes
[*] double: 8 bytes
[*] long double: 12 bytes

Arrays

An array is a series of variables, which all have the same data type. To declare an array of five integers, do [lua]int x[5];[/lua] To access the third element from the array, do [lua]x[3][/lua]

Pointers

Pointers are a special type of variable, that hold a memory address. They are declared by putting an asterisk after the data type they're meant to point to. A pointer to integers is declared like this: [lua]int * x;[/lua]
A common misconception is that pointers don't take up memory, but they actually do, and sometimes quite a bit. All pointers on a machine take up the same amount of memory, be them pointers to integers, to bools, or doubles. Pointers take up 32-bits (4 bytes) on 32-bit machines, and 64-bits (8 bytes) on 64-bit machines.

To access the data pointed to by the pointer (dereference), put an asterisk in front of it: [lua]*x[/lua] If the pointer points to a series of objects (array), access its elements as if the pointer were the array: [lua]x[number][/lua] If the pointer points to an instance of a class or struct (see Structs section), access its members by using [lua]x->member[/lua] or [lua](*x).member[/lua] These lead to some misconceptions, and will be discussed further in the next section.[lua]x[0][/lua] == [lua]*x[/lua]

[lua]x[number][/lua] == [lua]*(x + number)[/lua]

Differences between pointers and arrays

Pointer arithmetic

Next to come: examples, references, loops, and more.

[Image: 88x31.png]
Reply
#2
I thought that was interesting how you had to declare how big your variable size was.
[Image: chaosthegreat.png]
Reply
#3
It's just part of its low-levelness.

Added: a license. :p
Reply
#4
You forgot to explain what ; means.
Well, ; Is used when the script ends.
-Happyman
Reply
#5
(08-08-2011, 06:34 PM)Happyman Wrote: You forgot to explain what ; means.
Well, ; Is used when the command ends.

Tongue
Reply
#6
(08-08-2011, 06:34 PM)Happyman Wrote: You forgot to explain what ; means.
Well, ; Is used when the statement ends.

Well, I said that the tutorial already assumes you know the basics of programming... But I'll probably add that.
Reply
#7
Added: arrays and pointers
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)