Monday, March 22, 2021

Using namespace std;

 Understanding "Using namespace std;

namespace is a declarative region that provide scope to identifiers and is used to avoid the name collision ensuring uniqueness of names.

Let's take real life example to understand namespace

suppose we have two friends
friend 1 ava
friend 2 ava

both have the same name. so the real question is how to call friend 1 and how to access friend one if both have the same name.
so we use some additional information about friend 1 and friend 2 to avoid the confusion . 
we can use something like the place they belong or address to access and call them. And this will help in overcoming the difficulties of name collision.

similarly in C++ there can be methods , functions, variables and others having same name and they can be included in some standard or user defined files . so whenever they are used computer will try to access the point to get the code  and include that in our program for successful compilation.
so this namespace tells the computer where to look for that particular method to be included in our program. 

SO WE CAN SAY THIS NAMESPACE HAVE THE ADDRESS OF ALL THE METHODS DEFINED IN DIFFERENT FILES THAT IS INCLUDED AND THIS NAMESPACE TELLS THE COMPUTER WHERE TO LOOKS FOR THAT METHOD TO AVOID CONFUSION.

why namespace std in C++ ?
we include a header file #include<iostream>
in our C++ program and inside main function
we use method like cout cin endl etc...
so iostream contains all these method to be
used in a program . so when it  is used computer 
will look for these method to include in 
our program. So if namespace std; is not used
and there maybe more than one header file so computer will
not find where to look for that exactly and it will through error.
and if we have used namespace std;
it will tell the computer to look for method like cout ,cin...
in iostream and it will work.

Understanding with pictures




       Author   
   Md wasif



Join the CodifiedYOUTH community to learn free skills and get free internships and lot more
Join on Whatsapp
Join on Linkedin
visit CodifiedYOUTH website for more detail

Friday, March 19, 2021

Preprocessor directives in C++

 Understanding the meaning and use of Preprocessor directives in C++

The example of the very basic C++ program
 can be a hello world program...

           fig 1             

The program started with #(hash) include and this contains the iostream(this is standard input output stream). This #include is called "preprocessor directive" , there can be many preprocessor directives in a program and that is added first in the program. other examples of preprocessor directives are #define, #ifndef  etc.

-) what is the use of  #include  ?
This #include is the way of including the standard and user defined 
files in the program that is from the outside sources and these standard and user defined files contains pieces of codes that can perform certain tasks. And this "#" (hash) has a role to provide a path between these standard or user defined files and preprocessors. And "include" adds the extra code to the program while the program preprocessed before compilation.


       Understanding with example in  fig-1   

In fig-1, there is a header file #include<iostream> the iostream is a standard file that contains methods that performs certain function whenever called. This iostream contains method like cout , cin, etc. , so in the example in fig-1 cout<<"hello world!"; is  there inside the main function so when the program is to be executed , it first looks for if there is preprocessor directives before compiling the program. If preprocessor directive is found then first program is preprocessed and then it is compiled. so #  provide the preprocessor the path to preprocess the files included and this "include" will include the code for cout method that is defined in standard input output stream.                                          

case if there is no preprocessor directive 

when there is no preprocessor directive in the program , no such method or function can be defined and used inside the main function that is included in some standard files or user defined files. such program can look like this...
                                                            
        
following program doesn't contain any preprocessor directive ,so no such method that is defined inside that file will be used inside the main function. This program will get compiled and there will be no preprocessing as there is no such preprocessor directives present.
           

                                                                              




       Author   
   Md wasif



Join the CodifiedYOUTH community to learn free skills and get free internships and lot more
Join on Whatsapp
Join on Linkedin
visit CodifiedYOUTH website for more detail

Using namespace std;

  Understanding "Using namespace std; namespace is a declarative region that provide scope to identifiers and is used to avoid the name...