Types, standard operations, pointers
-
Some basic types
There are a couple of basic types in C++. They differ in what
they represent (integer numbers, real numbers, letters, etc.), in
the amount of bytes used to store them, in the way they interact
with other types. In the table below some of them will be
listed:
Type |
Representing |
Bytes |
bool |
Boolean numbers 0 or 1
representing true and
false |
1 bit |
int |
Integer numbers from
-blabla to blabla |
2 bytes |
long int |
Integer numbers from
-blabla to blabla |
4 bytes |
float |
Single precision real numbers
with blabla significant digits. |
4 bytes |
real |
Double precision real numbers
with blabla significant digits. |
8 bytes |
char |
One letter (character) encoded by
ASCII code. |
1 byte |
string |
A word of up to blabla chars.
Only with the string library included.
|
1 byte |
-
Some basic operations
With the algebraic types above you can do a number of operations:
-
Most of them are quite obvious, the only thing that is not so
obvious for the non-programmer is the role of the "="-operation.
This operation does not work in the mathematical sense with
computers, computers just have no clue about maths. In fact, the
"=" operator represents an assignement and for a construction
like
it should be read like
the item on the left hand side, conveniently called left
gets a new value, to be found on the right.
|
This obviously implies that the types of left and right should be
identical (or at least somehow "convertible").
-
Equipped with this information it is not hard to understand lines
like
int variable = 5;
variable = variable + 3;
variable += 3;
variable++;
|
In the example above I first declared a variable and assigned the
value 5. In the second line, the old value of the variable will
be taken and a 3 will be added. The same thing happens in the
third line and here you got to know a convenient short hand
notation. In the last line, the value of variable is incremented
by one (the "++"-operator). Similar decrement operators exist
as "-=" and "--".
-
More obvious operators are: "-", "*", "/", "*=" and "/=", where
the latter is of limited use. There's a not so obvious operator,
namely "%". This operator is defined for int-types only and
a line
int remnant, numerator, denominator;
remnant = m%n; |
and I leave it as a nice little exercise for you to figure out
what it does (by the way: "%=" is an allowed operator, too).
-
A warning: Usually, if not stated otherwise, compilers tend to
use up as little memory as possible. If you want to use numbers
in a calculation and you want to make sure that the computer
things about your numbers as real numbers, add a decimal point
after the integer number. To see what I mean compare the
following two lines:
double result_nopoints = 1/3;
double result_points = 1./3.; |
In the first line, the compiler treats the numbers as integers,
and in the division the result will be zero, in the second line
the compiler knows that you insisted on real numbers, ergo the
result is a third.
-
Pointers
To understand this nice concept let me invoke an analogy:
Let me assume you're talking about some kind of information,
how would you try to pass it ? Basically there are two options:
- You might just pass the information as is, read the book
aloud or whatever kind of information you were thinking about.
- Alternatively you might just pass the information of where
you can find the information, i.e. refer to the book (with author
etc.).
So far we were talking only about the direct way to handle
information, but in C++ you have also the option to pass the
addresses of your information only. Some examplatory syntax would
look like:
double * x;
x = new double[3];
x[0] = 0.;
x[1] = 1.;
x[2] = 2.;
double result = x[0]+x[1]+x[2];
delete [] x;
|
What do these lines of code above ? Well, the answer is pretty
simple although the code looks frightening in the beginning.
-
First a field (a vector) of reals named x is declared. The
keyword for this vector thing is "*".
Basically its starting address is given by x and the complier is
told that starting from this hexadecimal address x the subsequent
memory is to be organized in terms of 8-byte double
variables.
-
But, this memory is not allocated (reserved) yet. This
happens in the second line, and there it becomes clear as well
that it will be three double numbers to be stored. The
corresponding memory will be allocated by the using keyword
"new".
-
The very same memory is then freed again by the keyword
"delete", cf. the last line of the example above.
-
In between the "new"- and the "delete"-statement the single
components are given numbers that are then added to yield a
result.
-
Note that the counter of this field starts with zero !
[prev]
[top]
[next]
|