0% found this document useful (0 votes)
26 views62 pages

Computer Programming: P.B.Shehani Ariyathilake - RSGIS - Faculty of Geomatics - 0765251192 - Shehani@geo - Sab.ac - LK

Uploaded by

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

Computer Programming: P.B.Shehani Ariyathilake - RSGIS - Faculty of Geomatics - 0765251192 - Shehani@geo - Sab.ac - LK

Uploaded by

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

Computer Programming

FC12323
Lecture 01

P.B.Shehani Ariyathilake|RSGIS|Faculty of Geomatics|0765251192|[email protected]


Module Content
• Introduction to C++
• Introduction to object oriented programming
• Data types
• Variables and constant
• Basic input and outputs in C++
• Operators and expressions
• Control structures
• Basics of functions
• Arrays and classes in C++
• Pointers and references
• OOP With C++

2
Scheme of Evaluation
• Continuous Assessments - 50%
• Mid Term Exam(MCQ) - 15%
• Quiz (In class) - 5%
• Practical Exam - 30%
• Final Examination(Most probably Online) - 50%

3
Recommended Reading
• Deitel & Deitel’s (2016), C++ How to Program, 9th Edition

4
What is a program?
• Collection of instructions that describes a task or set of tasks to be
carried out by a computer.

• Can be simple or complex

• Three basic tasks


• Manipulate data
• Perform operations
• Provide results

5
What is a program and programming Language?

Programmer Instructions / Program Computer

Programming Language

C++

6
Programming Language
• Arti icial human created language, which translates instructions from a
readable format to a computer readable format.

• It has become a main method of instructing a computer to perform


speci ic task.

• Each language has its own special set of keywords and syntax, which
makes each programming language unique.

7
f
f
Introduction to C++
• C++, an extension of C, was developed by Bjarne Stroustrup in 1979 at Bell
Laboratories. Originally called “C with Classes”, it was renamed to C++ in
the early 1983.

• C++ is the object oriented extension of C.

• As for C there is an ANSI/ISO standard ( inal draft 1998) for the C++
programming language. This will ensure that the C++ code is portable
between computers.

8
f
Introduction to C++
• Each component becomes a self-contained object that contains its own
instructions and data that relate to that object. In this way, complexity is
reduced and the programmer can manage larger program.

• Standard C++ is the one that is currently accepted by all major compilers

• Intermediate language

• Case sensitive language

9
Introduction to C++

10
C Vs C++
// C Program // C++ Program

#include <stdio.h> #include <iostream>


using namespace std;
Int main ( )
{ int main()
{
printf (“Hello World ! \n”); cout << “Hello World!\n”;
return 0; return 0;

} }

Output Hello World !


11
Language Types
• Programmers write instructions in various programming languages to
perform their computation tasks such as:

12
Machine Language
• Any computer can directly understand only its own machine language
(also called machine code), de ined by its hardware architecture.

• Machine languages generally consist of numbers (ultimately reduced to 1s


and 0s).

• Such languages are cumbersome for humans.

13
f
Assembly Language
• Programming in machine language was simply too slow and tedious for most
programmers. Instead, they began using English-like abbreviations to represent
elementary operations.

• These abbreviations formed the basis of assembly languages.

• Translator programs called assemblers were developed to convert assembly-language


programs to machine language.

• Although assembly-language code is clearer to humans, it’s incomprehensible to


computers until translated to machine language.

14
High Level Language
• To speed up the programming process further, high-level languages were
developed in which single statements could be written to accomplish such tasks.

• High-level languages, such as C++, Java, C# and Visual Basic, allow us to write
instructions that look more like everyday English and contain commonly used
mathematical expressions.

• Translator programs called compilers convert high-level language programs into


machine language.

15
High Level Language
• The process of compiling a large high-level language program into
machine language can take a considerable amount of computer time.

• Interpreter programs were developed to execute high-level language


programs directly (without the need for compilation), although more
slowly than compiled programs.

16
High Level Language(Cont…)

• The high-level programming languages are broadly categorised in to two


categories:

1. Procedure oriented programming(POP) language

2. Object oriented programming(OOP) language


17
Group Activity

• Compare and contrast between C and C++ (Small PPT Describing


similarities and di erences between C and C++)

• Compare and contrast between POP and OOP(Small PPT Describing


similarities and di erences between POP and OOP)
1.

18
ff
ff
Procedure oriented programming(POP) language
• Problem is viewed as sequence of things to be done such as reading ,
calculation and printing.

• Procedure oriented programming basically consist of writing a list of


instruction or actions for the computer to follow and organizing these
instruction into groups known as functions.

19
Procedure oriented programming(POP) language(Cont…)

• The disadvantage of the procedure oriented programming languages


• No reusability of codes
• It does not model real word problem very well
• No data hiding

20
Procedure oriented programming(POP) language(Cont…)

Characteristics of procedure oriented programming


• Emphasis is on doing things(algorithm)
• Large programs are divided into smaller programs known as
functions.
• Most of the functions share global data
• Data move openly around the system from function to function
• Function transforms data from one form to another.
• Employs top-down approach in program design

21
Object oriented programming (OOP) Language
• Object Oriented Programming is a method of implementation in which,
programs are organized as a collection of objects which cooperate to
solve a problem.

22
Object Oriented Programming (OOP) Language
• Complex system is developed using smaller sub systems.
• Sub systems are independent units containing their own data and functions
• Can reuse these independent units to solve many di erent problems.
• Object
• Class
• Inheritance
• Polymorphism
• Abstraction
• Encapsulation

23

ff
Object

Oxford Dictionary

24
What are real world Objects?

25
Objects in OOP
• An Object is a software entity that models something in the real world.
• A combination of data and program that represent some real word entity.

26
Objects in OOP
• It has two main properties:
!State: the object encapsulates information about itself attributes or
ields.
!Behaviour: the object can do some things on behalf of other objects
metmethods

27
f
Objects in OOP

28
Objects in OOP
Cannot be change - Attributes or type=“Ferrari”;
What do you have? color=“Red”;
nouns
weight=500;

Break break()
Action or verbs
What do you do? Accelerate accelerate()
Drive drive()
29
Objects in OOP
Cannot be change - Attributes or type=“Ferrari”;
What do you have? color=“Red”;
nouns
weight=500;

Break break()
What do you do? Accelerate accelerate()
Action or verbs Drive drive()

Object is a combination of properties and Actions.


An object is any real world thing that you want to represent in
a program with 2 parts.
• Something it has – Properties
• Something it does - Actions
30
Grouping things together

31
Classes
We can classify objects into concepts. To do this we focus on the essential
properties of an Object. Classes are Concepts

32
Classes in OOP
Class car

What do you have? What do you have?


type=“Ferrari”; type=“Beetle”;
color=“Red”; color=“Maroon”;
weight=1500; weight=840;

What do you do? What do you do?


Break break() Break break()
Accelerate accelerate() Accelerate accelerate()
Drive drive() Drive drive()
Abstraction
• Data abstraction refers to the act of representing essential features
without including the background details.

• It is concerned with separating the behavior of a data object from its


representation.

34
Inheritance
• A new class of objects can be created quickly and conveniently by
inheritance the new class absorbs the characteristics of an existing class,
possibly customizing them and adding unique characteristics of its own.

• In our car analogy, an object of class “convertible” certainly is an object of


the more general class “automobile,” but more speci ically, the roof can
be raised or lowered.

35

f
Encapsulation
• Classes encapsulate (i.e., wrap) attributes and member functions into objects—an
object’s attributes and member functions are intimately related.

• Data is not accessible to the outside world and only those functions which are
declared in the class, can access it.

• Objects may communicate with one another, but they’re normally not allowed to
know how other objects are implemented implementation details are hidden
within the objects themselves. This information hiding, is crucial to good software
engineering.

36
Polymorphism
• Polymorphism in OOP is the ability to treat an object of any subclass of a
base class as if it were an object of the base class.

• A base class has, therefore, many forms: the base class itself, and any of
its subclasses.

37
Object oriented programming (OOP) Language(Cont…)

Features of the Object Oriented programming


• Emphasis is on doing rather than procedure.
• Programs are divided into what are known as objects.
• Data structures are designed such that they characterize the objects.
• Functions that operate on the data of an object are tied together in the data
structure.
• Data is hidden and can’t be accessed by external functions.
• Objects may communicate with each other through functions.
• New data and functions can be easily added.
• Follows bottom-up approach in program design.

38
Object oriented programming (OOP) Language(Cont…)
Advantages of the Object Oriented programming
• Through inheritance we can eliminate redundant code and extend the use of existing classes.
• We can build programs from the standard working modules that communicate with one another,
rather than having to start writing the code from scratch. This leads to saving of development
time and higher productivity.
• This principle of data hiding helps the programmer to build secure programs that can’t be
invaded by code in other parts of the program.
• It is possible to have multiple instances of an object to co-exist with out any interference.
• It is easy to partition the work in a project based on objects.
• Object-oriented systems can be easily upgraded from small to large systems.
• Message passing techniques for communication between objects makes the interface
description with external systems much simpler.
• Software complexity can be easily managed.

39
What makes C++ unique?
• Object oriented programming
• Simple
• High level programming language
• Case sensitive
• Compiler based
• Existence of libraries
• Reusability of codes
• Portability

40
C++ Applications

• Adobe Systems : All major applications of adobe systems are developed in


C++ programming language

• Skype : Core written in C++

• Google : Some of the Google applications are also written in C++, including
Google ile system and Google Chrome

41
f
C++ Applications(Cont…)
• Mozilla : Internet browser Firefox and email client Thunderbird are
written in C++ programming language (open source projects)

• MySQL : MySQL is the world’s most popular open source database


software, with over 100 million copies of its software downloaded or
distributed throughout its history.

42
C++ Applications(Cont…)
• Apple–OSX : Few parts of apple OS X are written in C++ programming
language. Few application for iPod are written in C++

• Microsoft : Windows 95, 98, Me, 200 and XP are also written in C++.
Also Microsoft O ice, Internet Explorer and Visual Studio are written in
Visual C++.

• Sebian : OS for cellular developed by C++

http s://mrcodehunter.com/what-is-cpp-us ed-for-re al-world-


applications/

43
ff
GIS Applications using C++

• Mapnik : C++/Python library for rendering

• QGIS : GIS software which integrates a variety of modules of the GRASS


GIS framework.

• Capaware : A C++ 3D GIS Framework with a multiple plugin architecture


for geographic graphical analysis and visualization.

• SAGA: GIS software which runs on Linux and Windows OS

44
C++ deployment environment
• C++ systems generally consist of three parts:

• A program development environment

• The language

• The C++ Standard Library

45
How C++ works
• C++ programs typically go through six phases
• Phase 1: Editing a Program
• Phase 2: Preprocessing a C++ Program
• Phase 3: Compiling a C++ Program
• Phase 4: Linking
• Phase 5: Loading
• Phase 6: Execution

46
How C++ works
• Phase 1: Editing a Program

47
How C++ works
• Phase 2: Preprocessing a C++ Program

48
How C++ works
• Phase 3: Compiling a C++ Program

49
How C++ works
• Phase 4: Linking

50
How C++ works
• Phase 5: Loading

51
How C++ works
• Phase 6: Execution

52
C++ compilers
• Computer software that translates (compiles) source code written in
a high-level language (e.g., C++) into a set of machine-
language instructions that can be understood by a digital
computer’s CPU.

• Compilers are very large programs, with error-checking and other


abilities.

53
C++ compilers

54
Console programs
• Console programs are programs that use text to communicate with the user and the environment,
such as printing text to the screen or reading input from a keyboard.

• A computer terminal where a user may input commands and view output such as the results of
inputted commands or status messages from the computer.

• Console programs are easy to interact with, and generally have a predictable behavior that is
identical across all platforms. They are also simple to implement and thus are very useful to learn the
basics of a programming language.

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

55
Console programs
• Here you have instructions on how to compile and run console programs
using di erent free Integrated Development Interfaces (IDEs)

• If you happen to have a Linux or Mac environment with development


features, you should be able to compile any of the examples directly from
a terminal just by including C++11 lags in the command for the compiler:

56
ff
f
Algorithms
• De inition: A step by step process to solve a particular problem

• we can represent algorithms by two ways,


1. Flow chart : The diagram representation

2. Pseudo codes : Representation or algorithm


57
f
Symbols of low chart
Start or stop

Input or output

Process

Decision or condition

Flow

58
f
Problem solving
• In a programming environment the problem solving process requires the
following steps.
1. Analyse the problem, outline the problem and its solution
requirement and design algorithm to solve the problem.
2. Implement the algorithm in a programming language such as C++
and verify that the algorithm works.
3. Maintain the program by using and modifying it if the problem
domain changes.

59
Exercise
• Find the sum of two numbers by using a low chart and pseudo code.

60
f
Exercise Start

Pseudo code
Read 2 num x, y
Input x, y
Find sum S as x+y
Print S
S = x+ y

Print S

End

61
Exercise
• Find the average of three numbers by using a low chart and pseudo
code.

62
f

You might also like