Tutorial C++ : Summary of the C++ language / PDF
Tutorial C++ : Summary of the C++ language / PDF
Sample of the pdf document
Comments: //
rest of line is ignored
I/O: cin
>> i; // no & needed
cout
<< “i = “ << i << endl;
manipulators: dec,
hex, setw(int), setfill(int), left, right
Types: bool b; //
either true or false
enum
day_of_week {sun,mon,tue,wed,thu,fri,sat};
day_of_week
d;
const
int MAX 999; // use const instead of
#define
Assignment
operator:
Assignment
also has a value associated with it - the rhs of the assignment
j =
(p=3); // j is assigned 3
Comma
operator:
for
(i=0,j=0;i<10 ...="..." i="i" j="j" o:p="o:p">10>
An
expression involving the comma operator has the value of the right-hand
expression.
k =
(i=4),(j=5); // k is assigned 5
Conditional
operator:
m =
(n>10)? 4 : 5;
break,
continue:
Flow
around a loop may be altered by the use of:
break
- ends the loop
continue
- restarts the loop - increment of a for
loop, test of a while
loop.
Declarations:
Variables
may be declared anywhere. They will exist until the enclosing block terminates.
Default
arguments:
Arguments
to functions may be given default values:
int fct(int i=5,int j=0);
Function
overloading:
Versions
of the same function may exist, if the arguments or return types are different:
int max(int m, int n);
float max(float x, float y);
The
compiler chooses which function to call in the following order:
look
for an exact match
look
for a match by promoting short->long, float->double etc
look
for a match by converting float->int
Inline
functions:
The
overhead of calling a function can be removed if the function is declared inline.
inline cube(int j) {return j*j*j;}
The
compiler will not create inline functions for recursive functions.
Code
size will increase if large inline functions are used.
Call
by reference:
Arguments
that are changed within a function, no longer need to be passed as pointers.
Arguments that are passed using call-by-reference can be altered by the
function:
int
m=3;
change_it(m);
cout
<< m; // m is now 4
...
void change_it(int &n) { //n refers back to m
n =
4;
}
Scope:
Static
local variables retain their value from one function call to another.
Static
global variables are only visible to functions within that particular source
file.
Global
variables can be made visible to other source files using extern.
Functions
are known across all source files by default
Static
functions are known only to the particular source file.
C++
uses namespace to further restrict scope:
namespace XXX {
void
fct(void);
}
fct
is only known within the namespace XXX.
To
access the function either use the scope operator ::
XXX::fct();
or
allow all functions within the namespace to be used directly with:
using namespace XXX;
Header
files that obey the namespace convention have no .h.
Normal
C functions are all within the namespace std
Memory
allocation:
new
and delete are better ways of allocating and
freeing dynamic memory than malloc
and free
int *p,*q;
p = new int;
q = new int[20];
delete p;
delete [] q; //special
form to delete arrays......
Click here for Download PDF / FREE
Tutorial C++ : Summary of the C++ language / PDF
0 commentaires: