Laboratorio Di Tecnologie Dell'informazione: Ing. Marco Bertini Bertini@dsi - Unifi.it
Laboratorio Di Tecnologie Dell'informazione: Ing. Marco Bertini Bertini@dsi - Unifi.it
Laboratorio Di Tecnologie Dell'informazione: Ing. Marco Bertini Bertini@dsi - Unifi.it
dell'Informazione
Ing. Marco Bertini
[email protected]
http://www.dsi.unifi.it/~bertini/
“Hello world”: a review
Some differences between C
and C++
• Let’s review some differences between C and
C++ looking at the “Hello world” program
#include <iostream>
int main() {
// let’s greet
std::cout << "Hello, ";
string name = "world\n";
std::cout << name;
}
The main function
• In C++ we can declare variable anywhere, but always before they are
used
• A control variable for a for loop is often declared inside the for
loop, e.g.
for (int i = 0; i < 100; i++) {
Default function arguments
• In C++ it is possible to provide “default arguments” when
defining a function. These arguments are supplied by the
compiler when they are not specified by the programmer.
Example:
• // sample header file
extern void three_ints(int a, int b=4,
int c=3);
or:
extern "C"{
// C-declarations go in here
void *xmalloc(int size);
}
or:
extern "C"{
}
Using C functions in C++ - cont.
// This is xmalloc.h
#ifdef __cplusplus
extern "C" {
#endif
/* declaration of C-data and functions are
inserted here. */
void *xmalloc(int size);
#ifdef __cplusplus
}
#endif
Credits