1. UNITED REPUBLIC OF TANZANIA
MINISTRY OF EDUCATION, SCIENCE AND TECHNOLOGY
&
PRESIDENT’S OFFICE – REGIONAL ADMINISTRATION AND LOCAL GOVERNMENT
C++ PROGRAMMING
27/07/25 1
2. BRIEF CONTENTS TO BE COVERED
1. PROGRAMMING LANGUAGES
2. INTRODUCTION TO C++ PROGRAM
3. DATA TYPES
4. VARIABLES AND CONSTANTS
5. INPUT/OUTPUT
6. EXPRESSIONS AND ASSIGNMENTS
7. DECIONS
8. ITERATION
9. FUNCTIONS
10. ARRAYS AND STRINGS
3. •INTRODUCTION.
•LEVELS OF PROGRAMMING LANGUAGES.
•TRANSLATORS (COMPILERS, INTERPRETERS AND
ASSEMBLERS).
•SOURCE CODE AND OBJECT CODE.
•GENERATIONS OF PROGRAMMING LANGUAGES
1. PROGRAMMING LANGUAGES
4. A computer program is a set of instructions that tells a computer what to do.
These instructions can range from simple tasks, like adding two numbers, to
more complex processes like running a web server or simulating physical
phenomena.
In order to create a computer programs, you need a special language to
instruct the computer because it cannot understand normal human languages
such as English and Swahili.
Computer programming refers to the process of creating software programs,
which are sets of instructions that tell a computer what to do.
Programming languages are languages used to program the computer.
I: INTRODUCTION
5. •Historically computer programming language have passed
through five (5) generations. These generations can be
divided into two levels of programming languages.
a.Low level languages
b.High level languages.
II: LEVELS OF PROGRAMMING LANGUAGES
6. Low level programming languages- are characterized by little effort to
translate programs into machine readable forms. They are not portable; hence
they are not easy to be transferred from one computer to another. The reason
for importability is due to the fact that only the same processor of particular
manufacturer that language was designed for can read the codes written in
that computer for processor to translate. These languages are machine
dependent, and they do not require any compiler to translate them into
machine codes. Example includes machine language and assembly language.
a. Low Level Languages
7. Advantages of Low level
Programming Languages
a. Programs designed by using low level memory requires less
memory to be executed. For that case, they run fast.
b. Low level language do not need compilers or interpreters to
change into machine readable form.
c. Since a low-level language is closely related to machine
language, it provides direct interactions with the internal
memory of the computer.
d. There is direct communication between hardware parts and low
level programming language.
8. a. Low level language need high expertise in programming to design a
program.
b. The programs created with low-level language are not portable simply
because they cannot be transferred from one hardware or software to
another.
c. Using and learning low-level languages is not easy compared to high-
level language.
d. It is complicated to debug errors in low-level language.
Disadvantages of Low level
Programming Languages
27/07/25 8
9. High level languages are machine independent. This means that,
the execution of the program designed by high-level language does
not interfere with the normal operation of the computer system.
Unlike low-level language, high level language are closely related
to human language and not machine language. Example of high
level programming language includes Pascal, Fortan, COBOL, C,
C++, Java, Javascript, Visual Basics and Python.
b: High Level Languages
27/07/25 9
10. i.Commercial languages- are programming language used for commercial purpose such
COBOL.(Common Business Oriented Language)
ii.Scientific languages- are programming languages used for engineering and scientific
purpose such as FORTAN (FORmula TRANslation)
iii.Special purpose languages- are programming language intended to be used for a specific
purpose such as air traffic control and space technology. Example include Ada, and database
language Structured Query Language (SQL)
Types of High Level Languages
27/07/25 10
11. Types of High Level Languages
27/07/25 11
iv. Command languages for the operating system- are programming languages
created to communicate with the operating system and control operations
with in the computer system. An example of command language for
operating system is Shell language within UNIX operating system.
v. Scripting languages- are programming languages that do not need to be
translated into machine code before running. This means, compilation is not
required to execute scripting languages. Most scripting language are used to
design web pages. Example JavaScript, VBScript, PHP, Perl, Python and
Ruby.
12. Types of High Level Languages
27/07/25 12
vi. Procedural programming languages- are programming languages
divide a program into blocks of codes known as structured programs.
They design a program in sequence of steps or procedures made by a
systematic arrangements of functions, commands, and variable to
accomplish a particular task. Example of procedural programming
language are Pascal, BASIC, C, FORTAN and COBOL.
13. Types of High Level Languages
27/07/25 13
vii. Object oriented programming languages- are programming
languages made by many functional blocks known as objects. Each
object contains instructions similar to procedural programming.
Object oriented programming is characterized by polymorphism,
encapsulation and inheritance. Example of object-oriented
programming languages are C++, Java, Javascript, VisualBasic.net
and Python
14. Types of High Level Languages
27/07/25 14
viii. Multi-purpose languages- are programming languages required to
perform different task.
Example include C, C++, Java, JavaScript and Python.
15. Advantages of High Level Languages
27/07/25 15
• High-level languages provides an easy way to recognize prone to errors
• High level languages are machine independent, and they are portable
programming languages that can easily be transferred from one device to another.
• High level languages provides a conductive environment for developing,
executing and maintaining a program.
• High level programming language provides an easy way to learn and use them.
• Since high level languages run on different platforms, they are widely used in
programming.
16. Disadvantages of High Level Languages
27/07/25 16
• The program which is developed by high level languages must be interpreted or
compiled to change it into machine-readable form.
• They are not fast as they require time to change a program from source code to
object code.
• They overload the processor by using several typed statements, hence slowing the
computer’s processing speed.
• They need enough memory to be maintained and executed. Since high level
languages are not practiced in machine code, they have no direct interaction with the
hardware device.
17. III: TRANSLATORS (COMPILERS, INTERPRETERS
AND ASSEMBLERS).
27/07/25 17
TRANSLATORS
• Are tools that convert source code written in one programming language
into another form.
• There are three main categories of translators namely Assemblers,
Interpreters and Compilers
18. • An assembler is a program that translates assembly language code
(a low-level programming language that is closely related to machine
code) into machine code (binary instructions that a computer’s
processor can execute directly.
• Assembly language is more human-readable compared to raw
machine code, as it use mnemonics (like MOV, ADD, SUB, etc) to
represent machine instructions, making it easier for programmers to
write and understand compared to raw binary code.
Assemblers
27/07/25 18
19. 1. One-pass Assembler: This type of assembler reads the source code once and
produces the output in a single pass. It’s faster but may have limitations (like
requiring all labels to be defined before they’re used)
2. Two-pass Assembler: In the first pass, it gathers information about labels and
instructions, and in the second pass, it generates the machine code. This method is
more flexible and can handle forward references
•Note: Assemblers are critical for low-level system programming, where high
performance and close hardware interaction are necessary.
Types of Assemblers
27/07/25 19
20. • It checks whether the instructions written are valid, and identifies
any errors in the program. The assembler will display these errors as
well as the complete source and object programs. If the program has
no errors, the job control will let it run immediately, or save the
object program so that it may run it later without translating it again.
Functions of Assembler
27/07/25 20
21. • It assigns memory locations to the names the programmer uses.
Eg; the assembler keeps a table of these names so that if an instruction
refers to it, the assembler can easily tell the location to which it is
assigned.
27/07/25 21
Functions of Assembler
22. • It generates the machine code equivalent of the assembly instructions.
Usually, the assembler generates a machine code only when no errors
are detected. Some of the errors include;
Typing mistakes, Using wrong format for an instruction, Specifying
a memory location outside the range 0 - 2047
27/07/25 22
Functions of Assembler
23. • Are programs that translate code written in a high-level programming
language (like C++, Java, or Python) into machine code or an
intermediate form that a computer’s processor can understand and
execute.
• The process of translating a program written in a high-level source
language into machine language using a compiler is called Compilation.
• The process usually involves several stages: (Look the next slide)
Compilers
27/07/25 23
24. •1. Lexical analysis – The source code is broken down into tokens, which
are the smallest units of meaning.
•2. Syntax analysis – The structure of the code is checked against the
grammar of the programming language (parsing)
•3. Semantic analysis – The compiler checks whether the code makes
sense in terms of the language’s rules and logic.
•4. Optimization – The code is improved to run more efficiently, like
reducing memory usage or execution time.
•5. Code generation – The final output is produced, usually in the form of
machine code or bytecode.
Stages of Compilers
27/07/25 24
25. 27/07/25 25
Functions of Compiler
A compiler performs the following tasks during the compilation process;
•It identifies the proper order of processing, so as to execute the process as
fast as possible and minimize the storage space required in memory.
•It allocates space in memory for the storage locations defined in the
program to be executed.
•It reads each line of the source program and converts it into machine
language.
26. Are another type of language processor, but instead of translating the entire source code into
machine code at once like compilers, they translate and execute the code line by line, or
statement by statement.
•Here’s how they work: (Functions of an Interpreter)
•1. Reading the Source code – The interpreter reads the source code line or instruction at a time.
•2. Parsing – It parses each line to check if it’s syntactically correct.
•3. Executing - The interpreter directly executes the parsed instruction on the computer’s
hardware or via an intermediate representation (like bytecode)
Interpreters
27/07/25 26
27. • Execution speed: Interpreters generally execute code more slowly than compiled programs
because of the line-by-line processing.
•Error detection: Interpreters stop at the first error they encounter and give feedback
immediately, while compilers analyze the whole program and give all errors at once.
•Portability: Interpreted code is typically more portable (can run on different systems with the
appropriate interpreter) because it doesn't need to be compiled into machine code
specific to a platform.
Key differences between interpreters and compilers
27/07/25 27
28. Difference between Compilers
and Interpreters
27/07/25 28
S/N Compilers Interpreters
i. It converts the whole program into machine code at
once.
It translates only one statement at a time.
ii. It takes more time to analyze the source code. In
other words, compile time is more, however the
overall execution time is less.
It comparatively takes less time to analyze the
source code. In other words, compile time is less,
however the overall execution time is more.
iii. It generates an intermediate object code. Therefore,
more memory is used.
It does not generate any intermediate object code.
Hence, it is memory efficient.
iv. The whole program is compiled and then it shows
all errors together. Therefore, debugging is difficult.
It stops the compilation if any error occurs.
Hence, debugging is easier.
v. Programming languages like C, C++, Java, etc uses
compiler.
Programming languages like Python, Ruby, PHP,
etc use an interpreter. These interpreted languages
are also called scripting languages
29. • In programming, source code and object code are two
different stages of the software development process:
• i. Source code: Is the human-readable instructions written
in a high-level programming language. It’s a code that
developers write and understand when creating a program.
IV: SOURCE CODE AND OBJECT CODE
27/07/25 29
31. •ii. Object Code: Is the machine-readable output generated by a compiler
when it translates the source code. The object code is typically not directly
readable by humans and is what the computer can execute, often being
linked with other object files to form an executable. In other words, it is
typically in binary format (0s and 1s) that a computer's processor
can understand and execute.
IV: SOURCE CODE AND OBJECT CODE
33. •In the generation of programming languages for low level
languages are grouped in two generation namely first
generation and second generation, while high-level
languages can be grouped into three generation, namely
third, fourth and fifth generation.
•Therefore there are five generations of the programming
languages as follows:
V: GENERATIONS OF PROGRAMING LANGAUGES
34. Instruction in a machine language are written by using binary codes or
digits including 0 and 1. Machine language involves simple
operations such as addition and subtraction of number. Since
computer understand binary codes a program which is written in a
machine languages does not need further translation. For that case, a
program which is in machine language can be executed easily by
computer.
FIRST GENERATION COMPUTER
PROGRAMMING (MACHINE LANGUAGE)
35. • However is challenging to read and understand program written in
machine language because one must have program knowledge of
machine instruction. Example;
CONT...
36. Assembly language involves a set of symbolic operations
called mnemonics. Mnemonics are codes made up of two or
three letters such as LDN, STA and JPU. Normally features of
the assembly language differ from one computer to another
program written in assembly language should be translated
from the source program to machine code known as object
code. The program which is required to translate assembly
language into machine code is known as assembler.
SECOND GENERATION COMPUTER
PROGRAMMING(ASSEMBLY LANGUAGE)
38. Third generation languages involves programming
languages that can divide a program into subprograms
known as procedures. Some third generation languages are
called structural or procedural. A structural programming
language is simple, readable and easily recognized by a
human being. Structural programming languages apply few
and simple compared with other high-level languages.
Example of third generation languages are Pascal, FORTAN,
COBOL, BASIC, Ada and C.
THIRD GENERATION PROGRAMMING
LANGUAGES
40. Fourth generation language are closely related to human language.
They involves a graphical user interface in collaboration with event-
driven which is determined by actions of the user such as pressing
keyboards keys and mouse clicks. Fourth languages includes dropping
menus, forms which provide easy interaction to human being. The
fourth generation languages can be used to design websites and
databases. Example of fourth generation languages are Python, Ruby,
SQL, PHP, Javascript, VisualBasic and C++.
FOURTH GENERATION PROGRAMMING LANGUAGES
41. Figure below represent a simple calculator program that was
designed by using Visual Basics programming languages.
FOURTH GENERATION PROGRAMMING LANGUAGES
42. Fifth generation languages are advanced languages that do not need
human interaction to accomplish task. The program written in fifth
generation languages apply constraints of the program to solve problems
instead of an algorithm developed by the programmer. These languages
provided many automated features including an application generator
which stops programmers from typing several codes like in other
programming languages.
Examples of fifth generation languages are Mercury, Prolog and OPS5
FIFTH GENERATION PROGRAMMING LANGUAGES
#5:Reminder about the need to reflect on what we learn. You may find that there is a lot to take in or just need to scaffold learning
#7:Clarifying for ourselves the expectations for the training and the concerns. Individually, reflect and then share with a partner – share in a group of four. 8 mins max
#18:Planet Zog participants come in and question, seek clarification, press for more information to seek clarity. The scribe writes down roles and key responsibilities when the questioning is over. The sheets are used as a market place exercise and post it notes are used to critique and give feedback.
#19:Purpose of the exercise is the reinforce the importance of every one knowing their responsibilities and what they have to do to sustain and build the system securely.
#20:Planet Zog participants come in and question, seek clarification, press for more information to seek clarity. The scribe writes down roles and key responsibilities when the questioning is over. The sheets are used as a market place exercise and post it notes are used to critique and give feedback.
#23:Purpose of the exercise is the reinforce the importance of every one knowing their responsibilities and what they have to do to sustain and build the system securely.
#28:Purpose of the exercise is the reinforce the importance of every one knowing their responsibilities and what they have to do to sustain and build the system securely.
#29:Planet Zog participants come in and question, seek clarification, press for more information to seek clarity. The scribe writes down roles and key responsibilities when the questioning is over. The sheets are used as a market place exercise and post it notes are used to critique and give feedback.
#30:Introduce the Domains- why they were chosen. See section of the Handbook