Home C/C++/C#
C/C++/C# PDF Print E-mail
Written by Willy Gerber   
Saturday, 21 February 2009 12:31
Basics
Structure
#include <iostream=Directives for the preprocessor
#define PI 3.14159;Definition
using namespace std;Standard C++ library
int main ()
{
Beginning of the definition of the main function
  <Code>Line is a C++ statement
  return 0;
}
Return statement
// line commentLine comment
/* block comment */Block comment
Variable type
charCharacter or small integer.
short int (short)Short Integer.
intInteger.
long int (long)Long integer.
boolBoolean value. It can take one of two values: true or false.
floatFloating point number.
doubleDouble precision floating point number.
long doubleLong double precision floating point number.
wchar_tWide character.
int a;
int a, b, c;
float mynumber;
unsigned short int NumberOfSisters;
signed int MyAccountBalance;
type identifier = initial_value;
type identifier (initial_value);
int a = 0;
int a (0);
string mystring = "This is a string";
string mystring ("This is a string");
\ nnewline
\ rcarriage return
\ ttab
\ vvertical tab
\ bbackspace
\ fform feed (page feed)
\ aalert (beep)
\ 'single quote (')
\ "double quote (")
\ ?question mark (?)
\ \backslash (\)
Operators
a = 5;
a = b + 5;
a = 2 + (b = 5); (C++)
a = b = c = 5; (C++)
Assignment operator
+Addition
-Subtraction
*Multiplication
/Division
%Modulo
value++value = value + 1
value--value = value - 1
value += increase;
value -= decrease;
value /= divide;
price *= units + 1;
value = value + increase;
value = value - decrease;
value = value / divide;
price = price * (units + 1);
==Equal to
!=Not equal to
>Greater than
<Less than
>=Greater than or equal to
<=Less than or equal to
condition ? result1 : result2if condition is true it returns result1 else result2
a = (b=3, b+2); 
&AND: Bitwise AND
|OR: Bitwise Inclusive OR
^XOR: Bitwise Exclusive OR
~NOT: Unary complement (bit inversion)
<<SHL: Shift Left
>>SHR: Shift Right
int i;
float f = 3.14;
i = (int) f;
Type casting operator
a = sizeof (char);Size of a variable
Basic Input/Output
cout << x;Prints the content of x on screen.
cin >> x;Standard input from imput device (usually keyboard) Note: stops at the first space, tab character or newline.
stringstream(mystr) >> myint;Standard input for a general string.
Control Structures
Statements
if (condition_1)
  statement_1;
else if (condition_2)
  statement_2;
else
  statement_3;
Conditions
while (expression){
  statement;
}
While loop
do{
  statement;
}while (condition);
Do while loop
for (initialization; condition; increase) statement;for (int i=0;i<10;i++){
  statement;
}
for (i=0,j=10;i!=j;i++,j--){
  statement;
}
for (initialization; condition; increase) {
  statement_1;
  if (other_condition) continue;
  statement_2;
}
If other_condition fullfilled exist the loop.
loop:
  statement;
  if (condition) goto loop;
If other_condition fullfilled skip to loop tag.
switch (expression)
{
  case constant1:
    group of statements 1;
    break;
  case constant2:
  group of statements 2;
    break;
  default:
    default group of statements
}
Case alternatives.
Functions
format type function(format type variable1, format type variable2,...)
z = function(x,y);
Function definition
action(format type& variable1, format type& variable2,...)& pointer to the variable, function takes action on the variables
function(...)
{
  statements1;
  y = function(...);
  statements2;
}
Recursive calls possible.
void function (int a);

int main ()
{
  statements1;
  y = function(...);
  statements2;
}

function (int a)
{
  statements;
}
calling a subroutine
Compound Data Types
Array
int variable [5];Define an array.
int variable [5] = { 16, 2, 77, 40, 12071 };Define an array and asign values.
variable[2] = 75;Asign an individual value of the array.
int matrix[rows][columns];Definition of a 2 dimensional array (no limit in dimensions)
void procedure (int arg[])
int myarray [40];
procedure (myarray);
Passing an array in an function argument.
char myword [6];
char myword[] = { 'H', 'e', 'l', 'l', 'o', '\0' };
char myword [] = "Hello";
Defining an char array.
string mystring;
char myntcs[]="some text";
mystring = myntcs;
Defining strings.
Pointer
address_of_variable = &variable;Address of a variable.
other_variable = *address_of_variableAssign to other_variable the value of variable.
variable[index]variable [offset of index]
*(variable+index)pointed by (variable+index)
char * variable = "text";Defining a pointer to a string.
*p++=*(p++)Pointer of p increased by one (address of a modify variable)
(*p)++Value pointed by p increased by one (new address)
**variablePointer of pointer (C++)
Dynamic Memory
pointer = new type
pointer = new type [number_of_elements] (C++)
Define pointer
int * variable;
variable = new int [number] (C++)
Define dynamic memory
delete pointer;
delete [] pointer; (C++)
Delete pointer
Structure
struct structure_name {
  type1 name1;
  type2 name2;
  type3 name3;
  .
} object_name1, object_name2;
struct product {
  int weight;
  float price;
} apple, banana, melon;
object_name1.name1apple.weight
pobject_name->name1
(*object_name1).name1
Access from a pointer to the structure a member.
*pobject_name.name1
*(pobject_name.name1)
Pointer of the member
Object Oriented Programming
class class_name {
  private:
    privat_variables
    privat_functions
  public:
   public_variables
    public_functions
  ...
} object_names; Definition of a class

class_name::public_functions {}
Definition of a class
public:
  public_variables
  public_functions()
  public_functions(argument)

class_name::public_functions(){}
class_name::public_functions(argument){}
Overloading Constructors
public_functions function_1;
public_functions function_2(argument);
Calls in the main routine (without ())
Last Updated ( Sunday, 22 February 2009 18:04 )
 
Copyright © 2010 gPhysics. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.