Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lecture 3

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 34

COLLEGE OF INFORMATICS AND VIRTUAL EDUCATION

(CIVE)

CP123: Introduction to High-Level Programming

Introduction to Programming with C++

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

 A programming language is an artificial language


designed to communicate instructions to a machine,
particularly a computer.
 A program is a set of ordered instructions that
enable a computer to carry out a specific task.
 Software is a set of instructions or programs, which
when translated and executed cause a computer to
perform specific operations.
 A programmer (coder, developer, software
engineer) is a person who designs, writes and tests
computer programs.
3
Programming Language History
 1822:Charles Babbage Difference machine.
 1942:ENIAC – the first electric machine.
 1945: John Von Neumann
 Shared program technique.
 Condition control transfer.
 1949:John Von Neumann – first computer
language- Short Code.
 1951: Grace Hopper – first compiler.
 1957: IBM FORTRAN (FORmula TRANslate).
4
Programming Language History..
 1959: COBOL – (Common Business Oriented
Language) language for Business.
 1958: Algol – the root of Pascal, C and Java.
 1968 : Niklaus Wirth – Pascal – invented for
education purposes.
 1972 : Dennis Ritchie - Bell Labs – C for new
UNIX system.
 1983 : Bjarne Stroustroup – C++ OOP Concept.
 1990 : Sun Microsystems – Java.

5
Programming Language History..

6
Programming Approaches and Technologies

 Imperative (Structural, Procedural)


Programming:
 program as a collection of statements and
procedures affecting data (variables).
 E.g. FORTRAN, BASIC, COBOL, Pascal, C.
 Declarative Programming:
 style that describes what computation should be
performed and not how to compute.
 E.g. HTML, XML, XAML, XSLT.

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

 Third Generation of Programming


Languages (3rd GPLs), aka High-level
language -- permits humans to write complex
programs without going step-by step.
 High-level languages include Pascal, C, C++,
FORTRAN, Java, BASIC, and many more.
 One command in a high-level language, like
writing a string to a file, may translate to
dozens or even hundreds of machine language
instructions. 11
Generations of Programming
Languages..
 Fourth Generation Programming
Languages(4th GPL) use a text-based
environment (like a 3GL) or may allow the
programmer to work in a visual environment,
using graphical tools.
 4th GPLs are easier to use than 3 rd GPL.
 Examples are Visual Basic(VB), Visual Age,
Authoring Environments, and Database Query
Language.
12
Generations of Programming
Languages..
 Fifth-generation languages (5th GPLs) are an
issue of debate in the programming community
– some programmers cannot agree that they
even exist
 These high-level languages would use artificial
intelligence to create software, making 5 th
GPLs extremely difficult to develop.
 Solve problems using constraints rather than
algorithms, used in Artificial Intelligence
13
Generations of Programming
Languages..
 Machine Language and Assembly Language are
also referred to as Low-level Languages because
problems are solved in an environment in which
the human must conform to the characteristics of
the Machine.
 Third, Fourth and Fifth Generation Programming
Languages are also referred to as High-level
Programming languages because problems are
solved in an environment in which the machine
conforms to the human’s characteristics.
14
Generations of Programming
Languages..
 As you move from the first generation to the
fifth, programming becomes easier and less
prone to errors.

 Finding and fixing errors is easier with High-


level languages (3rd, 4th , and 5th ) than with
Machine Language and Assembly Language.

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.

 Whereas a compiled-program executes much faster than an


interpreted-program, an interpreter allows examination and
modification of the program while it is running (executing).

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

You might also like