Lecture 3
Lecture 3
Lecture 3
(CIVE)
1
Topic Outline
Programming: Terms and Concepts
Programming Language History
Generations of Programming Languages
Compiler and Assembler
Introduction to Programming with C++
2
Programming: Terms and Concepts
5
Programming Language History..
6
Programming Approaches and Technologies
7
Programming Approaches and Technologies..
Logical Programming:
programs written in a logical programming language are
sets of logical sentences, expressing facts and rules about
some problem domain.
E.g. Prolog, Lisp, Oz, Ciao, Visual Prolog, XSB
Functional Programming:
program as a collection of (math) functions.
E.g. LISP, ML, Haskell.
Object Oriented Programming
program as a collection of classes for interacting objects.
E.g. SmallTalk, C++, Java. 8
Generations of Programming
Languages
First Generation Programming Language
(1st GPL), also known as Machine language
-- actual binary code that gives basic
instructions to the computer's CPU.
These are usually very simple commands like
adding two numbers or moving data from one
memory location to another.
Example: 1010 0010
9
Generations of Programming
Languages..
Second Generation of Programming Language (2 nd
GPL) also known as Assembly language -- a way
for humans to program computers directly without
memorizing strings of binary numbers.
It makes use of mnemonic codes, instead of binary
numbers .
There is a one-to-one correspondence with machine
code.
For example, in Intel x86 machine language, ADD
and MOV are mnemonics for the addition and move
operations.
10
Generations of Programming Languages..
15
Compilers and Assemblers
Microprocessors can only run machine
language programs directly.
Assembly language programs are assembled,
or translated into machine language.
Likewise, programs written in high-level
languages, like C++, must also be translated
into machine language before they can be run.
To do this translation is to compile a program.
16
Assemblers
An Assembler is a Programming language
processor that translates an assembly language
program (the source program) to the machine
language program (the object program)
executable by a computer.
17
Compilers
A Compiler is a Programming language
processor that translates a program written in a
high-level language (the 'source code') which
humans can understand, into machine
language program (the 'object code') which
the computers can understand.
In addition, the compiler optimizes the code
to run faster, adds error-correction code,
and links the code with subroutines stored
elsewhere.
18
Interpreters
An Interpreter is a Computer language processor that
translates a program line-by-line (statement-by-statement)
and carries out the specified actions in sequence.
19
Source Code
Source code -- it is the code written by the programmer in a
particular programming language.
This typically has an extension that indicates the language
used.
For example, Pascal source code usually ends in ".pas" and
C++ code usually ends in ".cpp“
20
Source Code..
The source code needs to be translated into machine
language (executional code or object code) that the
computer can understand in order to be executable.
The source code alone is just text; the actual program is the
translated code brought to life in machine language
A compiler is a program which translates source code to
object code. Therefore you will need a compiler to program
in C++.
21
Source Code..
Once you write your source code in the compiler, you
need to compile the source code to translate it into
machine language.
After compilation, you may run the program to test it.
If compilation is not successful and an error is shown,
you will need to debug the code to make the program
work.
22
Object Code..
Object code – is the output of a compiler after it
processes source code.
Object code usually includes only one module of a
program, and cannot be run yet since it is incomplete.
On DOS/Windows systems, this usually has an
extension of ".obj"
23
Executable Code..
Executable code -- refers to the code that can run directly on the
computer.
This is usually obtained by compiling and linking the source code.
All the object code modules necessary for a program to function
are linked together.
On DOS/Windows systems, this usually has an extension of ".exe"
24
Errors in Programming
Three kinds of Errors are encountered when programming:
Syntax errors are errors in the code you write so that
the compiler cannot understand your code, and the code
will not compile.
This usually involves misspelling a command or
forgetting a semicolon at the end of a statement
25
Errors in Programming..
Runtime or execution errors are errors halting the flow of
the program after it is compiled and run.
This usually happens when the wrong type of variable is given
in input (e.g. inputting text where an integer should be
entered).
Logical errors are errors in the sequence of instructions or in
the methods of calculation which do not halt the program but
produce the wrong results.
26
Introduction to C++
C++ was developed by Bjarne Stroustrup at AT&T Bell Labs in the
1980s.
C++ was derived from C, C was derived from B, and B was derived from
BCPL language.
The main addition to C++ is support for Object-Oriented programming.
So ‘++’ in C++ is used to denote that it is basically a C language that
supports Object-oriented programming style.
27
Introduction to C++
C++ is considered to be an intermediate-level language, as it
encapsulates both high- and low-level language features.
As a result, it allows a programmer to have direct control
over computer resources such as memory.
C++ is often used to develop games, game engines, and
Desktop applications.
28
C++ Compilers
You will need a text editor (e.g. Notepad) to write and edit your programs.
The compiler simply allows you to compile and run programs from the
command line interface:
GCC
MinGW
LLVM
Intel C++ Compiler
Clang
Turbo C++
29
Independent Development Environment
Independent Development Environment (IDE) is a software
application that provides comprehensive facilities to computer
programmers for software development.
It is more convenient and user-friendly than solely using a
compiler and text editor.
An IDE normally consists of a source code editor, build automation
tools and a debugger.
Examples include: Code::blocks, Visual Studio Express, Dev-C++
This course is going to use Dev-C++ IDE for all examples.
30
Dev-CPP IDE
Independent Development Environment (IDE)
31
Basic C++ Program
#include <iostream>
int main()
{
std::cout<<“Muce 2018”;
return 0;
}
Output when compiled and run
32
Basic C++ Program…
#include <iostream> instructs the preprocessor to include a
section of standard C++ code, known as header iostream, that
allows to perform standard input and output operations, such
as displaying text on the screen
int main () this denotes a function declaration. The function
is a block of code performing specific task. This function has
is specifically used when a C++ program is executed (run).
33
Basic C++ Program…
{ and } denotes the beginning and the end (respectively) of the
main function’s definition.
std::cout<<“Muce 2018”; this statement allows the words
“Muce 2018” to be displayed on the screen.
return 0; a function is supposed to return some value. In this
case, the main function is of integer (int) type, and therefore by
default it will display 0. This line is optional for the case of
main function.
34