/* `Hello World` example where we make use of: - Comments - Directives - Namespaces - Terminators (;) - `main` function + body*/#include<iostream>usingnamespace std;intmain() { cout <<"Hello World"; // system("PAUSE"); // cin.ignore();return0; // program has completed its execution without any errors}
Variables can be 'global' or 'local'.
Variable Type
short / short int
short integer (2 bytes)
int
integer (4 bytes)
long / long int
long integer (4 bytes)
bool
boolean (1 byte)
float
floating point number (4 bytes)
double
double precision floating point number (8 bytes)
char
Character (1 byte)
// Input / Outputint uservalue;cout <<"The value of variable sum is "<< sum << endl;cin >> uservalue;
Operators
In C++ there are four main classes of operators:
Operator
Relational
>, >=, <, <=, ==, !=
Logical
&&, `
,!`
Bitwise
&, `
,^,~,>>,<<`
Expressions that use relational or logical operators return 0 or false and 1 for true.
0 value converts to false
non-zero values automatically converts to true
Iteration and Conditional Structures
Useful to instruct the program to execute or to repeat a specific operation when some condition is matched.
while (condition) { statement;}do {} while (condition);
goto label;......label:
Pointers
A pointer is a variable that holds a memory address. This address is the location of another object in memory.
If a variable is a pointer, it must be declared in a different way.
type defines the type of variable the pointer can point to.
* is the complement of &. It returns the value located at the address of the following operator.
type *name;x = &y; // places the value in memory pointed by y into x. So if y contains the memory address of another variable, x will have the same of that 3rd variable
Arrays
An array is a collection of variables of the same type, which is accessed by an index. All arrays have 0 as an index of the first element:
type var_name[size];// Iterateintx[20];int i;for (i=0; i<20; i++) {x[i] = i;}
Functions Study Guide
Functions are blocks of statements defined under a name.
typefunction_name(param1,param2, ...){ statements;}// function with "formal" parametersintsum(int x,int y) {int z; z = x + y;return(z);}
In almost any programming language, there are two ways in which we can pass arguments to a function:
By value
Copies the value of an argument into a parameter. Changes made to the parameter do not affect the argument
Code in the function does not alter the arguments used by the caller
It's a copy of the value of the argument passed into the function
What occurs inside the function has NO EFFECT on the variable provided by the caller
By reference
The address of an argument (not the value) is copied into the parameter
Inside the function, the address is used to access the actual argument used in the call
Changes made to the parameter will affect the argument