Fp201 unit2 1

21
Unit 2: Understand the basic structure of C++

description

 

Transcript of Fp201 unit2 1

Page 1: Fp201 unit2 1

Unit 2:Understand the basic structure of C++

Page 2: Fp201 unit2 1

Objectives Evolution of C++ Structure of C++ program

• Preprocessor directives• Header files• Main() function• Return statement

Hands On!

Page 3: Fp201 unit2 1

At the end of this presentation, you will be able to:• Describe the general structure of C++

programs• Write a simple C++ programme

Page 4: Fp201 unit2 1

Bjarne Stroustrup founded C++ in mid 80’s.

C++ was developed at AT&T Bell laboratories.

Additional features than C. Features are closer to the real world

solution.

Page 5: Fp201 unit2 1

Include File(must have)

Class declaration (if any)

Class Member Function definition (if any)

Main function(must have)

Page 6: Fp201 unit2 1

Header file Description

<cassert> Contains macros and information for adding diagnostic that aid program debugging

<cstring> Contains function prototype for C-style string processing

<cmath> Contains function prototype for math library function

<iostream> Contains function prototype for standard input and standard output function

<iomanip> Contains function prototype for stream manipulator that enable formatting of streams of data

<fstream> Contains function prototype for functions that perform input from files on disk and output to files on disk.

Page 7: Fp201 unit2 1

<preprocessor directive>

<main function> { <variable declaration>….

<C++ statement>….

<return statement(if any)> }

Page 8: Fp201 unit2 1

//First C++ Program#include <iostream>using namespace std;

int main(){int a;cout << “Hello World!”;cin >> a;return 0;}

Comment

Preprocessor Directive

Main Function

Variable Declaration

Statement

Page 9: Fp201 unit2 1

Line 1: // First C++ program • comment line. • ignored by the compiler and do not have

any effect on the executable. • comments in programs help the

programmer (and users) to understand what the program (or section) does.

• C++ supports two types of comments: // line comment

/* block comment */

Page 10: Fp201 unit2 1

Line 2: #include <iostream> • lines beginning with a hash (or pound) sign (#)

are directives for the preprocessor. • runs before the compiler each time the

compiler is invoked. • translates any line that begins with a hash

symbol (#) into a special command, getting your code file ready for the compiler.

• #include <iostream> tells the preprocessor to include the iostream standard file which contains the declarations of the basic standard input-output library in C++.

Page 11: Fp201 unit2 1

Line 3: using namespace std; • namespace allows to group entities like variables,

classes, objects and functions under a name. • elements belonging to the standard C++ library

are declared in what is called a std namespace. • example: cout is defined under std namespace

where you can see the details in the file <iostream>.

• if you omit this line, in order to use cout, you need to write the name of the namespace (std) followed by scope operator (::) before cout. Eg: std::cout << "Hello world!";.

Page 12: Fp201 unit2 1

Line 4: empty line• An empty line does nothing except for help

the programmer to view the source code more clearly.

Line 5: int main()

• actual program starts must start with a function named main().

• every C++ program has only one main() function.

Page 13: Fp201 unit2 1

Lines 6 and 11: The body of the main() function is enclosed in braces

({ }).

Line 8: cout << "Hello World!"; this line is a C++ statement which performs the only

action that generates a visible effect in our first program. Each statement must end with a semicolon character (;). Here's how cout is used: type the word cout, followed by

the output insertion (or redirection) operator (<<). Whatever follows the output insertion operator is output

to the screen.If you want to output a string of characters be sure to

enclose them in double quotes (" "), as shown on line 7, "Hello World!".

Page 14: Fp201 unit2 1

Line 10: return 0; • return statement causes the main() function

(i.e. the program) to finish.

Page 15: Fp201 unit2 1

Write a program that outputs following lines to the screen:

Welcome to the world of C++

Page 16: Fp201 unit2 1

#include <iostream>using namespace std;

void main( ){cout << “Welcome to the world of C++” }

Page 17: Fp201 unit2 1

Why does the following program fail?

#include <iostreams> using namespace std;

void main() { cout << “Is there a bug here?”; }

Page 18: Fp201 unit2 1

Because of the typo error for the preprocessor directive

Wrong : iostreams Correct: iostream

Page 19: Fp201 unit2 1

Explain why we use std:: in the following program. What is the output of program?

#include <iostream>

void main() { std::cout << " ## # # "<< std::endl; std::cout << "# ### ###"<<std::endl; std::cout << " ## # # "<< std::endl; }

Page 20: Fp201 unit2 1

Refer to slide 11

Page 21: Fp201 unit2 1