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

Lecture 4 Structure of a Program(1)

The document provides an introduction to the structure of a C++ program, specifically focusing on a simple 'Hello World' program. It explains the importance of practice in programming, the use of comments, and the role of namespaces and functions in C++. Additionally, it covers the basics of outputting text and the significance of the main function in C++ programs.

Uploaded by

luizmtui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lecture 4 Structure of a Program(1)

The document provides an introduction to the structure of a C++ program, specifically focusing on a simple 'Hello World' program. It explains the importance of practice in programming, the use of comments, and the role of namespaces and functions in C++. Additionally, it covers the basics of outputting text and the significance of the main function in C++ programs.

Uploaded by

luizmtui
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 27

Structure of a C++ program:

Program to output ‘hello world’

|
Introduction
• No one learnt to ride a bicycle by
just reading about how to ride. At
some time you have to get up there
and take some bumps.
• Just like riding the bike, you are not a
good confident rider until you have
practiced riding over and over.
• Apply the same persistence to
learning how to write C++ programs.

Monday, May 12, 2025 Introduction to Programming | 2


Topic Objectives
• Introduce the use of an integrated
development environment in
programming.

• Understand the basic structure of a


C++ program

• Write a program to output text.“Hello


world”

Monday, May 12, 2025 Introduction to Programming | 3


Introduction
• Writing C++ programs is like writing
an essay in English

• How?

Monday, May 12, 2025 Introduction to Programming | 4


Program to output a line of text
// Hello World program in C++
#include <iostream>
using namespace std;
int main ()
{
cout << “Hello World";
return 0;
}

Monday, May 12, 2025 Introduction to Programming | 5


Single line Comments in C++

// my first program in C++

• Single Line Comments begin with //,


indicating that the remainder of each
line is a comment.
• Comments are added to help other
people read and understand your
program
Monday, M
ay 12, 202
Introduction to Programming | 6
Multi line Comments in C++

/*This is my first programming language in C++ and I hope to


use it to solve real world problems*/

• Multi-Comments begin with /*, and


end with */
• Multi line Comments are used for
comments containing many lines

Monday, M
ay 12, 202
Introduction to Programming | 7
#include <iostream>
• programming is carried out in add-on
style. That is, if something is already
available you can use the same and
develop further.
• There are trillions of programs
already developed by others. Why
should we re-invent the wheel?

Monday, M
ay 12, 202
Introduction to Programming | 8
#include <iostream>
• If we plan to use the readily
available programs we have to ask
the compiler to include them.
• Actual code will be available in
machine language found in in built
libraries.

Monday, M
ay 12, 202
Introduction to Programming | 9
#
• Lines beginning with a hash sign (#) are
directives for the preprocessor.
• This essentially means read me before
you compile and do what I say
essentially...
• This line notifies the preprocessor to
include in the program the contents of the
input/output program that is<iostream>.

Monday, M
ay 12, 202
Introduction to Programming | 10
#
• General format for directives to the
pre processor is
• #include <inbuild program you want
to include>
• the < and > are just enclosures for
the compiler to read between and
import accordingly .

Monday, M
ay 12, 202
Introduction to Programming | 11
#include <iostream>
• iostream must be included for any
program that outputs data to the
Monitor or inputs data from the
keyboard using C++’s stream
input/output.

Monday, M
ay 12, 202
Introduction to Programming | 12
using namespace std;
• A namespace is a declarative region
that provides a scope to the identifiers
(names of the types, function, variables
etc) inside it.
• We are utilizing the standard (std)
namespace which contains the
definition of cin, cout, vector, map..etc
identifiers
Monday, M
ay 12, 202
Introduction to Programming | 13
The using directive:
• This directive tells the compiler that
the subsequent code is making use
of names in the specified
namespace.
• In this case the namespace is std

• The std namespace includes C++ I/O


objects/names cout and cin.
Monday, M
ay 12, 202
Introduction to Programming | 14
using namespace std;

• The semicolon depicts the end of a


statement
• What is a statement?

Monday, M
ay 12, 202
Introduction to Programming | 15
Statements in C++
A programming statement is the
smallest independent unit in a
program, just like a sentence in the
English language. It performs a piece
of programming action.
A programming statement must be
terminated by a semi-colon (;), just
like an English sentence ends with| a16
Monday, M
ay 12, 202
Introduction to Programming
Statements in C++
Why not ends with a period like an
english sentence? This is because
period crashes with decimal point - it
is hard for the computer to
differentiate between period and
decimal point

Monday, M
ay 12, 202
Introduction to Programming | 17
int main ()
• When a program begins running, the
system calls the function main,
which marks the entry point of the
program.

Monday, M
ay 12, 202
Introduction to Programming | 18
int main ()
• This line initiates the declaration of a

function. Essentially, a function is a

group of code statements which are

given a name: in this case, this gives the

name "main" to the group of code

statements that follow.

Monday, M
ay 12, 202
Introduction to Programming | 19
int main ()
• Functions will be discussed in detail in

a later chapter, but essentially, their

definition is introduced with a

succession of a type (int), a name

(main) and a pair of parentheses (()),

Monday, M
ay 12, 202
Introduction to Programming | 20
int main ()
The function named main is a special
function in all C++ programs; it is the
function called when the program is
run. The execution of all C++ programs
begins with the main function
regardless of where the function is
actually located within the code.

Monday, M
ay 12, 202
Introduction to Programming | 21
cout
• The standard output stream

• This is a name that belongs to the


namespace std

Monday, M
ay 12, 202
Introduction to Programming | 22
<<
• Referred to as the stream insertion
operator

• The right operand’s characters


normally print exactly as they
appear between the double quotes.

Monday, M
ay 12, 202
Introduction to Programming | 23
Out Put of our hello world program

Monday, M
ay 12, 202
Introduction to Programming | 24
Class /Lab Exercise
• Write a program that will output the
following statement

Happy New year 2020

Monday, M
ay 12, 202
Introduction to Programming | 25
Class /Lab Exercise
• Write a program that will output the
following statement

Welcome to the wonderful world of C+


+!!!

Monday, M
ay 12, 202
Introduction to Programming | 26
Ole Sangale Road, Madaraka Estate. PO Box 59857-00200, Nairobi, Kenya
Tel: (+254) (0)703 034000/200/300 Fax : +254 (0)20 607498
Email: [email protected] Website: www.strathmore.edu
|

You might also like