SAMPLE PROGRAM FOR BEGINNERS

// sample program

#include <iostream>

using namespace std; 
int main () 

 cout << "This is my first program"; 
return 0; 


// sample program


This is a comment line. All lines starting with slash signs (//) are  comments and don't 
have any impact on the behavior of the program. The computer programmer will use them to incorporate short explanations or observations among the ASCII text file itself. In the above Example the line may be a temporary description of  what our program is or what is it all about.This is used in very large programs in order to avoid any confusion.


#include <iostream>


Lines starting with a hash sign (#) are directives for the preprocessor. They're not regular code lines 
with expressions however indications for the compiler's preprocessor.In the above example, the directive #include  tells the preprocessor to incorporate the iostream customary file. This specific file (iostream) includes the declarations of the essential customary input-output library in C++, & it is enclosed because its  functionality is going to be used later within the program.Various other standard library header files are as followed:


Utilities library

<cstdlib>               General purpose utilities: program control, dynamic memory allocation                                                    
<csignal>              Functions and macro constants for signal management
<csetjmp>              Macro (and function) that saves (and jumps) to an execution context
<cstdarg>              Handling of variable length argument lists
<typeinfo>              Runtime type information utilities
<typeindex>           std::type_index
<type_traits>       Compile-time type information
<bitset>              std::bitset class template
<functional>      Function objects, designed for use with the standard algorithms
<utility>              Various utility components
<ctime>              C-style time/date utilites
<chrono>               C++ time utilites
<cstddef>              typedefs for types such as size_t, NULL and others
<initializer_list>       std::initializer_list class template
<tuple>)              std::tuple class template


using namespace std; 


All the elements of the quality C++ library are declared inside what's known as a namespace, the 
namespace with the name std.This line is frequent in C++ programs that use the  library functions, 
and actually it'll be enclosed in most of the source codes.
Namespaces in C++ are used to define a scope and allows us to group global classes, functions and/or objects into one group.

When you use using namespace std; you are instructing C++ compiler to use the standard C++ library. If you don't give this instruction, then you will have to use std::, each time you use a standard C++ function/entity.

int main () 


This line corresponds to the beginning of the definition of function operate. The main function is with which all C++ programs begin their execution, severally of its location among the ASCII text file. It 
does not matter whether or not there is different functions with different names outlined before or when it - the instructions contained among this function's definition can continuously be the primary ones to execute in any  C++ program. For that very same reason, it is essential that each one C++ programs have a main operate
The word main is followed within the code by a combine of parentheses (()).That is because it is a function  declaration:  In C++, what differentiates a main  declaration from different kinds of expressions are these  parentheses that follow its name. Optionally, these parentheses might enclose an inventory of parameters among  them. Right after these parentheses you can find the body of the main function enclosed in braces {}. What is  contained within these braces is what the function does when it is executed.


cout << "This is my first program"; 


This line is a C++ statement. A statement may be a straightforward or compound expression which will actually turn out some result. In fact, this statement performs the sole action that generates an understandable result in our initial program. 
cout represents the quality output stream in C++,  which means  to insert a sequence of characters (in this case the 'This is my first' program sequence of characters) in to the quality output stream (which is usually the output screen). 
Notice that the statement ends with a punctuation mark character (;). This character is employed to mark the end of the statement &  it should be enclosed at the end of all expression statements.


return 0;


The return statement causes the main function to finish. return may be followed by a return code (in our 
example is followed by the return code 0). A return code of 0 for the main function is generally interpreted 
as the program worked as expected without any errors during its execution. This is the most usual way to 
end a C++ console program.



0 comments to "Basics of c++"

Post a Comment

Powered by Blogger.