CPP Lecture L3S
CPP Lecture L3S
Systems
BIT Level 100 Summer
IT 102: High Level Programming
Lecturer: Christian Wogbe Biekro
Cell: 024 - 487 6102 / 050 – 447 8665
E-mail: [email protected]
[email protected]
Lecture 3
3
Sir Chris, GTUC -Koforidua
Learning Objectives
By the end of this lecture, students should be able
to:
• explain the basic structure of C++ program.
4
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
1. The Basic Structure of C++ Program
Program Result / Output
// My first program in C++
# include <iostream> This is my first C++ Program.
using namespace std;
int main ( )
{
cout <<"This is my first C++ Program.";
system (“pause”);
return 0;
}
5
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
2. The Structure of C++ Program Explained
• Line 1: - // My first program in C++
- This is a comment line.
- All lines beginning with two or double slash signs
(//) are considered comments and do not have any
effect on the behaviour of the program.
6
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
2. The Structure of C++ Program Explained
• Line 2: - # include <iostream>
- Lines beginning with a sharp/hash sign (#) are
directives for the preprocessor and are not
executable code lines.
7
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
2. The Structure of C++ Program Explained
• Line 3: - using namespace std;
- All the elements of the standard C++ library are
declared within what is called a namespace, the
namespace with the name std.
8
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
2. The Structure of C++ Program Explained
• Line 4: - int main( )
- This line corresponds to the beginning of the
definition of the main function. The main function
is the point where all C++ programs start their
execution, independently of its location within the
source code.
What is contained within these braces is what the function does when it is
executed.
10
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
2. The Structure of C++ Program Explained
• Line 5: - {
program_statements
}
- The two curly brackets ( { } ) are used to group all statements
together. This is known as Block, which contain a set of logically
connected statements that are surrounded by opening and closing
braces.
- The program_statements are the necessary instructions that make up
the main function. These may include variables declarations,
variable assignments, decision making statements, looping
statements, input and output statements, etc.
11
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
2. The Structure of C++ Program Explained
• Line 6: - cout<< “This is my first C++
Program”;
• This line is a C++ statement. A statement is a simple or compound
expression that can actually produce some effect.
- cout – console output, represents the standard output stream in C++,
and the meaning of the entire statement is to insert a sequence of
characters (in this case the “This is my first C++ Program ” sequence
of characters) into the standard output stream which usually is the
screen.
- cout - is declared in the iostream standard file within the std namespace,
so that's why we needed to include that specific file and to declare that
we were going to use this specific namespace earlier in our code.
12
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
2. The Structure of C++ Program Explained
• Line 7: - system (“pause”);
- This tells the compiler to pause after compile & run
for the programmer to view the output of the
compiled program.
• Line 8: - return 0;
- The return 0 tells the compiler that the program
ends here, or it is the last executable statement of
the main function.
13
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
2. The Structure of C++ Program Explained
• Semicolon - ;
- In C++, the semicolon is a statement terminator. That is,
each individual statement must end with a semicolon. It
indicates the end of one logical entity. Absent of it may
result in syntax error.
15
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
3. Installing C++ Compiler cont’d
16
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
4. Entering Your First C++ Program
• Enter, compile and run the program below using Dev-C++ or
Code::Block IDE.
Program
// My first Program in C++
# include <iostream>
using namespace std;
int main ( )
{
cout <<"This is my first C++ Program.";
system (“pause”);
return 0;
}
• The files you create with your editor are called source files and for C+
+ they typically are named with the extension .cpp by default. 17
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
4. Your Code or Program should look like this:
18
Sir Chris, GTUC -Koforidua
Class Exercise
Question:
1. Write a C++ program that prints “ I am Learning to program in
C++ ” message.
20
Sir Chris, GTUC -Koforidua
Solution - Class Exercise
Solution to Question 2:
Program:
21
Sir Chris, GTUC -Koforidua
Review Questions
Question:
1. (a) (i) What is C++ ?
(ii) What is IDE? Name two IDEs and their platforms that can
be used to compile C++program.
2. (i) Why are comments required in a C++ program? Name and
explain the type of comments that can be used in C++
program.
(ii) Why must you write comments before or while you write the
program, and never after?
(iii) What is the purpose of the return 0; near the end of each
program?
(iv) What is the meaning and use of Cout and Cin in C++?
22
Sir Chris, GTUC -Koforidua
Reading Assignment
Read on:
1. C++ Colour (Color) Codes.
2. Standard End Line (endl) and New Line (\n).
3. Escape Sequence.
4. Global and Local Variables
4. Operators in C++.
23
Sir Chris, GTUC -Koforidua
END