fp201 unit2 1
DESCRIPTION
TRANSCRIPT
![Page 1: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/1.jpg)
Unit 2:Understand the basic structure of C++
![Page 2: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/2.jpg)
Objectives Evolution of C++ Structure of C++ program
• Preprocessor directives• Header files• Main() function• Return statement
Hands On!
![Page 3: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/5.jpg)
Include File(must have)
Class declaration (if any)
Class Member Function definition (if any)
Main function(must have)
![Page 6: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/7.jpg)
<preprocessor directive>
<main function> { <variable declaration>….
<C++ statement>….
<return statement(if any)> }
![Page 8: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/8.jpg)
//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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/14.jpg)
Line 10: return 0; • return statement causes the main() function
(i.e. the program) to finish.
![Page 15: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/15.jpg)
Write a program that outputs following lines to the screen:
Welcome to the world of C++
![Page 16: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/16.jpg)
#include <iostream>using namespace std;
void main( ){cout << “Welcome to the world of C++” }
![Page 17: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/17.jpg)
Why does the following program fail?
#include <iostreams> using namespace std;
void main() { cout << “Is there a bug here?”; }
![Page 18: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/18.jpg)
Because of the typo error for the preprocessor directive
Wrong : iostreams Correct: iostream
![Page 19: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/20.jpg)
Refer to slide 11
![Page 21: Fp201 unit2 1](https://reader035.vdocuments.mx/reader035/viewer/2022081413/54981b1db479592b748b45f6/html5/thumbnails/21.jpg)