C++
|
C
|
Variables can be declared at any point in the code.
void main( void )
{
int n;
n = 0;
double d;
}
|
Variables can be declared only at the beginning of a block,
before any executable statements.
void main( void )
{
int n;
double d;
n = 0;
}
|
/* begins a comment and */ ends one.
Also all text on a line following // is a comment.
/*
a comment
*/
void func( void ); // comment
|
Only the /* ... */ style is used
/*
a comment
*/
|
Functions can be overloaded.
Two functions can have the same name
if they have different parameter lists.
int max( int, int );
double max( double, double );
|
Every function must have a unique name.
int int_max( int, int );
double double_max( double, double );
|
The operators 'new' and 'delete' are used to allocate
and deallocate storage in the heap.
char* p = new char[ 20 ];
delete [] p;
|
The functions 'malloc' and 'free' are used to allocate
and deallocate storage in the heap.
char* p = malloc( 20 * sizeof char );
free p;
|
streams are used for input and output
cin >> n;
cout << "n = " << n << endl;
|
printf and scanf are used for formatted input and output.
Other functions exist for reading and writing characters
and strings.
scanf( "%d", &n );
printf( "n = %d/n", n );
|
A struct defines a type which can be used to declare
variables later in the code.
struct point
{
int x;
int y;
};
point p;
|
A struct and a typedef define a type which can be used to declare
variables later in the code.
typedef struct
{
int x;
int y;
} point;
point p;
|
A struct can contain elements that have the same type as the struct.
struct node
{
int data;
node* next;
};
|
A struct that contains elements of the type of the struct
must use a tag.
typedef struct node
{
int data;
struct node* next;
} node;
|
Arguments to functions can be passed by reference or by value.
int i;
int j;
swap( i, j );
...
void swap( int& x, int& y )
{
int temp = x;
x = y;
y = temp;
}
|
Arguments to functions can be passed only by value. (So
you have to use pointers.)
int i;
int j;
swap( &i, &j );
...
void swap( int* xp, int* yp )
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
|
void* must be cast if it is to be assigned to another pointer type
void* func( void );
...
char* s = (char*) func();
|
void* is compatible with any pointer type
void* func( void );
...
char* s = func();
|