PF Lecture 2 (Cin, Cout, Basic Structure of Program)
PF Lecture 2 (Cin, Cout, Basic Structure of Program)
Lecture # 2
Lecture Content
• Basic structure of C++ Program
• Cin statements
• Cout statements
• Errors
• Comments
• Lab Exercise
2
Basic structure of C++ Program
• The format of writing program in C++ is
called its structure. It consists of the
following parts:
– Preprocessor directive
– Main() function
– Program body (C++ statements)
3
Preprocessor Directive
• Preprocessor directive is an instruction given
to the compiler before the execution of actual
program.
• The preprocessor directives are processed by
a program known as preprocessor. It is part of
C++ compiler.
• It modifies C++ source program before
compilation.
• The preprocessor directive start with #
symbol.
4
Include preprocessor
In C++ Include preprocessor is used to
include header file in program.
• #include <iostream>
• The above statement tells the compiler
to include the file iostream in source
program before compiling.
5
Header files
• Libraries which contains different types of predefined
functions.
• Many header files can be included in one program.
• The header file must be included in the program
before calling any of its functions in the program.
• The extension of a header file is .h.
• These files are provided by C++ compiler system.
• The header files are normally stored in INCLUDE
subdirectory. The name of header file is written in
angle brackets.
6
main() Function
• Main function is starting point of the
program.
• Also it called entry point of the program.
void main()
{
Body of main function
}
7
cout and cin statements
These are predefined statements which help us to
perform input output operations.
Cout statement:
– Use to display output on console
– Operator used(<<) in cout statement is called
insertion operator.
Cin statement:
– Used to take input from user.
– Operator used(>>) in cin statement is called
extraction operator.
– Compile time errors
– Run time errors
– Logical errors
12
Compile-time error causes
Those error which comes at compile time e.g.
• Syntax errors
• Type checking errors
• (Rarely) compiler crashes
13
Run-time error causes
• Division by zero
• Dereferencing a null pointer
• Running out of memory
14
Comments
• Program comments are explanatory
statements that you can include in the
C++ code. These comments help
anyone reading the source code. All
programming languages allow for some
form of comments.
• C++ supports single-line and multi-line
comments. All characters available
inside any comment are ignored by C++
compiler.
15
Comments(Examples)
#include <iostream>
using namespace std;
Int main()
{
cout << "Hello World"; // prints Hello World
return 0;
}
16
Comments(Examples)
/* This is a comment */
17
Lab Exercise
1. Overview of visual studio
2. How to create C++ Project
Note: Project should be empty in start
3. How we can compile our code in visual
studio.
4. How we can run/execute our code in
visual studio.
18
Lab Exercise(Cont…)
• Write C++ code to print your name and age on
consol.
• Write and run a program to print your first name
on the first line, your middle name on the
second line and your last name on the third line
using only cout statement.
• Write program to display following pattern(You
can use only cout statement).
*************
*************
*************
19 *************
Lab Exercise(Cont…)
• Write a program that produces the
following output:
20
Lab Exercise(Cont…)
• Write C++ code which take your age as
an input and display following
statement,
“My age is 25”.
Note: Use proper commenting