Header files Mark Hennessy Dept Computer Science NUI
- Slides: 8
Header files Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th – 22 nd September 2006
C++ Source Code Model l Reusable library code and user defined functions are maintained in header files, typically labelled “file. h”. l We use #include to import them into our programs. l #include <iostream> l #include “file. h”
What’s the difference between <iostream. h> and <iostream> l l l <iostream> is part of the ANSI standard C++ library <iostream. h> is an artifact of pre-standard C++ However, vendors do not want old code to break; thus, <iostream. h> will likely continue to be supported. We will not be using old headers!!! C++ is standardised so no more non-compliant code!
What’s the difference between string. h and string l similar to iostream. h and iostream l <string. h> -- the old C-strings (char *) l <string> -- C++ strings and C-strings l To use the string library: #include <string> std: : string my. String = “hello”;
What’s the algorithm for old/new old headers are likely to continue to be supported, even though they’re not in the ANSI standard l new C++ headers have same name w/out the. h, but contents are in std l C headers, like <stdio. h> continue, but not in std l new C++ headers for functionality in old C library have names like <cstdio>; offer same content but are in std l
Preprocessor facilities l Conditional l compilation a useful way to handle multiple include files #ifndef SOME___HEADER___FILE #include “SOME___HEADER___FILE” #endif
Command line parameters main(int argc, char * argv[]) argc is the number of parameters argv is an array of char* with argv[0] the first parameter argv[1] the second paramter etc. Note that there is always at least one parameter
Command line parameters Integer parameters must be converted: #include <cstdlib> main(int argc, char * argv[]) { if (argc < 2) { std: : cout << “usage: “ << argv[0] << “ <number>” << std: : endl; return 1; } int n = std: : atoi(argv[1]);