02 - First C++ Program

A C++ program is a collection of functions. Every C++ program must have the function main (). Execution of a program begins from the main() . Let us begin with a simple program that prints the string Have fun programming

#include <iostream.h>
void main()
{
   cout<< “Have fun programing”; // C++  statement
}

01 Function main()

Every C++ program requires a main function. This is where the program execution begins.

02 Comments

C++ supports both single line and multi-line comments. To add comments in a program we use “//” (double slash). A comment can start anywhere in a line, and whatever follows “//” is ignored by the compiler. A programmer can use these statements to add his own comments in a program.

03 Basic Input / Output

The iostream file contains declarations for cout,<<, cin and >> operators. The “#include” statement, in the program, causes the contents of the iostream file to be included in the above program. In essence, to use cin and cout statements in your program you need to include the iostream file in your program.

04 STANDARD OUTPUT (cout)

The only statement in the above example is an output statement. In C++, you should place a semi colon at the end of each statement. cout is a predefined object that belongs to the standard output stream. It provides you the ability to display characters to the screen.

<< is know as insertion or put to operator. It places the contents of the variable on its right to the object on its left. <<, in the program above, places the string “Have fun programing” in the cout object, which in turn displays it on the screen.

05 STANDARD INPUT (cin)

If you want the user to input a value, then you will need to use the cin object with the operator >> in your program. >> operator extracts the value from the object on its left and stores it to the variable to its right. This is the reason why  >> is called the extraction or get from operator. 

 

 

 

Like us on Facebook