You are on page 1of 4

1.

#include <iostream>
2. using namespace std;
3. int main()
4. {
5. cout<<"Hello World!";
6. return 0;
7. }
The program prints Hello World! in the output screen.
What is #include <iostream>?

This statement includes the header file into the application so that you are able to use the operations included in them.
Also, you can create your own header files and include them in your program using the #include.

What is iostream?
iostream is what you call the header file. It is a standard C++ input/output library file.
It comes packaged with the compiler/IDE and contain mechanisms to get the information from the user and print same
or added information to a file, screen or any other media.
What is #include?
The #include iostream file, into the program. This ensures that now you’re able to use the
operations, iostream operations (like: taking input from user, displaying output on the screen), in the program.

Method Description

C++ cerr writes to error stream cerr is the standard error stream which is used to output the
errors. It is used when we need to display the error message
immediately.

C++ cin accepts input from user

C++ clog used for streaming logs This is used to display errors but unlike cerr the error is first
inserted into a buffer and is stored in the buffer until it is
not fully filled.

C++ cout displays output to output device


i.e monitor

C++ wcerr prints to error stream as wide


character type

C++ wcin accepts input in wide character


type

C++ wclog writes to log stream with wide


character

C++ wcout displays wide characters


(Unicode) to screen

What is using namespace std;”?

The statement is intuitive in itself, you are “using” the “namespace” “std” in your file.
We use the namespace std to make it easier to reference operations included in that namespace.
If we hadn’t used the namespace, we’d have written std::cout instead of cout. This tells the compiler that every cout is
actually std::cout.

What’s a namespace?

It’s a region where your code resides. It limits or expands the scope of your code to one or more files.

Why do you use namespace?

Like two persons can have the same name, variables and functions in C++ can have same names as well. The use of
namespace is to avoid the confusion of which variables/functions you are referencing to.

What is std?

std is a standard namespace used in C++.

Escape Description
sequence
\n Newline. Position the screen cursor to the beginning of the next line.
\t Horizontal tab. Move the screen cursor to the next tab stop.
Carriage return. Position the screen cursor to the beginning of the current
\r
line; do not advance to the next line.
\a Alert. Sound the system bell.
\\ Backslash. Used to print a backslash character.
\' Single quote. Use to print a single quote character.
\" Double quote. Used to print a double quote character.

Cascading of I/O Operators


We have used the insertion operator << repeatedly in the last two statements for printing results. The statement
Cout << “Sum = “ << sum << “\n”;
First sends the string “Sum = “ to cout and then sends the value of sum. Finally, it sends the newline character so that
the next output will be in the new line. The multiple use of << in one statement is called cascading. When cascading an
output operator, we should ensure necessary blank spaces between different items. Using the cascading technique, the
last two statements can be combined as follows:
Cout << “Sum = “ << sum << “\n”<< “Average = “ << average << “\n”;
This is one statement but provides two line of output. If you want only one line of output, the statement will be:
Cout << “Sum = “ << sum << “,”<< “Average = “ << average << “\n”;
The output will be:
Sum = 14, average = 7
We can also cascade input iperator >> as shown below:
Cin >> number1 >> number2;
The setw Manipulator
You can think of each value displayed by cout as occupying a field: an imaginary box with a certain width. The default
field is just wide enough to hold the value. That is, the integer 567 will occupy a field three characters wide, and the
string “pajamas” will occupy a field seven characters wide. However, in certain situations this may not lead to optimal
results. Here’s an example. The WIDTH1 program prints the names of three cities in one column, and their populations
in another.
// width1.cpp
// demonstrates need for setw manipulator
#include <iostream>
using namespace std;
int main()
{
long pop1=2425785, pop2=47, pop3=9761;
cout << “LOCATION “ << “POP.” << endl<< “Portcity “ << pop1 << endl<< “Hightown “ << pop2 << endl
<< “Lowville “ << pop3 << endl;
return 0; }
Here’s the output from this program:
LOCATION POP.
Portcity 2425785
Hightown 47
Lowville 9761
Unfortunately, this format makes it hard to compare the numbers; it would be better if they lined up to the right. Also,
we had to insert spaces into the names of the cities to separate them from the numbers. This is an inconvenience.
// width2.cpp
// demonstrates setw manipulator
#include <iostream>
#include <iomanip> // for setw
using namespace std;
int main() {
long pop1=2425785, pop2=47, pop3=9761;
cout << setw(8) << “LOCATION” << setw(12)<< “POPULATION” << endl<< setw(8) << “Portcity” << setw(12)
<< pop1 << endl<< setw(8) << “Hightown” << setw(12) << pop2 << endl<< setw(8) << “Lowville” << setw(12) <<
pop3 << endl;
return 0; }
The setw manipulator causes the number (or string) that follows it in the stream to be printed within a field n characters
wide, where n is the argument to setw(n). The value is right justified within the field.
Here’s the output of WIDTH2:
LOCATION POPULATION
Portcity 2425785
Hightown 47
Lowville 9761
#include<iomanip>
Below are the Parametric manipulators −
Setbase: It is used to set basefield flag. Ex. setbase (int base); std::cout << std::setbase(16);
Setfill: It is used to set fill character. Ex. std::cout << std::setfill ('x')
Setprecision: It is used to set decimal precision. Ex. setprecision (int n); std::cout << std::setprecision(5)
Setw: It is used to set field width.
get_money: It is used to get monetary value.
put_money: It is used to put monetary value.
get_time: It is used to get date and time. Ex. std::cin >> std::get_time(&when,"%R");
put_time: It is used to put date and time.
Setiosflags: It is used to Set format flags.
Resetiosflags: It reset format flags.
#include<cmath>
A set of functions to perform mathematical operations
Method Description
C++ abs(): returns absolute value of an argument
C++ acos(): Returns Inverse cosine a Number
C++ acosh(): returns hyperbolic cosine of a number
C++ asin(): Returns Inverse Sine a Number
C++ asinh(): returns arc hyperbolic sine of a number
C++ atan(): Returns Inverse tangent a Number
C++ atan2(): Returns Inverse Tangent of a Coordinate
C++ atanh(): returns arc hyperbolic tangent of a number
C++ cbrt(): Computes Cube Root of a Number
C++ ceil(): Return ceiling value of number
C++ copysin(): returns num with value of first and sign of second
C++ cos(): Returns Cosine of the Argument
C++ cosh(): Returns Hyperbolic Cosine of an Angle
C++ exp(): returns exponential (e) raised to a number
C++ exp2(): Returns 2 raised to a Number
C++ expm1(): Returns e raised to Power Minus 1
C++ fabs(): returns absolute value of argument
C++ fdim(): Returns Positive Different Between Arguments
C++ floor(): Returns floor value of decimal number
C++ fma(): Returns Fused Multiply–Accumulate
C++ fmax(): returns largest among two arguments passed
C++ fmin(): returns smallest among two given arguments
C++ fmod():Computes floating point remainder of division
C++ frexp():breaks float to its binary significand
C++ hypot():Returns Square Root of sum of square of Arguments
C++ ilogb():returns integral part of logarithm of |x|
C++ ldexp():returns product of x and 2 raised to the power e
C++ llrint():Rounds argument using current rounding mode
C++ llround(): Rounds argument to nearest long long int value
C++ log():Returns Natural Logarithm of a Number
C++ log10():Returns Base 10 Logarithm of a Number
C++ log1p():returns natural logarithm of x+1.
C++ log2():returns base2 logarithm of a number
C++ logb():returns logarithm of |x|
C++ lrint():Rounds argument using current rounding mode
C++ lround():Returns the long int value nearest to the argument
C++ modf():Breaks Number Into Integral and Fractional Part
C++ nan():returns a quiet NaN value
C++ nearbyint():Rounds argument to using current rounding mode
C++ nextafter():returns next value after x in direction of y
C++ nexttoward():returns next value after x in direction of y
C++ pow():Computes Power a Number
C++ remainder():Returns remainder of x/y
C++ remquo():Computer remainder and stores quotient of x/y
C++ rint(): Rounds argument using current rounding mode
C++ round(): Returns integral value nearest to argument
C++ scalbln(): Scales x by FLT_RADIX to the power n
C++ scalbn(): Scales x by FLT_RADIX to the power n
C++ sin(): Returns Sine of the Argument
C++ sinh(): returns hyperbolic sine of an angle
C++ sqrt(): Computes Square Root of A Number
C++ tan(): Returns Tangent of the Argument
C++ tanh(): returns hyperbolic tangent of an angle
C++ trunc(): Truncates the decimal part of a number

You might also like