fp201 unit2 1
DESCRIPTION
TRANSCRIPT
Unit 2:Understand the basic structure of C++
Objectives Evolution of C++ Structure of C++ program
• Preprocessor directives• Header files• Main() function• Return statement
Hands On!
At the end of this presentation, you will be able to:• Describe the general structure of C++
programs• Write a simple C++ programme
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.
Include File(must have)
Class declaration (if any)
Class Member Function definition (if any)
Main function(must have)
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.
<preprocessor directive>
<main function> { <variable declaration>….
<C++ statement>….
<return statement(if any)> }
//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
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 */
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++.
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!";.
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.
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!".
Line 10: return 0; • return statement causes the main() function
(i.e. the program) to finish.
Write a program that outputs following lines to the screen:
Welcome to the world of C++
#include <iostream>using namespace std;
void main( ){cout << “Welcome to the world of C++” }
Why does the following program fail?
#include <iostreams> using namespace std;
void main() { cout << “Is there a bug here?”; }
Because of the typo error for the preprocessor directive
Wrong : iostreams Correct: iostream
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; }
Refer to slide 11