csc1100 lecture01 ch01 pt2-paradigm (1)

21
A First Book of C++ Chapter 1(Pt 2) Programming Design & Paradigm

Upload: iium

Post on 13-Feb-2017

155 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Csc1100 lecture01 ch01 pt2-paradigm (1)

A First Book of C++Chapter 1(Pt 2)

Programming Design &

Paradigm

Page 2: Csc1100 lecture01 ch01 pt2-paradigm (1)

A First Book of C++Programming Design

Page 3: Csc1100 lecture01 ch01 pt2-paradigm (1)

Step 1 - Analyze the problem Outline the problem and its requirements Design steps (algorithm) to solve the problem

Step 2 - Implement the algorithm Implement the algorithm in code Verify that the algorithm works

Step 3 - Maintenance Use and modify the program if the problem

domain changes

Problem Solving Process (revisit)

C++ Programming: From Problem Analysis to Program Design, Third Edition 3

Page 4: Csc1100 lecture01 ch01 pt2-paradigm (1)
Page 5: Csc1100 lecture01 ch01 pt2-paradigm (1)

Thoroughly understand the problem Understand problem requirements

Does program require user interaction? Does program manipulate data? What is the output?

If the problem is complex, divide it into sub-problems Analyze each sub-problem as above

Analyze the Problem

C++ Programming: From Problem Analysis to Program Design, Third Edition 5

Page 6: Csc1100 lecture01 ch01 pt2-paradigm (1)

If problem was broken into sub-problems Design algorithms for each sub-problem

Check the correctness of algorithm Can test using sample data Some mathematical analysis might be

required

Design an Algorithm

C++ Programming: From Problem Analysis to Program Design, Third Edition 6

Page 7: Csc1100 lecture01 ch01 pt2-paradigm (1)

Once the algorithm is designed and correctness verified Write the equivalent code in high-level

language

Type the program using text editor

Write the Code

C++ Programming: From Problem Analysis to Program Design, Third Edition 7

Page 8: Csc1100 lecture01 ch01 pt2-paradigm (1)

Run code through compiler If compiler generates errors

Look at code and remove errors Run code again through compiler

If there are no syntax errors Compiler generates equivalent machine code

Linker links machine code with system resources

Compiling and Linking

C++ Programming: From Problem Analysis to Program Design, Third Edition 8

Page 9: Csc1100 lecture01 ch01 pt2-paradigm (1)

Once compiled and linked, loader can place program into main memory for execution

The final step is to execute the program Compiler guarantees that the program

follows the rules of the language Does not guarantee that the program will run

correctly

The Loader and Executing

C++ Programming: From Problem Analysis to Program Design, Third Edition 9

Page 10: Csc1100 lecture01 ch01 pt2-paradigm (1)

A First Book of C++Programming Paradigm

Page 11: Csc1100 lecture01 ch01 pt2-paradigm (1)

Definition:

A way of conceptualizing what it means to perform computation and how tasks to be carried out on the computer should be structured and organized.

Programming Paradigm

A First Book of C++ 4th Edition 11

Page 12: Csc1100 lecture01 ch01 pt2-paradigm (1)

General types of programming paradigms: Structural Imperative Declarative Procedural Object-Oriented Functional Domain-Specific

Programming Paradigm (cont'd.)

A First Book of C++ 4th Edition 12

Page 13: Csc1100 lecture01 ch01 pt2-paradigm (1)

Structured programming (or modular programming) is a problem solving strategy and a programming methodology that includes the following guidelines: The flow of control in the program should

be as simple as possible. The construction of a program should

embody top-down design.

Programming Paradigm (cont'd.)

A First Book of C++ 4th Edition 13

Page 14: Csc1100 lecture01 ch01 pt2-paradigm (1)

Top-down design means: It repeatedly decomposing a problem into

smaller problems. Eventually leads to:

a collection of small problems or tasks. each can be easily coded

Programming Paradigm (cont'd.)

A First Book of C++ 4th Edition 14

Page 15: Csc1100 lecture01 ch01 pt2-paradigm (1)

Object-oriented programming is a method of programming based on a hierarchy of classes, and well-defined and cooperating objects.

A class is a structure that defines the data and the methods to work on that data. In simple definition, a class is a programmer

defined data type.

Programming Paradigm (cont'd.)

A First Book of C++ 4th Edition 15

Page 16: Csc1100 lecture01 ch01 pt2-paradigm (1)

Object-oriented programming (OOP) concepts are:

Programming Paradigm (cont'd.)

A First Book of C++ 4th Edition 16

Concepts FunctionsData Abstraction

specifies behavior

Encapsulation controls visibility of names (data hiding)

Polymorphism accommodates various implementations

Inheritance facilitates code reuseModularity relates to unit of compilation

Page 17: Csc1100 lecture01 ch01 pt2-paradigm (1)

Structured vs. Object-Oriented Programming

A First Book of C++ 4th Edition 17

Structured Programming:

MAIN PROGRAM

FUNCTION 3FUNCTION 2

GLOBAL DATA

FUNCTION 5FUNCTION 4

FUNCTION 1

Page 18: Csc1100 lecture01 ch01 pt2-paradigm (1)

Structured Programming:

Using functions. Function & program is divided into

modules. Every module has its own data and function

which can be called by other modules.

Structured vs. Object-Oriented Programming

A First Book of C++ 4th Edition 18

Page 19: Csc1100 lecture01 ch01 pt2-paradigm (1)

Structured vs. Object-Oriented Programming

(cont'd.)

A First Book of C++ 4th Edition 19

Object-Oriented Programming:

Object 1 Object 2

Data

Function

Data

Function

Object 3

Data

Function

Page 20: Csc1100 lecture01 ch01 pt2-paradigm (1)

Object-Oriented Programming:

Objects have both data and methods. Objects of the same class have the same

data elements and methods. Objects send and receive messages to

invoke actions.

Structured vs. Object-Oriented Programming

A First Book of C++ 4th Edition 20

Page 21: Csc1100 lecture01 ch01 pt2-paradigm (1)

Data object Set of values packaged as single unit (e.g., student’s name &

grade) Class

Set of objects with similar attributes General concept of object-oriented programming is the

difference between an object and the larger set of which it is a member (class)

A red Ford is an instance, or object, of a general class of automobiles or cars

Classes and Objects

A First Book of C++ 4th Edition 21