Presentation is loading. Please wait.

Presentation is loading. Please wait.

BASIC PROGRAM CONSTRUCTION

Similar presentations


Presentation on theme: "BASIC PROGRAM CONSTRUCTION"— Presentation transcript:

1 BASIC PROGRAM CONSTRUCTION
Let’s look at the first program in C++. # include < iostream.h > int main ( ) { cout << “This is our first program in C++” ; return 0; }

2 PREPROCESSOR DIRECTIVES
The first line of the program # include is called a Preprocessor Directive, which is an instruction to the compiler. The preprocessor directive # include tells the compiler to insert a file into your source file.

3 Header Files In our example, the preprocessor directive # include tells the compiler to add the source file iostream.h to our program’s source file before compiling. The source file iostream.h contains declarations needed for input and output operators. Without these, the compiler won’t recognize I/O operators. So apart from other header files, in C++, this header file is always necessary.

4 FUNCTIONS Functions are the fundamental building blocks of C++. The first program consists entirely of a single function called main ( ) . The parentheses following the word main are the distinguishing feature of a function. A program may consist of many functions, but on startup, control always goes to main ( ). So Always Start with to main ( ), otherwise, the linking error will occur. The word int preceding the function name indicates that this particular function has an integer return value .

5 The Function Body Braces: The braces ( or curly brackets) play the role of Begin and End. Every function must use this pair of braces. Program Statements: are the basic unit of C++ programming. In our program we’ve only one program statement i.e. cout << “This is our first program in C++” ; This statement tells the computer to display the quoted phrase. A semicolon (;) indicates the end of the statement. Thus each statement has to be followed by a semicolon otherwise, the compilation error will occur.

6 OUTPUT USING cout The identifier cout ( pronounced as “ C out” ) is a predefined object in C++, called as Standard Output Stream. It flows data to display ( and other output devices). The operator << is called the Insertion or Put to operator. It directs the contents of the variable on R.H.S to the object on L.H.S. In our example, the phrase “This is our first program in C++” is an example of a String Constant or String Literal, is directed to cout by the insertion operator <<.

7 COMMENTS Comments are used to explain, what a program is doing. The compiler ignores the comments, so they do not add to the file size or execution time of a program. In C++, comments start with double slash symbol (//) and terminate at the end of the line. Comments can be written anywhere in a program. For example: // comments.cpp //demonstrates comments


Download ppt "BASIC PROGRAM CONSTRUCTION"

Similar presentations


Ads by Google