#include <iostream.h>
void main() {
cout << "hello\n";
}
The newer C++ standard library header files have their definitions in a namespace called «std». To use the types from these headers you need to prefix them with std:: or use a using namespace std; statement.
Example 1 -using explicit std:: prefix.
void main() {
std::cout << "hello\n";
std::cout << "world\n";
}
Example 2 -using namespace statement
#include <iostream>
using namespace std;
void main() {
cout << "hello\n";
cout << "world\n";
}
//myClassFile.h
class MyClass{
};
//test file
#include <iostream.h>
#include <string.h>
#include "myClassFile.h"
void main() {
...
}
//myClassFile.h
class MyClass{
};
//test file
#include <iostream>
#include <cmath>
using namespace std; //put this after including c++ standard libraries but before including your own header files
#include "myClassFile.h"
void main() {
...
}
Here are some common C++ standard libraries and their older .h counterpart. Originally native C libraries were used with C++ with names that end in .h. Later C++ libraries were added that were more «standard» among compiler manufacturers. These libraries like <iostream> and <string> did not use the .h extension. The .h libraries are now considered deprecated, but because there is so much legacy code out there that uses them, one must still be aware of them, and be prepared to use them. Alos new C++ versions of the C libraries are also used and they add a «c» prefix instead of the .h extension. For example <cmath> in place of <math.h>. This can all be a bit confusing. The non-dot-h libraries and the c-prefix libraries are placed in a namespace called «std». The .h libraries are not in a namespace. When using the C++ (i.e. non-dot-h) libraries you will often see a using namespace std; statement after the headers are included with the #include directive.
| newer | older | use |
|---|---|---|
| iostream | iostream.h | input and output to console, keyboard, files |
| cmath | math.h | math functions like pow(), sin(), cos(), sqrt() |
| cstring | string.h | string copy, comparison etc. strcpy() strcmp(), strcat(), strlen() |
| string | NOT TO BE CONFUSED WITH cstring. This defines a string object | |
| cstddef | stddef.h | Includes definitions including the symbolic constant: NULL (a system depdenent null pointer -typically 0) |
| cctype | ctype.h | return results for characters, like whether they are digits, or printable etc. e.g.: isdigit(ch), ispunct(ch), isprint(ch) |