Learn Basics of C++

#include <iostream>
Using namespace std;
int main()
{
int number1;
cin >> “Enter number1”;
cout << number1;
cout << “c++ is better than c n”;
return 0;
}

 

Program feature

Like C, the C++ program is a collection of function. The above example contain only one function main(). As usual execution begins at main(). Every C++ program must have a main(). C++ is a free form language. With a few exception, the compiler ignore carriage return and white spaces. Like C, the C++ statements terminate with semicolons.

Comments

C++ introduces a new comment symbol // (double slash). Comment start with a double slash symbol and terminate at the end of the line. A comment may start anywhere in the line, and whatever follows till the end of the line is ignored. Note that there is no closing symbol.
The double slash comment is basically a single line comment. Multiline comments can be written as follows:

// This is an example of
// C++ program to illustrate
// some of its features

 

The C comment symbols /* */ are still valid and are more suitable for multiline comments. The following comment is allowed:

/* This is an example of
C++ program to illustrate
some of its features
*/

 

The iostream File

We have used the following #include directive in the program:

#include <iostream>

 

The #include directive instructs the compiler to include the contents of the file enclosed within angular brackets into the source file. The header file iostream.h should be included at the beginning of all programs that use input/output statements.

Namespace

Namespace is a new concept introduced by the ANSI C++ standards committee. This defines a scope for the identifiers that are used in a program. For using the identifier defined in the namespace scope we must include the using directive, like

Using namespace std;

 

Here, std is the namespace where ANSI C++ standard class libraries are defined. All ANSI C++ programs must include this directive. This will bring all the identifiers defined in std to the current global scope. Using and namespace are the new keyword of C++.

Return Type of main()

In C++, main () returns an integer value to the operating system. Therefore, every main () in C++ should end with a return (0) statement; otherwise a warning an error might occur. Since main () returns an integer type for main () is explicitly specified as int. Note that the default return type for all function in C++ is int.

The following main without type and return will run with a warning:

main ()
{
…………..
………….
}

 

Variables

The program uses one variable number1. They are declared as type int by the statement.

int number1;

 

All variable must be declared before they are used in the program.

Input Operator

cin >> number1;

 

Is an input statement and causes the program to wait for the user to type in a number. The number keyed in is placed in the variable number1. The identifier cin (pronounced ‘C in’) is a predefined object in C++ that corresponds to the standard input stream. Here, this stream represents the keyboard.

The operator >> is known as extraction or get from operator. It extracts (or takes) the value from the keyboard and assigns it to the variable. This corresponds to a familiar scanf() operation. Like >>, the operator >> can also be overloaded.

Output operator

Cout << ”C++ is better than C.”;

 

Causes the string in quotation marks to be displayed on the screen. This statement introduces two new C++ features, cout and << . The identifier cout(pronounced as C out) is a predefined object that represents the standard output stream in C++. Here, the standard output stream represents the screen. It is also possible to redirect the output to other output devices. The operator << is called the insertion or put to operator.

Leave a Comment