Home | | Computer Science 11th std | Sample program - A first look at C++ program

Chapter: 11th Computer Science : Chapter 9 : Introduction to C++

Sample program - A first look at C++ program

Let us start our first C++ program that prints a string “Welcome to Programming in C++” on the screen.

Sample program – A first look at C++ program

 

Let us start our first C++ program that prints a string “Welcome to Programming in C++” on the screen.


The above program produces, the following output:

 

Welcome to Programming in C++

 

This is very simple C++ program which includes the basic elements that every C++ program has. Let us have a look at these elements:

 

1 // C++ program to print a string

This is a comment statement. Any statement that begins with // are considered as comments. Compiler does not execute any comment as part of the program and it simply ignores. If we need to write multiple lines of comments, we can use /* ……. */.

 

2 # include <iostream>

Usually all C++ programs begin with include statements starting with a # (hash / pound). The symbol # is a directive for the preprocessor. That means, these statements are processed before the compilation process begins.

#include <iostream> statement tells the compiler’s preprocessor to include the header file “iostream” in the program.

The header file iostream should includ in every C++ program to implement input / output functionalities.

In simple words, iostream header file contains the definition of its member objects cin and cout. If you fail to include iostream in your program, an error message will occur on cin and cout; and we will not be able to get any input or send any output.

 

3 using namespace std;

The line using namespace std; tells the compiler to use standard namespace. Namespace collects identifiers used for class, object and variables. Namespaces provide a method of preventing name conflicts in large projects. It is a new concept introduced by the ANSI C++ standards committee.

 

4 int main ( )

C++ program is a collection of functions. Every C++ program must have a main function.

The main( ) function is the starting point where all C++ programs begin their execution.

Therefore, the executable statements should be inside the main( ) function.


The statements between the curly braces (Line number 5 to 8) are executable statements. This is actually called as a block of code. In line 6, cout simply sends the string constant “Welcome to Programming in C++” to the screen. As we discussed already, every executable statement must terminate with a semicolon. In line 7, return is a keyword which is used to return the value what you specified to a function. In this case, it will return 0 to main( ) function.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 9 : Introduction to C++ : Sample program - A first look at C++ program |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.