0% found this document useful (0 votes)
7 views

CPP Lecture L3S

The document discusses the basic structure of a C++ program. It explains that a C++ program includes header files, namespaces, a main function containing statements like input/output, and a return value. It also covers installing a C++ compiler like Dev-C++ or Code::Blocks and entering a sample program to output the text "This is my first C++ Program."

Uploaded by

George Darko
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

CPP Lecture L3S

The document discusses the basic structure of a C++ program. It explains that a C++ program includes header files, namespaces, a main function containing statements like input/output, and a return value. It also covers installing a C++ compiler like Dev-C++ or Code::Blocks and entering a sample program to output the text "This is my first C++ Program."

Uploaded by

George Darko
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Faculty of Computing & Info.

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

Introduction to High Level


Language Programming (C+
+) - 2

Sir Chris, GTUC -Koforidua


Outline
1. The Structure of a C++ Program
2. The Structure of a C++ Program Explained
3. Installing C++ Compiler
4. Entering Your First C++ Program
5. Assignment

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.

• install C++ compiler and be familiar with the


source program environments - IDEs;

• code, compile & run their first 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.

- E.g. // - single line comment


/* block comment */ OR

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.

- The #include <iostream> is a directive or library


file that contains definitions of the routines that
handle inputs from the keyboard (e.g. cin) and
outputs to the screen (i.e. cout)

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.

- Namespace std contains all the classes, objects and


functions of the standard C++ library.

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.

- It is essential that all C++ programs have a main


function, and should contain only one main
function.
9
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
2. The Structure of C++ Program Explained
• Line 4: - int main( ) cont’d
- The word main is followed in the code by a pair of parentheses
(( )).
- That is because it is a function declaration.
- In C++, what differentiates a function declaration from other
types of expressions are these parentheses that follow its name.
- Optionally, these parentheses may enclose a list of parameters
within them.
 Right after these parentheses we can find the body of the main function
enclosed in braces ({ }).

 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.

•-<<In C++, << is an operator, called the stream insertion


operator. It is used to send output to the screen. i.e. cout<<
•- >>
In C++, >> is an operator, called the stream extraction
operator. It is used to get data from the keyboard into the
screen. i.e. cin>>
14
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
3. Installing C++ Compiler
• The easiest way for beginners to compile C++ programs is by
using an Integrated Development Environment (IDE). An IDE
generally integrates several development tools, including a
text editor and tools to compile programs directly from it.

• To use C++ compiler in Windows, you can download and install


any of the following IDE below.

15
Sir Chris, GTUC -Koforidua
Introduction to HLL Programming – C++ - 2
3. Installing C++ Compiler cont’d

• Integrated Development Environments (IDEs),


Platform and Console programs.

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.

2. Write a C++ program to compute the sum and average of two


numbers.

3. Write a C++ program to perform Addition, Subtraction,


Multiplication and Division of two numbers.
Hint:
Draw Algorithm, Flowchart and Screen-print your
program and result (output) for Question 3.
19
Sir Chris, GTUC -Koforidua
Solution - Class Exercise
Solution to Question 2:
Algorithm: Flowchart:
1. Input: Two numbers x and y.
2. Output: Sum and average of x
and y.
Step 1: input x, y
Step 2: sum=0, average = 0
Step 3: sum = x + y
Step 4: average = sum /2
Step 5: print sum and average. Sum,

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

Please bear in mind that C++ programming requires a lot of


thinking and skills. Most people are not able to write (good)
programs; not because learning C++ programming is difficult but
rather thinking to come out with the needed instructions the
computer must follow in solving the problem is difficult for them.
Sir Chris, GTUC -Koforidua

You might also like