C++ code begins with the inclusion of header files . There are many header files available in the C++ programming language which will be discussed while moving ahead with the DSA Bootcamp by Sunyul Hossen (Link)
Header files :-
The names of program elements such as variables, functions, classes, and so on must be declared before they can be used. For example, you can’t just write x = 25 without first declaring variable ‘x’ as: int x = 25 ;
The declaration tells the compiler whether the element is an int, a double, a float, a char , a function or a class. Similarly, header files allow us to put declarations in one location and then import them wherever we need them. This can save a lot of typing in multi-file programs. To declare a header file, we use ‘#include’ directive in every cpp file.
Some bad practices while putting header files into the code :-
- Built-in-type definitions at namespace or global scope
- Non-inline function definitions
- Unnamed namespaces
- Non-constant variable definitions
- Using directives
- Aggregate definitions
So, the code is :-
#include <iostream>
using namespace std;
iostream means Input/Output stream, means this header file is necessary if you want to take input through the user or want to print the output.
~ iostream , namespace & semicolon :
“iostream” contains the definitions for the functions:
1) cin : used to take user input // cin >>
2) cout : used to print the output // cout <<
‘namespace’ defines which input/output form is to be used. You will understand these better as you progress in the course.
‘semicolon’ (;) is used for terminating a C++ statement. means different statements in a C++ program are separated by semicolon
~ main() function:
#include <iostream>
using namespace std;
// may have some code/function
int main() {
// code
// Statement 1;
// Statement 2;
cin >> n ; // cin : input taker
cout << n+2 ; // cout : output printer
// other stuffs
}
in this statement int main () : “int” is a return type function , which return integer value & “main()” is that portion, inside which all the commands are written and gets executed & “{ }” is known as scope of a particular function, all the code written inside this curly braces is said to be one block.