Header Files in C++ Header files hold definitions of Functions and Variables, which is trade in or usedinto any C++ program by using the pre-processor #include statement. Header file have an extension “.h” which comprises C++ function declaration also macro definition.
Each header file holds information for a precise group of functions. the stdio.h header file contains declarations of standard input and output functions obtainable in C++ which is used for acquire the input and print the output. Likewise, the header file math.h covers declarations of mathematical functions existing in C++.
Need of header files
if we want to use any function in our C++ program then fwe need to ingress their definition from C++ library,.in that case,we need to contain header file in program usingusing #include. Header file take in at the top of any C++ program.
Here, print message on screen hello world! by using cout but we don’t describe cout here in fact already cout has been stated in a header file named iostream.
- h>- input output stream header file
- itOffers functionality to use an abstraction termed streams specifically designed to do input and output operations on series of character, similar to files or strings.
- The Objects cin, cout are from iostream header.
<conio.h> console input output header file
h is a C header file used in old MS-DOS compilers to generate text user interfaces
- Both are header files which compriseslibery of numerous functions.
- for e.g.clearscr() ,getch() are from conio.h
- If we use these function them without introducing header files ,at times compiler throws error.
- C++ offers a new way to perform the input and output operations named iostream method.
- The I/O stream is nothing but aorder of following characters written for the screen display or else read from the keyboard.
- theseoperations in C++ are performed by using the I/O stream as cin for input and cout for output.
- The Borland C++ uses iostream.h for most stream associated operations.
|C++ compilers||Header Files To Be Contained within|
Microsoft Visual C++
UNIX C++ (AT&T)
|iostream .h, ios .h, iomanip . h
iostream .h, ios .h, iomanip . h
ios .h, istream . h, ostream . h and streamb . h
stream . h
stream . h
- h The ‘ios’ relate to input and output streams . C++ lets three types of stream classes, specifically,
- Istream Theistreaminvolves of input functions to recite a streams of characters from the keyboard.
- Ostream Theostreamcontains of output functions to inscribe a character onto the screen.
- 1Ostream the iostreammaintenances both input/output stream of functions to read a stream of characters form the keyboard .it displays a stream of objects onto the video screen.
- The iostream (input/output stream) class is derived from both istream and ostream.
The standard input stream (cin)
Standard input stream
- It is Object of classistream that signifies the standard input stream concerned with to narrow characters. It resembles to the C stream stdin.
- Thestandard input stream is a source of characters . It is mostlyexpected to be input from an external source, like the keyboard or else a file.
- As per an object of classistream, characters can be improved either as formed data using the extraction operator .also as unformatted data, using member functions like
- The object is stated in header<iostream> with external linkage also static duration: it lasts the complete duration of the program.
- In expressions ofstatic initialization order, cin is definite to be suitably constructed and altered no later than the first time an object of type ios_base::Init is made.
- the standard input device is the keyboard. With the cin and >> operators it is feasible to recite input from the keyboard.
The cin operator will always return the variable type that you usage with cin. Thus if you demand an integer you will get an integer.
The cin operator is likewise chainable.
cin>> XX >> YY;
here, the user must give two input values, that are parted by any useable blank separator like tab, space or else new-line.
The standard output stream (cout)
Standard Output (cout)
the standard output of a program points at the display. using the cout operator and the insertion operator (<<) you can pattern a message onto the screen. example:
To motif the content of a variable the double quotes are not used.
The << operator know how to be used numerous times in a single statement.
It is conceivable to combine variables and text:
if you need to print two sentences you will have to use the new-line character i.e.( \n ).
cout<< "This is one sentence.\n"; cout<< "This is another.\n";
It is probable to use the endl manipulator as a replacement for of the new-line character.
cout<< "This is one sentence." <<endl; cout<< "This is another." <<endl;
The endl manipulator will place a new-line character, so the result is the same.
The standard error stream (cerr)
- most systems have their standard error output fixed to the console, where text messages are shown, even though this can usually be pass on.
- As cerr is an object of class ostream, it is possible to write characters to it either as configured data using for example the insertion operator (ostream::operator<<) or else as unformatted data using the writemember function, among others.
- It resembles to the C stream stderr . The standard error stream is a purpose of characters resoluted by the environment.
The standard log stream (clog)
- Object of class ostreamthat signifies the standard logging stream oriented to narrow characters (of type char). It matches, along with cerr, to the C stream stderr.
- The standard logging streamis a determination of characters invincible by the background.
- being an object of class ostream, characters can be written to it either as arranged data using the insertion operator (operator<<) or else as unformatted data, using member functions such as write.
- The object is stated in header <iostream>with external linkage and static duration: it continues the total duration of the program.
- The clog is sure to be properly constructed and reset no later than the first time an object of type ios_base::Initis constructed.actuallyclog is not tied to any other stream.
- The clog is an instance of ostream class.
The clog is also used in combining with the stream insertion operator as like this.
int main( )
char str = “Unable to read….”;
clog << “Error message : ” <<str<<endl;
Error message : Unable to read….
here, this is good practice to show error messages using cerr stream and while exposing other log messages then clog would be used.
- It is for declaring numerousiostreams manipulators takingan argument <iomanip>
- It Offers facilities to operate output formatting, such as the base used when formatting integers and the precision of floating point values.
- it declares the C++ streams I/O manipulators .also,itholds macros for makingparameterised manipulators. As like this :
Date Time in C++
there are so many ways to acquire the date and time that people can simply get confused.
As always, there are a few jargons that you may have to recognize about,
UTC (Coordinated Universal Time) : This is the standard international time or else the Greenwich Mean Time.
epoch : number of seconds elapsed ever since 00:00:00 on January 1, 1970, Coordinated Universal Time.
1. ASNI Standard Library
- the CPU connected functions also types as signified by clock_t and clock() functions.
Whereas,the calendar time represented by nearly everything else in that header file.
2. Microsoft implementations
Over years Microsoft has more its own version of date and time functions
- With the Win32 API, Microsoft did not deliver any direct functions to compute time differences or else provide ability to compare time values other than file times.
- one can use theSYSTEMTIME structure, together with the GetSystemTime function to regain the current system time in UTC format.
- In the Win32 date and time world, perhaps one of the more useful functions that you will run into is theLocalFileTimeToFileTime function to change a local time to a UTC time format .
MFC datetime classes
- The MFC framework shortened working with times significantly by introducing two wrapper classes, theCTimeand COleDatetime
- ACTime object characterizes an absolute time and date, based on synchronized universal time (UTC).
- TheCTimeSpan class is used in combination with CTime to do subtractions and additions. It denotes a relative time span as the name puts forward and delivers four constructors.
- COleDateTime:TheCOleDateTime class can grasp any date from January 1, 100 to December 31, 9999, but the reason for its being, as its name may propose, is to be deal Automation support.
- COleDateTimeSpan:Theconjured companion class used in conjunction withCOleDateTime, is COleDateTimeSpan. And it carries more if not less the same functionality that CTimeSpan goes for CTime.
Additional useful header files
Here is a list of usually used C++ headers.
· cassert (assert.h)
This library holds only the function assert. You useassert(assertion);
tocheck the validity of an assertion. If assertion is false, assert inscribes an error message and ends program execution.
- cctype (ctype.h)
Maximum functions in this library categorize a given ASCII character as a letter, a digit, and so on. Two other functions change letters among uppercase and lowercase.
It Describes named constants that identify the range of floating-point values.
it Describes named constants that state the range of integer values.
The C++ functions in this library calculate certain standard mathematical functions. These functions are bogged down to put up float, double, and long double.
abortDismisses program execution abnormally
absYields the absolute value of an integer
atofChanges a string argument to floating point
atoiAlters a string argument to an integer
exitEnds program execution
rand() Makes an unsigned int between 0 and RAND_MAX, a named constant defined in cstdlib header file
srand(unsigned n) Seeds the rand() function so that it produces different
sequences of random numbers. srand is frequently used in conjunctionwith the time function from the ctime library.
This library permits you to operate C strings that end in the char ‘\0’, the null char. these functions return a pointer to the ensuing string in addition to familiarizing an appropriate argument. The argument ch is a character, n is an integer, and the other arguments are strings, which typically means they are names of a char array, but can be string constants in some cases.
Defines functions for operating time and dates.
Defines classes, types, and functions that narrate to exception handling.
Declares the C++ classes that support file I/O.
The manipulation in this library mark the format of steam operations.
The manipulators in this library mark the format of stream operations.
This library allows you to manipulate C++ strings. In addition, you can use the operators with C++ strings: =, +, ==, !=, <, <=, >, >=, <<, also>>. Note that places within a string initiate at 0.