|
All of the C++ info
has been gathered from www.cplusplus.com published by Juan Soulie And also
www.cppreference.com by
www.cppreference.com/credits.html |
For further
details, refer to those two sites. This quick reference closely follows Soulie's site and samples, to make it easier to go back
and forth. |
|
Aurora info gathered from The Help
files and the Aurora Forums by
Paul Turley and all the great members of the forums. |
|
C++ Notes |
C++ |
|
|
|
|
C++ Preprocessor
Directives |
|
|
|
manipulate strings |
#, ## |
|
|
|
define variables |
#define |
same as c++ |
|
|
used to undefine variables |
#undef |
|
|
|
conditional
operators |
#ifdef |
same as c++ |
|
|
conditional
operators |
#ifndef |
same as c++ |
|
|
conditional
operators |
#if |
|
|
|
conditional
operators |
#endif |
same as c++ |
|
|
conditional
operators |
#else |
same as c++ |
|
|
conditional
operators |
#elif |
|
|
|
set line and file
information |
#line |
|
|
|
display an error
message |
#error |
|
|
|
insert the contents
of another file |
#include |
same as c++ |
|
|
implementation
specific command |
#pragma |
|
|
|
|
not used/supported |
*#define |
|
|
|
not used/supported |
#asm |
|
|
|
not used/supported |
#autodefine |
|
|
|
not used/supported |
#typedef |
|
|
|
not used/supported |
#undeclare |
|
|
|
not used/supported |
#use |
|
|
C++ Notes |
C++ Predefined
Macro Names |
|
|
|
|
_LINE_ |
not used/supported |
|
|
|
_FILE_ |
not used/supported |
|
|
|
_DATE_ |
not used/supported |
|
|
|
_TIME_ |
not used/supported |
|
|
|
_cplusplus |
not used/supported |
|
|
C++ Notes |
C++ Program
Structure |
|
|
|
|
namespace |
not used/supported |
|
|
|
int main () { } |
global sub main() { } |
|
|
|
// line comment |
same as c++ |
|
|
|
/* block comment */ |
same as c++ |
|
|
|
C++ Identifiers
Examples |
|
|
|
|
a |
same as c++ |
|
|
|
a1 |
same as c++ |
|
|
|
a_1 |
same as c++ |
|
|
|
No spaces. No
symbols. Can't start with a number. Is case sensitive. |
same as c++ |
Not case sensitive |
|
C++ Notes |
C++ Program
Keywords |
|
|
|
|
not used/supported |
alias |
|
|
same as using && |
and |
same as c++ |
|
|
same as using &= |
and_eq |
not used/supported |
|
|
|
not used/supported |
as |
|
|
insert an assembly
instruction |
asm |
not used/supported |
|
|
declare a local
variable |
auto |
not used/supported |
|
|
same as using bit
operator & |
bitand |
not used/supported |
|
|
same as using bit
operator | |
bitor |
not used/supported |
|
|
declare a boolean variable |
bool |
|
|
|
break out of a loop |
break |
same as c++ |
|
|
|
not used/supported |
baselen |
|
|
|
not used/supported |
byref |
|
|
|
not used/supported |
byte |
|
|
|
not used/supported |
byval |
|
|
a block of code in
a switch statement |
case |
same as c++ |
|
|
handles exceptions
from throw |
catch |
not used/supported |
|
|
declare a character
variable |
char |
not used/supported |
|
|
Apparently Linux
C++ uses this? |
not used/supported |
cdecl |
|
|
declare a class |
class |
same as c++ |
|
|
same as using bit
operator ~ |
compl |
not used/supported |
|
|
declare immutable
data or functions that do not change data |
const |
same as c++ |
|
|
cast from const
variables |
const_cast |
not used/supported |
|
|
bypass iterations
of a loop |
continue |
|
|
|
|
not used/supported |
def |
|
|
default handler in
a case statement |
default |
same as c++ |
|
|
make memory
available |
delete |
|
|
|
looping construct |
do |
same as c++ |
|
|
declare a double
precision floating-point variable |
double |
same as c++ |
|
|
|
not used/supported |
dstring |
|
|
perform runtime
casts |
dynamic_cast |
not used/supported |
|
|
alternate case for
an if statement |
else |
same as c++ |
|
|
create enumeration
types |
enum |
not used/supported |
|
|
only use
constructors when they exactly match |
explicit |
not used/supported |
|
|
intended to allow
template definitions to be separated from their declarations (not supported
by some compilers) |
export |
|
|
|
tell the compiler
about variables defined elsewhere |
extern |
|
|
|
the boolean value of false |
false |
same as c++ |
|
|
declare a
floating-point variable |
float |
same as c++ |
|
|
looping construct |
for |
same as c++ |
|
|
grant non-member
function access to private data |
friend |
not used/supported |
|
|
|
not used/supported |
global |
|
|
jump to a different
part of the program |
goto |
same as c++ |
|
|
Definition: memory
from this area allocated during execution using the new operator in C++ |
not used/supported |
heap |
|
|
execute code based
off of the result of a test |
if |
same as c++ |
|
|
optimize calls to
short functions |
inline |
not used/supported |
|
|
|
not used/supported |
import |
|
|
declare a integer
variable |
int |
same as c++ |
|
|
|
not used/supported |
int64 |
|
|
|
not used/supported |
interface |
|
|
|
not used/supported |
len |
|
|
declare a long
integer variable |
long |
not used/supported |
|
|
override a const
variable |
mutable |
not used/supported |
|
|
partition the
global namespace by defining a scope |
namespace |
not used/supported |
|
|
allocate dynamic
memory for a new variable |
new |
same as c++ |
|
|
same as using ! |
not |
same as c++ |
|
|
same as using != |
not_eq |
not used |
|
|
|
not used/supported |
null |
|
|
create overloaded
operator functions |
operator |
not used/supported |
|
|
same as using || |
or |
same as c++ |
|
|
same as using |= |
or_eq |
not used/supported |
|
|
|
not used/supported |
pointer |
|
|
|
not used/supported |
print |
|
|
declare private
members of a class |
private |
not used/supported |
|
|
declare protected
members of a class |
protected |
not used/supported |
|
|
declare public
members of a class |
public |
not used/supported |
|
|
request that a
variable be optimized for speed |
register |
not used/supported |
|
|
change the type of
a variable |
reinterpret_cast |
not used/supported |
|
|
return from a
function |
return |
same as c++ |
|
|
|
not used/supported |
select |
|
|
declare a short
integer variable |
short |
not used/supported |
|
|
modify variable
type declarations |
signed |
not used/supported |
|
|
return the size of
a variable or type |
sizeof |
not used/supported |
|
|
|
not used/supported |
sprint |
|
|
create permanent
storage for a variable |
static |
not used/supported |
|
|
perform a nonpolymorphic cast |
static_cast |
not used/supported |
|
|
|
not used/supported |
string |
|
|
define a new
structure |
struct |
same as c++ |
|
|
|
not used/supported |
sub |
|
|
execute code based
off of different possible values for a variable |
switch |
same as c++ |
|
|
create generic
functions |
template |
not used/supported |
|
|
a pointer to the
current object |
this |
same as c++ |
|
|
throws an exception |
throw |
not used/supported |
|
|
the boolean value of true |
true |
same as c++ |
|
|
execute code that
can throw an exception |
try |
not used/supported |
|
|
create a new type
name from an existing type |
typedef |
not used/supported |
|
|
describes an object |
typeid |
not used/supported |
|
|
declare a class or
undefined type |
typename |
not used/supported |
|
|
a structure that
assigns multiple variables to the same memory location |
union |
same as c++ |
|
|
declare an unsigned
integer variable |
unsigned |
same as c++ |
|
|
|
not used/supported |
until |
|
|
import complete or
partial namespaces into the current scope |
using |
not used/supported |
|
|
|
not used/supported |
va_start |
|
|
create a function
that can be overridden by a derived class |
virtual |
same as c++ |
|
|
declare functions
or data with no associated data type |
void |
|
|
|
warn the compiler
about variables that can be modified unexpectedly |
volatile |
not used/supported |
|
|
declare a
wide-character variable |
wchar_t |
not used/supported |
|
|
looping construct |
while |
same as c++ |
|
|
|
not used/supported |
word |
|
|
same as using ^ |
xor |
same as c++ |
|
|
same as using ^= |
xor_eq |
|
|
|
C++ Notes |
C++ Fundamental
Data Types |
|
|
1byte |
signed: -128 to 127
unsigned: 0 to 255 |
char |
byte |
1 byte |
2bytes |
signed: -32,768 to
32,767 unsigned: 0 to 65,535 |
short int (short) |
word |
2 bytes |
4bytes |
signed:
-2,147,483,648 to 2,147,483,647 unsigned: 0 to
4,294,967,295 |
int |
int |
4bytes |
4bytes |
signed:
-2,147,483,648 to 2,147,483,647 unsigned: 0 to
4,294,967,295 |
long int (long) |
|
|
|
|
|
int64 |
8 bytes an integer
from 0 -18446744073709551615 (if unsigned) |
1byte |
true or false |
bool |
|
|
4bytes |
3.4e +/- 38 (7
digits) |
float |
float |
|
8bytes |
1.7e +/- 308 (15
digits) |
double |
double |
|
12bytes |
1.7e +/- 308 (15
digits) |
long double |
|
|
2bytes |
1 wide character |
wchar_t |
|
|
|
|
not used/supported |
pointer |
an address |
|
|
not used/supported |
string |
string of
characters up to a length of 255 |
|
|
not used/supported |
dstring |
a dimensionable string that can contain any number of
characters |
|
|
not used/supported |
wstring |
a unicode string up to a length of 255 characters |
|
|
not used/supported |
dwstring |
a dimensionable unicode string
that can contain any number of characters |
|
C++ Notes |
C++ Valid
Variable Declarations |
|
|
|
|
int a; |
same as c++ |
|
|
|
float mynumber; |
same as c++ |
|
|
|
int a, b, c; |
same as c++ |
|
|
|
int a; int b; int c; |
same as c++ |
|
|
|
unsigned short int NumberOfSisters; |
unsigned int NumberOfSisters; |
|
|
int MyAccountBalance;
same as signed int MyAccount |
signed int MyAccountBalance; |
int MyAccountBalance; |
|
|
|
short is equivalent
to short int |
|
|
|
|
long is equivalent
to long int |
|
|
|
|
signed is
equivalent to signed int |
|
|
|
|
unsigned is
equivalent to unsigned int |
|
|
|
C++ Notes |
C++ Scope of
Variables |
|
|
|
While a local
variable is one declared within the body of a function or a block. Global
variables can be referred from anywhere in the code even inside functions,
whenever it is after its declaration. |
A variable can be
either of global or local scope. A global variable is a variable declared in
the main body of the source code outside all functions. |
|
|
|
C++ Notes |
C++
Initialization of Variables |
|
|
|
Both ways of
initializing variables are valid and equivalent in C++. |
type identifier = initial_value ; |
same as c++ |
|
|
|
int a = 0; |
same as c++ |
|
|
|
string mystring = "This is a string"; |
same as c++ |
|
|
type identifier (initial_value) ; |
int a (0); |
not used/supported |
|
|
type identifier (initial_value) ; |
string mystring ("This is a string"); |
not used/supported |
|
|
Strings can also
perform all the other basic operations that fundamental data types can, like
being declared without an intial value and being
assigned values during execution. |
string mystring; mystring = "This is the initial
string content"; |
same as c++ |
|
|
C++ Notes |
C++ Constants |
|
|
|
the 5 in this piece of code was a
literal constant. |
literal a = 5; |
same as c++ |
|
|
75 decimal |
75 |
same as c++ |
|
|
75 octal |
0113 |
|
|
|
75 hexadecimal |
0x4b |
|
|
|
We can force them
to either be unsigned by appending the u character to it or long by appending
l. (lowercase L) |
75 //int 75u //unsigned int 75l //long 75ul //unsigned long |
|
|
|
Default type for
floating point literals is double If you explicitly want to express a float
or long double. In this case the case does not matter. |
3.14159 //3.14159
6.02e23 //6.02 x 1023 1.6e-19 //1.6 x 10-19 3.0 //3.0 3.14159L // long double 6.02e23f // float |
|
|
|
When writing both
single character and string literals it is necessary to put the quotation
marks surrounding them to distinguish them from possible variable identifiers
or reserved keywords. |
'z' 'p' "Hello
World" "How do you
do?" |
Hello World" "How do you
do?" |
'z' and 'p' are not
used, that is single quotes not supported |
|
boolean literals and are values of type bool |
true false |
same as c++ |
|
|
defined constants. uses
less memory than having variables. since this is a
preprocessor directive a ; is not required at the end. |
#define identifier
value #define PI
3.14159265 #define NEWLINE
'\n' |
|
|
|
declared constants
(const) you can declare constants with a
specific type. If none is given, int is used. |
const int pathwidth = 100; const char
tabulator = '\t'; const zipcode = 124400; |
|
|
|
C++ Notes |
C++ Escape Codes |
|
|
|
Newline |
\n |
same as c++ |
|
|
Carriage return |
\r |
|
|
|
Horizontal tab |
\t |
same as c++ |
|
|
Vertical tab |
\v |
|
|
|
Backspace |
\b |
|
|
|
Formfeed
(page feed) |
\f |
|
|
|
Audible bell alert (beep) |
\a |
|
|
|
Single quote |
\' |
|
|
|
Double quote |
\" |
same as c++ |
|
|
Question mark |
\? |
|
|
|
Backslash |
\\ |
same as c++ |
|
|
Hexadecimal number
(nnn) |
\xnnn |
\xnn |
An ASCII character
whose hex value is 'nn'. |
|
Octal number (nnn) |
\nnn |
|
|
|
Null character
(really just the octal number zero) |
\0 |
|
|
|
C++ Notes |
C++ Assignation
Operators |
|
|
|
assign a value to a
variable |
= |
same as c++ |
|
|
assigns 5 to all 3 variables. |
a = b = c = 5; |
not used/supported |
|
|
C++ Notes |
C++ Arithmetic
Operators |
|
|
|
Addition |
+ |
same as c++ |
|
|
Subtraction |
- |
same as c++ |
|
|
Multiplication |
* |
same as c++ |
|
|
Division |
/ |
same as c++ |
|
|
Modulus |
% |
same as c++ |
Modulus. Remainder
of an integer division. |
|
C++ Notes |
C++ Compound
Assignation Operators |
|
|
|
a += b; is the same as a = a + b; |
+= |
a=a+b: |
|
|
a -= b; is the same as a = a - b; |
-= |
a=a-b: |
|
|
a /= b; is the same
as a = a / b; |
/= |
a=a/b; |
|
|
a *= b; is the same
as a = a * b; |
*= |
a=a*b; |
|
|
C++ Notes |
C++ Bitwise
Compound Assignation Operators |
|
|
|
Bitwise shift right
and assign |
>>= |
|
|
|
Bitwise shift left
and assign |
<<= |
|
|
|
Bitwise AND and assign |
&= |
|
|
|
Bitwise exclusive
OR and assign |
^= |
|
|
|
(shift+ \)= Bitwise
inclusive (normal) OR and assign |
|= |
|
|
|
C++ Notes |
C++ Misc.
Operators |
|
|
|
Grouping
operator: (a + b) / 4; |
( ) |
same as c++ |
|
|
Array access: array[4] = 2; |
[ ] |
same as c++ |
|
|
Member access from
a pointer: ptr->age
= 34; |
-> |
same as c++ |
|
|
Member access from
an object: obj.age
= 34; |
. |
same as c++ |
|
|
Scoping
operator: Class::age
= 2; |
:: |
same as c++ |
|
|
Member pointer
selector: ptr->*var = 24; |
->* |
|
|
|
Member pointer
selector: obj.*var = 24; |
.* |
|
|
|
|
C++
Increment/Decrement Operators |
|
|
|
a++; same as a+=1;
same as a = a + 1; |
++ |
same as c++ |
|
|
a--; same as a-=1;
same as a = a - 1; |
-- |
same as c++ |
|
|
a contains 4, b contains
4 |
b=3; a=++b; |
b=3; b++; a=b; |
|
|
a contains 3, b
contains 4 |
b=3; a=b++; |
b=3; a=b; b++; |
|
|
C++ Notes |
C++ Relational
and Equality Operators |
|
|
|
Comparison equal to |
== |
same as c++ |
|
|
Comparison not
equal to |
!= |
same as c++ |
|
|
Comparison reater than |
> |
same as c++ |
|
|
Comparison less
than |
< |
same as c++ |
|
|
Comparison greater
than or equal to |
>= |
same as c++ |
|
|
Comparison less
than or equal to |
<= |
same as c++ |
|
|
C++ Notes |
C++ Logical
Operators |
|
|
|
not operand,
inverses the value true to false, false to true |
! |
same as c++ |
|
|
and operand |
&& |
same as c++ |
|
|
or operand |
|| |
same as c++ |
|
|
C++ Notes |
C++ Conditional
Operators |
|
|
|
Ternary conditional
operator, returns a value if that exression is true and a different one if false. condition
? result1 :
result2 |
? : |
not used/supported |
|
|
Comma sequential
operator. a = (b=3, b+2); in the end a would contain 5 and b a value of
3. |
, |
not used/supported |
|
|
C++ Notes |
C++ Bitwise
Operators |
|
|
|
AND |
& |
same as c++ |
|
|
OR inclusive OR |
| (shift+ \) |
same as c++ |
|
|
XOR exclusive OR |
^ |
|
|
|
NOT bit inversion. Bitwise complement |
~ |
|
|
|
SHL shift left |
<< |
same as c++ |
|
|
SHR shift right |
>> |
same as c++ |
|
|
C++ Notes |
C++ Explicit
Type Casting Operator |
|
|
|
(type) Cast to a
given type |
f is a float, i
= (int) f; makes f an int. another way to
write it, i = int
(f); |
|
|
|
|
C++ Sizeof Operator |
|
|
|
Return size in
bytes |
a = sizeof (char); a will = 1, for one byte. |
|
|
|
C++ Notes |
C++ Basic Input
/ Output Console Commands |
|
|
|
cout is used with the insertion
operator << |
cout << 120; outputs 120 |
print
("120"); |
|
|
cin is used with the extraction
operator >> |
cin >> age; waits for the RETURN key to be entered to
process the input. |
age = readln(); |
|
|
to input strings properly should use
getline with cin. |
getline (cin, mystr); |
name = readln(); |
|
|
extracts the value from the string as
input from the line above and assigns it to price and outputs it. |
stringstream (mystr)
>> price; |
price = StrToNum(readln()); |
|
|
C++ Notes |
C++ Conditional
Structures |
|
|
|
"if
(condition) statement; OR if (condition) { statements; } |
if |
same as c++ |
|
|
"if
(condition) statement; else statement; OR if (condition) { statements; } else { statements; } |
if else |
same as c++ |
|
use braces for blocks of multiple
statements, as in the previous 2 examples. |
if (condition) statement; else if (condition) statement; else statement; |
if -
else if - else |
same as c++ |
|
|
C++ Notes |
C++ Loops |
|
|
no braces needed
for single line statements |
while (expression) { statements; } |
while |
same as c++ |
|
no braces needed
for single line statements |
do { statements; } while (condition); |
do - while |
do { statements; } until (condition); |
|
no braces needed
for single line statements |
for
(initialization; condition; increase/decrease) { statements; } |
for |
same as c++ |
|
refer to the main site, for more info. |
allows you to put more than one
expression in any of the fields in a for loop using a comma. |
for with comma
operator |
not used/supported |
|
|
C++ Notes |
C++ Jump
Statements |
|
|
leave any loop even if the condition is
not fulfilled. |
for
(initialization; condition; increase/decrease) { statements; if (condition) { statements; break; } } |
break |
|
|
skip current
iteration and continue with the next |
for
(initialization; condition; increase/decrease) { if (condition) continue; statements; } |
continue |
|
|
absolute jump from the goto
to the labeled position. The label is followed by a : |
statements; goto stargate; statements; stargate: statements; |
goto
our label is stargate: |
|
|
exitcode of 0 terminated normally |
void exit (int exitcode); |
exit function |
|
|
there can be a long list of case
constants, just listed 2 for this example. default is an
optional label |
switch (expression) { case constant1: statements; break; case constant2: statements; break; default: statements; } |
switch |
select (expression) { case constant1: statements; break; case constant2: statements; break; default: statements; } |
select is used and switch is an
acceptable alias. everything else is the same. |
|
C++ Notes |
C++ Functions
Arguments Passed by Value |
|
|
type is the type of
the return value |
type name (param1, param2,...) { statements; } |
int addition (int
a, int b) { int r; r = a+b; return (r); } |
sub addition (int a, int b),int { int r; r = a+b; return r; } |
the int at
the end is the type of the return value. |
|
calling the
function above and getting a value back |
int z; z = addition (5,3); |
same as c++ |
|
|
no type function |
void printmessage () { cout <<
"I'm a function"); } |
sub printmessage () { print("I'm a function"); } |
|
|
calling the function
above and not expecting a value back |
printmessage (); |
printmessage (); |
|
|
C++ Notes |
C++ Functions
Arguments Passed by Reference |
|
|
|
if we call the
function to the right with: int x=1, y=3, z=7; doubleValue(x,y,z); since the function was passing
arguments by reference, any change we do within the function on a,b and c will affect the values of x, y and z outside of
the function. So x=2, y=6 and z=14 after the function call. |
void doubleValue(int& a, int& b, int& c) { a = a * 2; b = b * 2; c = c * 2; } |
|
|
|
C++ Notes |
C++ Functions
Default Values in Parameters |
|
|
|
the second parameter is the one with
an assigned default value. If no second parameter is sent, then in this case,
2 would be used. divide (12); would return 6. divide (20,4) would return 5, the 4 would replace the 2
since it was specified. |
int divide (int
a, int b=2) { int r; r = a / b; return (r); } |
|
|
|
C++ Notes |
C++ Overloaded
Functions |
|
|
|
2 different
functions can have the same name if their parameter types or number of
parameters are different. a function cannot be
overloaded only by its return type. at least one of
its parameters must have a different type. |
int operate (int
a, int b) { return (a/b); } float operate
(float a, float b) { return (a/b); } |
|
|
|
C++ Notes |
C++ Inline
Functions |
|
|
|
Inline specifier is an optimization specifier.
Represents an overhead advantage for very short functions. |
inline type name ( arguments...) { statements; } |
|
|
|
C++ Notes |
C++ Recursivity of Functions |
|
|
|
Recursivity is the property that functions
have to be called by themselves. |
long factorial
(long a) { return (a * factorial (a-1); else return (1); } |
|
|
|
C++ Notes |
C++ Declaring
Functions |
|
|
|
Declaring is just a
prototype of the function before it is used. You end it with a ; like a normal statment. For
the parameters, only the type is required, the name is optional. These are
placed near the beginning of your program. |
Both of these would
work: void odd (int); void even (int a); |
|
|
|
C++ Notes |
C++ Initalizing Arrays |
|
|
|
Possibility to
assign initial values to each one of its elements by enclosing the values in
braces { }. |
int a [5] = { 16,2,77,40,12071}; |
|
|
|
Empty brackets, the
compiler will assume a size for the array by the number of values included. |
int a [ ] = { 16,2,77,40,12071}; |
|
|
|
Multidimensional
array initialization |
int a[2] [3]; |
|
|
|
C++ Notes |
C++ Accessing
Values of Arrays |
|
|
|
Place the value 75
into the third element of the array a. |
a[2]=75; |
|
|
|
The value of the
third element of array a will be passed to variable b. |
b=a[2]; |
|
|
|
C++ Notes |
C++ Arrays as
Parameters |
|
|
|
In order to accept
arrays as parameters, when declaring the function, specify in its parameters
the element type of the array, an identifier and [] |
void procedure (int arg[]) |
|
|
|
To pass to this
function an array declared as: |
int myarray
[40]; |
|
|
|
It would be enough
to write a call like this: |
procedure (myarray); |
|
|
|
A function with a
multidimensional array as argument could be: The first brackets are left
blank. |
void procedure (int myarray[][3][4]) |
|
|
|
C++ Notes |
C++ Arrays for
Character Sequences |
|
|
\0 is the null
character. |
We can initialize
the array of char elements called myword with a
null-terminated sequence of characters by either one of these two methods: |
char myword[]={'H','e','l','l','o','\0'}; or char myword[]="Hello"; |
|
|
|
C++ Notes |
C++ Pointers |
|
|
|
& Reference
Operator. Can literally be translated as "address of" |
ted = &andy; would assign ted the address of andy |
|
|
|
* Dereference
Operator. Can literally be translated as "value pointed by" |
ted = *andy; ted equal to the value pointed by andy |
|
|
|
C++ Notes |
C++ Variables of
Pointer Types |
|
|
|
The ability of a
pointer to directly refer to the value that it points to, then needs to
specify which data type is being pointed at. |
type * name; for example: int * myVar; myVar points to an int. |
|
|
|
C++ Notes |
C++ Pointers and
Arrays |
|
|
|
In this declaration
the array myArray is actualy
always pointing to the first element of the array and can therefore be
thought of as a constant pointer. We can't assign values to constants. |
int myArray
[20]; |
|
|
|
We can change the
value of a pointer. So in this case all on the right will be valid. |
int myArray
[20]; int * p; p = myArray; |
|
|
|
But, this on the
right would be invalid. Because myArray only and
always points to the first element of myArray. |
myArray = p; this is invalid. |
|
|
|
C++ Notes |
C++ Pointer
Initialization |
|
|
|
When declaring
pointers we want to explicitly specify which variable we want them to point
to: |
int number; int *tommy
= &number; |
|
|
|
The code above is
equivalent to: |
int number; int *tommy; tommy = &number; |
|
|
|
We can initialize
the content where the pointer points on declaration. |
char * terry =
"hello"; |
|
|
|
Terry points to
what in essence is an array. In this case terry points to the first element
of the character sequence that contains the characters for hello and the null
terminator \0. We can access the elements of the sequence just as we can in
an array as: |
*(terry+1) or terry[1] which would be the
letter e |
|
|
|
C++ Notes |
C++ Pointer Arithmetics |
|
|
|
Addition and
Subtraction are supported. What is calculated is the size in bytes of the
type pointed by the pointer. So a char type with 1 byte would add or subtract
1 byte. A short 2 bytes and a long 4 bytes. |
char *mychar; mychar++; mychar would now be the address 1 byte over in memory. |
|
|
|
Because of
precedence, things can get confusing. The example at the right would increase
p to point to the next element, but it would actually still return the value
of the location before being increased. The compiler would read this example
as *(p++) |
*p++; |
|
|
|
This would add 1 to
the value pointed to by the pointer. It would not increase the pointer itself
as above. |
(*p)++; |
|
|
|
*p++ = *q++; Again because of
precedence. This would be the equivalent of: |
*p =*q; ++p; ++q; |
|
|
|
C++ Notes |
C++ Pointers to
Pointers |
|
|
|
Pointers can point
to pointers which in turn can point to other pointers. You add an * for each level
of reference in their declarations. The example at the right would give these
results: c has the address
of b and would be type char** *c has the address
of a and would be type char* **c has the value
of 'z' and would be type char |
char a; char * b; char ** c; a = 'z'; b = &a; c = &b; |
|
|
|
C++ Notes |
C++ Void
Pointers |
|
|
|
Void represents
absence of type. Void pointers can point to any data type. In order to
dereference a void pointer it has to be type cast to a concrete data type. |
One of its uses
might be to pass generic parameters to a function. void test (void*
data, int c) |
|
|
|
C++ Notes |
C++ Null Pointer |
|
|
|
Null pointer is a
pointer of any pointer type that says I am pointing nowhere. You type cast
the integer value of 0 to the pointer. |
int * p; p = 0; p now has a null
pointer value |
|
|
|
C++ Notes |
C++ Pointers to
Functions |
|
|
|
Pointers to
functions are typically used to pass a function as an argument to another
function. On the right, the example will declare a pointer to a function and
then assign it to point to the function subtraction, all in a single line.
Note the syntax: (*name)(parameter types) |
int subtraction (int
a, int b) { return (a-b); } int (*minus)(int,
int) = subtraction; |
|
|
|
C++ Notes |
C++ Dynamic
Memory |
|
|
|
Dynamic memory allocation
allows us to assign memory during the execution of the program. We do this by
assigning dynamic memory to a pointer. The operator new is used, its form is: pointer = new
type or pointer = new type
[elements] |
int * bobby; bobby = new int [5]; The first element
of bobby now can be accessed as: bobby [0] or *bobby the next element
would be accessed as: bobby[1] or *(bobby+1) |
|
|
|
Since computer
memory is limited, a check is required to make sure the memory was allocated
properly. |
C++ offers two methods
to check allocation. The first is handling exceptions and is the default when
using new. |
|
|
|
An exception of bad_alloc is thrown in this method. It is not handled by
a specific handler and the program terminates. |
This method would
be declared as above and be thrown. bobby = new int
[5]; if it fails, the program will terminate. |
|
|
|
The second method
is known as nothrow. If allocation fails, bad_alloc is not is not thrown and a null pointer is
returned and the program continues. |
This method would
be declared as: bobby = new (nothrow) int [5]; If it fails, a null
pointer is returned and the program continues. |
|
|
|
We free memory
allocated with new by using delete. It comes in two formats. One for single
element allocations and the second for arrays of elements. |
single element: delete pointer; multi-element: delete [ ] pointer; |
|
|
|
C++ Notes |
C++ Data
Structures |
|
|
|
A data structure is
a group of data elements, known as members, grouped under one name. Its form: struct structureName{ memberType1 memberName1; memberType2 memberName2; } |
struct product { int weight; float price; } ; product apple; product banana,
melon; |
|
|
|
We can also declare
the structure and declare the structure objects as: |
struct product { int weight; float price; } apple, banana,
melon; |
|
|
|
Once the objects
have been declared we can access their members directly with the dot (.)
inserted between the object name and the member name. And then operate on
them as with any variable of their type. |
apple.weight = 2; apple.price = 0.39; banana.price = 1.49; melon.weight = 4; |
|
|
|
C++ Notes |
C++ Pointers to
Structures |
|
|
|
Structures, like
any other type can be pointed by its own unique pointers. This example is
completely valid. |
struct movies{ string title; int year; } ; movies amovie; movies * pmovie; pmovie = &amovie; |
|
|
|
Since structures
have members. We have access to them with the -> arrow operator. This
operator serves to access a member of an object to which we have a reference.
Below is a summary or possible combinations. |
pmovie->title; is equivalent to: (*pmovie).title; |
|
|
expression a.b |
what is eavaluated member b of object
a |
equivalent |
|
|
a->b |
member b of object
pointed by a |
(*a).b |
|
|
*a.b |
value pointed by
member b of object a |
*(a.b) |
|
|
|
C++ Notes |
C++ Nesting
Structures |
|
|
|
Structures can be
nested. A valid element of a structure could be another structure. Using the
example on the right, all below are valid expressions: charlie.name maria.favMovie.title charlie.favMovie.year pfriends->favMovie.year (the last two refer
to the same member) |
struct movies{ string title; int year; ) ; struct friends{ string name; movies favMovie; } charlie, maria; friends * pfriends = &charlie; |
|
|
|
C++ Notes |
C++ Defined Data
Types |
|
|
|
Typedef is the keyword that allows us to
define our own types based on existing data types. typedef existingName
newTypeName; Typedef only creates synonyms of existing
types. It is useful as aliases to a frequently used type, or for a type that
might need to be changed in the future. |
typedef char C; typedef unsigned int
WORD; typedef char * pChar; typedef char field [50]; we have defined four data types as
types char, unsigned int, char* and char[50]
respectively. |
|
|
|
C++ Notes |
C++ Unions |
|
|
|
Unions look like
structures, but are totally different. It allows a portion of memory to be
shared by thetypes in the union, but only can be
used by one of them at a time. |
union unionName{ memberType1
memberName1; memberType2
memberName2; } objectName; |
|
|
|
The example at the
right defines three elements: myTypeA.c myTypeA.i myTypeA.f Any modification
will affect the value of all of them since only one can be using the space at
a time. |
union myTypes{ char c; int i; float f; } myTypeA; |
|
|
|
This example
defines three names that allow access to the same 4bytes of memory. mix.l, mix.s
and mix.c We can access these
4 bytes in different ways. mix.l would access all 4 bytes at one time. mix.s.hi and mix.s.lo would access 2
bytes at a time. And to access each byte we could use: mix.c[0] mix.c[1]
mix.c[2] mix.c[3] Union memory is
platform dependent and could cause problems with portability. |
union mixT{ long l; struct { short hi; short lo; } s; char c[4]; } mix; |
|
|
|
C++ Notes |
C++ Anonymous
Unions |
|
|
|
structure with
regular union We can access the
members as: book.price.dollars book.price.yens |
struct{ char title[50]: char author[50]; union { float dollars; int yens; } price; } book; |
|
|
|
structure with
anonymous union We can access the
members as: book.dollars book.yens |
struct{ char title[50]: char author[50]; union { float dollars; int yens; } ; } book; |
|
|
|
C++ Notes |
C++ Enumerations |
|
|
|
enum enumerationName{ value1, value2, } objectNames; The example at the
right would all be valid. |
enum colors {black, blue, green, red}; colors mycolor; mycolor = blue; if (mycolor == green) mycolor =
red; |
|
|
|
Enumerations have
their constants assigned an integer internally. If an integer is not
specified 0 is assigned in an a +1 progression. |
black would be 0 blue would be 1 green 2 and red 3. |
|
|
|
Since we specified
1, all the others will be a +1 progression. Unless, they too are assigned an
integer by the programmer. In this case, jan is 1, feb is 2 and mar is 3. |
enum months { jan=1,feb,mar}
test; |
|
|
|
C++ Notes |
C++ Classes |
|
|
|
A class is like a
structure, it can hold not only data, but also functions to handle that data. An object is an
instantiation of a class. Looking at it as a variable, the class would be the
type and the object the variable. |
class className{ accessSpecifier1; member1; accessSpecifier2; member2; } objectNames; |
|
|
|
Access specifier is optional. The default is private. Private
members are accessible only from within other member of the same class or
friends. Protected members are like Private but can also be accessed by
derived classes. Public members are accessable from
anywhere the object is seen. CRect::setValue
This is how you define a function of a class outside of the class
using the scope operator :: The function, area() is defined within the class in this example. |
class CRect{ int a; int b; public: void setValue
(int,int); int area ()
{ return (x*y);} } rect; void CRect::setValue (int a, int b){ x=a; y=b; } |
|
|
|
C++ Notes |
C++ Classes -
Constructor |
|
|
|
A Constructor is a
special function in a class that is called automatically when a new object of
this class is created. It allocates memory needed for the object. It must
have the same name as the class and can't have any return
type, not even void. Like any other function a constructor can be
overloaded. |
So using the
example above. The constructor for it would look like: CRect(); Where the other functions are
declared. Defined outside of
the class as: CRect::CRect(){ } |
|
|
|
C++ Notes |
C++ Classes -
Destructor |
|
|
|
A Destructor
automatically releases the dynamic memory allocated during the objects
creation. It has the same rules for naming as a Constructor, except it is
prefixed with a ~ |
So using the
example above. The desstructor for it would look
like: ~CRect();
Where the other functions are declared. Defined outside of
the class as: CRect::~CRect(){ } |
|
|
|
C++ Notes |
C++ Classes -
Default Constructor |
|
|
|
If no constructor
is declared in the class, the compiler will create a default constructor with
no parameters. It will also create the destructor, a copy constructor and a
copy assignment operator functions |
The copy
constructor and copy assignment operator copy the data in another object to
the data members of the current object. |
|
|
|
C++ Notes |
C++ Pointers to
Classes |
|
|
|
Is a pointer to an
object of class CRect. |
CRect * prect; |
|
|
|
A summary of how
class pointers and class operators can be read as. |
Expression |
|
|
|
pointed by x |
*x |
|
|
|
address of x |
&x |
|
|
|
member y of object
x |
x.y |
|
|
|
member y of object
pointed by x |
x->y |
|
|
|
member y of object
pointed by x |
(*x).y |
|
|
|
first object
pointed by x |
x[0] |
|
|
|
second object
pointed by x |
x[1] |
|
|
|
(n+1)th object pointed by x |
x[n] |
|
|
|
C++ Notes |
C++ Classes
Defined with Struct and |
|
|
|
Class and Struct as so similar, in C++ they can be viewed as
equivalent. The only difference, class default is private acess
and struct is public. |
Unions only store
one data member at a time, but they are classes and can hold function
members. Access is default public |
|
|
|
C++ Notes |
C++ Classes
Overloading Operators |
|
|
|
To overload an
operator to use it with classes we declare operator functions. Name them as:
operator keyword followed by the operator sign to overload. type operator sign (parameters) {....} For example declare
+ operator as overloaded in the class CVector as: CVector operator + (CVector); |
The definition
outside the class would be: CVector CVector::operator+
(CVector) { statements; } |
|
|
|
Overloadable Operators are: +
- * /
= < >
+= -= *=
/= << >> <<= >>= == != <=
>= ++ -- % & ^ ! | ~ &= ^=
|= && || %= [] () , ->* -> new
delete new[] delete[] |
In the example
above. The operator+ function can be called either implicitly using the
operator, or explicitly using the function name. Say we created 3 objects
from CVector, a,b and c. c = a+b; using the
operator c = a.operator+ (b); using the function name |
|
|
|
C++ Notes |
C++ Classes This
Keyword |
|
|
|
This is a pointer
to the object whose member function is currently being executed. All member
functions of a class have a this pointer. |
|
|
|
|
C++ Notes |
C++ Classes
Static Members |
|
|
|
A static member can
be either data or a function within a class. It is a member of that class.
Since it is static it can be referred to from objects created by that class
or by the class name. This can only happen with static members, as they are
members of the class and not the created object's member. |
For example if we
had a class named CTest with a static member n. CTest a; we just created an object a The following 2
expressions would refer to the same static class member n. a.n; CTest::n; |
|
|
|
C++ Notes |
C++ Classes
Friendship |
|
|
|
The friend keyword
allows us to give outside functions and classes
access to the private and protected members of our class that they are a
friend to. The use of friend functions is out of an object-oriented
programming methodology. It is better to use members of the same class to
perform operations with them. |
Here is a sample of
a friend function. friend CRectangle duplicate (CRectangle); This example would
make this a friend of the class it is placed in: friend class CRectangle; |
|
|
|
C++ Notes |
C++ Classes
Inheritance |
|
|
|
Inheritance allows to create classes which are derived from other classes so
that they automatically include some of its "parent's" members plus
its own. A derived class inherits every member of a base class except: its
constructor and destructor, its operator=() members and its friends |
Multiple inheritance is perfectly possible that a class inherits
members from more than one class. This is done by simply separating the
different base classes with commas in the derived class declaration. |
|
|
|
C++ Notes |
C++ Polymorphism Pointers to Base Class |
|
|
|
C++ Notes |
C++ Polymorphism Virtual Members |
|
|
|
C++ Notes |
C++ Polymorphism Abstract Base Classes |
|
|
|
C++ Notes |
C++ Function Templates |
|
|
|
C++ Notes |
C++ Class Templates |
|
|
|
C++ Notes |
C++ Template Specialization |
|
|
|
C++ Notes |
C++ Templates Non-Type Parameters |
|
|
|
C++ Notes |
C++ Templates and Multiple File Projects |
|
|
|
C++ Notes |
C++ Namespaces |
|
|
|
C++ Notes |
C++ Exceptions |
|
|
|
C++ Notes |
C++ Type Casting Implicit Conversion |
|
|
|
C++ Notes |
C++ Type Casting Explicit Conversion |
|
|
|
C++ Notes |
C++ Type Casting Dynamic Cast |
|
|
|
C++ Notes |
C++ Type Casting Static Cast |
|
|
|
C++ Notes |
C++ Type Casting Reinterpret Cast |
|
|
|
C++ Notes |
C++ Type Casting Const Cast |
|
|
|
C++ Notes |
C++ Type Casting Typeid |
|
|
|
C++ Notes |
C++ Preprocessor Directives |
|
|
|
C++ Notes |
C++ Type Casting Static Cast |
|
|
|
|
|
|
|