Thursday, June 19, 2014

Using namespace std

If you have seen C++ code before, you may have seen cout being used instead of std::cout. Both name the same object: the first one uses its unqualified name (cout), while the second qualifies it directly within the namespace std (as std::cout).

cout is part of the standard library, and all the elements in the standard C++ library are declared within what is a called a namespace: the namespace std.

In order to refer to the elements in the std namespace a program shall either qualify each and every use of elements of the library (as we have done by prefixing cout with std::), or introduce visibility of its components. The most typical way to introduce visibility of these components is by means of using declarations:

using namespace std;


The above declaration allows all elements in the std namespace to be accessed in an unqualified manner (without the std:: prefix).
With this in mind, the last example can be rewritten to make unqualified uses of cout as:

Comments

Comments do not affect the operation of the program; however, they provide an important tool to document directly within the source code what the program does and how it operates.
and they're ignored from compiler but makes source code to read easier from you and others and to know that program will do.

in C++ are two ways to make comments
First
// Line Coment
// Line 2 Comment
// Line 3 Comment

Second
/*Block Comments
Line 2

Line 3*/
Lets add some comments into our program

/* myprogram in C++
   with more comments */
#include <iostream>
int main ()
{
  std::cout << "Hello World! ";     // prints Hello World!
  std::cout << "I'm a C++ program"; // prints I'm a C++ program
} 
If comments are included within the source code of a program without using the comment characters combinations //, /* or */,
 the compiler takes them as if they were C++ expressions, most likely 
causing the compilation to fail with one, or several, error messages. 

Structure of a C++ Program

// my first program in C++
#include <iostream>

int main()
{
  std::cout << "Hello World!";
}

 

Line 1: // my first program in C++
Two slash signs indicate that the rest of the line is a comment inserted by the programmer but which has no effect on the behavior of the program. Programmers use them to include short explanations or observations concerning the code or program. In this case, it is a brief introductory description of the program.

Line 2: #include <iostream>
Lines beginning with a hash sign (#) are directives read and interpreted by what is known as the preprocessor. They are special lines interpreted before the compilation of the program itself begins. In this case, the directive #include <iostream>, instructs the preprocessor to include a section of standard C++ code, known as header iostream, that allows to perform standard input and output operations, such as writing the output of this program (Hello World) to the screen.

Line 3: A blank line.
Blank lines have no effect on a program. They simply improve readability of the code.

Line 4: int main ()
This line initiates the declaration of a function. Essentially, a function is a group of code statements which are given a name: in this case, this gives the name "main" to the group of code statements that follow. Functions will be discussed in detail in a later chapter, but essentially, their definition is introduced with a succession of a type (int), a name (main) and a pair of parentheses (()), optionally including parameters.

The function named main is a special function in all C++ programs; it is the function called when the program is run. The execution of all C++ programs begins with the main function, regardless of where the function is actually located within the code.

Lines 5 and 7: { and }
The open brace ({) at line 5 indicates the beginning of main's function definition, and the closing brace (}) at line 7, indicates its end. Everything between these braces is the function's body that defines what happens when main is called. All functions use braces to indicate the beginning and end of their definitions.

Line 6: std::cout << "Hello World!";
This line is a C++ statement. A statement is an expression that can actually produce some effect. It is the meat of a program, specifying its actual behavior. Statements are executed in the same order that they appear within a function's body.

This statement has three parts: First, std::cout, which identifies the standard character output device (usually, this is the computer screen). Second, the insertion operator (<<), which indicates that what follows is inserted into std::cout. Finally, a sentence within quotes ("Hello world!"), is the content inserted into the standard output.

Notice that the statement ends with a semicolon (;). This character marks the end of the statement, just as the period ends a sentence in English. All C++ statements must end with a semicolon character. One of the most common syntax errors in C++ is forgetting to end a statement with a semicolon.
 
#include <iostream>

int main()
{
  std::cout << "Hello World!";
} 
is same as 
 
#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;

    return 0;
}

First Program in C++

After installing Code:blocks
Create a Project and select Console Application. (File>New>Project)

use C++ , type a project name  and
open "main.cpp" in left side




in Build menu click Build and Run





now a Console Application will appear like this :


Follow us in next tutorials to know what are these lines in souce code and what they do .

What are compilers , linkers ?

Compiler is a program (software) that transforms source code* written in a programming language into a executable program.


Compiler translates source code from High level Programming Languages  (H.L.P.L) {C++, C, C#, Java} into a Low Level Programming Languages (L.L.P.L) {Machine Code or Assembly Language}
Why we writte in H.L.P.L ?
-Because we are humans and they're easy to read.
Why Compiler transforms it to L.L.P.L ?
-Because CPU reads only low level programming language as instructions.
------
This Summer we're gonna learn C++ together .
You can download any c++ compilers but i recommend  you this >
Code:Blocks cause its easy to use

What are Programming Languages ?

A programming language is a formal constructed language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs to control the behavior of a machine or to express algorithms.
The earliest programming languages preceded the invention of the computer, and were used to direct the behavior of machines such as Jacquard looms and player pianos.[1] Thousands of different programming languages have been created, mainly in the computer field, and many more still are being created every year. Many programming languages require computation to be specified in an imperative form (i.e., as a sequence of operations to perform), while other languages utilize other forms of program specification such as the declarative form (i.e. the desired result is specified, not how to achieve it).
The description of a programming language is usually split into the two components of syntax (form) and semantics (meaning). Some languages are defined by a specification document (for example, the C programming language is specified by an ISO Standard),
while other languages (such as Perl) have a dominant implementation that is treated as a reference.
(from wikipedia)
Programming languages divided in High Level Programming languages and Low Level P.L.
H.L.P.L are c++ , c , c# , java , perl,python etc.
L.L.P.L are is assembly language.
Read More about High and Low Level programming languages.
Here.