+ All Categories
Home > Documents > MECH 215 Programming for Mech. and Ind. Eng. I

MECH 215 Programming for Mech. and Ind. Eng. I

Date post: 06-Jan-2016
Category:
Upload: amanda
View: 83 times
Download: 0 times
Share this document with a friend
Description:
Prof. Chadi Assi CB-410-13, Ext. 5799 [email protected]. MECH 215 Programming for Mech. and Ind. Eng. I. Lecture: Tuesday/Thursday8:45 — 10:00H-920 Office hours: Monday/Wednesday 2:30—3:30 Textbook : C++ How to Program/5e ISBN: 0131857576 by DEITEL&DEITEL - PowerPoint PPT Presentation
37
Outline 1 MECH 215 Programming for Mech. and Ind. Eng. I Prof. Chadi Assi CB-410-13, Ext. 5799 [email protected]
Transcript
Page 1: MECH 215 Programming for Mech. and Ind. Eng. I

Outline1

MECH 215Programming for Mech. and Ind.

Eng. I

Prof. Chadi AssiCB-410-13, Ext. [email protected]

Page 2: MECH 215 Programming for Mech. and Ind. Eng. I

Outline2

Course Highlight

Lecture: Tuesday/Thursday 8:45 — 10:00 H-920Office hours: Monday/Wednesday 2:30—3:30

Textbook: C++ How to Program/5e ISBN: 0131857576 by DEITEL&DEITEL

Tutorials: (starts the week of September 11)

TA ----J---- 15:15 16:05 FG-B055 Mr. Christopher Johnston ([email protected])

TB ----J---- 15:15 16:05 H-613 Mr. Alborz Arzpeyna ([email protected])

Lab: (starts the week of September 18)

TI ----J---- 16:15 17:05 TBD

TJ ----J---- 17:15 18:05 TBD

TK ----J---- 16:15 17:05 TBD

TL ----J---- 17:15 18:05 TBD

Page 3: MECH 215 Programming for Mech. and Ind. Eng. I

Outline3

Grading and Rules

1. Homework should be submitted by e-mail to Ms. Alessandra Pollifrone ([email protected] )

2. No late homework. No exception

4. Copying from the internet or any other source is unacceptable and considered cheating

5. Programming assignments should be given both hard and soft copy (hard copies are submitted to me in class; if you cannot make it to the class, e-mail your assignment)

Midterms (2) 26%

Final 60%

Assignments 14%

Total 100%

Page 4: MECH 215 Programming for Mech. and Ind. Eng. I

Outline4

Course Outline

1. Introduction to Computers2. Introduction to C++ Programming3. Introduction to Classes and Objects4. Control Statements Part 15. Control Statements Part 26. Functions7. Arrays and Vectors8. Pointers and Strings9. Classes and Data Abstraction

Page 5: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

5

Introduction to Computers

Outline

-Introduction-What is a Computer?-Computer Organization-Evolution of Operating Systems-Machine Languages, Assembly Languages, and High-Level - Languages-History of C and C++-C++ Standard Library-Visual C++ -Other High-Level Languages-Structured Programming-The Key Software Trend: Object Technology-- OOP

-Basics of a Typical C++ Environment-Hardware Trends

Page 6: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

6

Introduction

• Software– Instructions to command computer to perform

actions and make decisions

• Hardware (controlled by software)• C++ is today’s most popular software

development language– Standardized version of C++

• United States– American National Standards Institute (ANSI)

• Worldwide– International Organization for Standardization

(ISO)– Structured programming– Object-oriented programming

Page 7: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

7

What is a Computer?

• Computer– Device capable of performing computations and

making logical decisions

• Computer programs– Sets of instructions that control computer’s

processing of data

• Hardware– Various devices comprising computer

• Keyboard, screen, mouse, disks, memory, CD-ROM, processing units, …

• Software– Programs that run on computer

Page 8: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

8

Computer Organization

• Six logical units of computer1. Input unit

• “Receiving” section• Obtains information (e.g., data) from input devices

– Keyboard, mouse, microphone, scanner, networks, …

2. Output unit • “Shipping” section• Takes information processed by computer• Places information on output devices

– Screen, printer, networks, … – Information used to control other devices

Page 9: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

9

Computer Organization

• Six logical units of computer3. Memory unit

• Rapid access, relatively low capacity “warehouse” section

• Retains information from input unit– Immediately available for processing

• Retains processed information– Until placed on output devices

• Often called: memory or primary memory

4. Arithmetic and logic unit (ALU) • “Manufacturing” section• Performs arithmetic calculations and logic

decisions

Page 10: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

10

Computer Organization

• Six logical units of computer5. Central processing unit (CPU)

• “Administrative” section• Supervises and coordinates other sections of

computer

6. Secondary storage unit • Long-term, high-capacity “warehouse” section• Storage for

– Inactive programs or data• Secondary storage devices

– Disks• Longer to access than primary memory• However, less expensive storage per unit than

primary memory

Page 11: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

11

Computer Organization

Page 12: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

12

Main (or primary) Memory (1)

• It stores – programs– data– results

Page 13: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

13

Main Memory (2)

Memory: an ordered sequence

of storage locations called memory cells

Every cell has a content (to store data) and an address

-27.2

354

0.005

75.62

Address Content

0

1024

1

1023

RTV0001

108 ADD1024

107

STR0005109

Page 14: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

14

Main Memory (3)

• To access cell info:– each cell has a unique address

• Content of a cell may or may not be meaningful to our program (unless initialized or accessed by the program)

• A cell =– A grouping of smaller units called bytes

• A byte =– Amount of storage required to store a single

character

• Number of bytes per cell varies

Page 15: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

15

Main Memory (4)

• A byte : – Composed of smaller units of storage called bits

• Binary: – a numbering system of two numbers 0 & 1

• Bit = Binary digit:– Smallest element a computer can deal with

(computers deal only with binary numbers)

0 1 2 3 4 5 6 7

byte

a bit

Page 16: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

16

Evolution of Operating Systems (OS)

• Early computers– Single-user batch processing

• Only one job or task at a time• Process data in groups (batches)

• Operating systems (developed to make the use of computers more convenient and improve the efficiency of computing systems)– Software systems– Manage transitions between jobs (early OS)– Increased throughput

• Amount of work computers process

Page 17: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

17

Evolution of Operating Systems

• As computers became more powerful, computing resources were not used more efficiently under the single-user batch processing model– Most of the time machine was waiting for slow

input/output devices

• Multiprogramming – Many jobs or tasks sharing computer’s

resources– “Simultaneous” operation of many jobs (i.e.,

sharing)

Page 18: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

18

Evolution of Operating Systems

• Timesharing– 1960s– Special case of multiprogramming– Users access computer through terminals

• Devices with keyboards and screens• Dozens, even hundreds of users

– Perform small portion of one user’s job, then moves on to service next user

• Does not run all users programs simultaneously

– Advantage: • User receives almost immediate responses to

requests (no pipelining of tasks)

Page 19: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

19Machine Languages, Assembly Languages, and High-level

Languages1.1. Machine languageMachine language

• Only language computer directly understands• “Natural or native language” of computer• Defined by hardware design

– Machine-dependent (a particular machine language can be used only on one type of computer)

• Generally consist of strings of numbers– Ultimately 0s and 1s (binary)

• Instruct computers to perform elementary operations– One at a time

• Cumbersome for humans• Example:

+1300042774+1400593419+1200274027

Page 20: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

20Machine Languages, Assembly Languages, and High-level

Languages2.2. Assembly languageAssembly language

• English-like abbreviations representing elementary computer operations

• Clearer to humans• Incomprehensible to computers

– Translator programs (assemblers)• Convert from assembly to machine

language• Example:

LOAD BASEPAYADD OVERPAYSTORE GROSSPAY

(addition of OVERPAY to BASEPAY and store result in GROSSPAY)

Page 21: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

21Machine Languages, Assembly Languages, and High-level

Languages3.3. High-level languages High-level languages

• Similar to everyday English, use common mathematical notations

• Single statements accomplish substantial tasks– Assembly language requires many

instructions to accomplish simple tasks• Translator programs (compilers)

– Convert from high level to machine language• Interpreter programs

– Directly execute high-level language programs (w/out compiling into machine language)

• Example:grossPay = basePay + overTimePay

Page 22: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

22

History of C and C++

• History of C– Evolved from two other programming languages

• BCPL and B– “Typeless” languages

– Dennis Ritchie (Bell Laboratories)• Added data typing, other features

– Development language of UNIX– Hardware independent

• Portable programs

– 1989: ANSI standard– 1990: ANSI and ISO standard published

• ANSI/ISO 9899: 1990

Page 23: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

23

History of C and C++

• History of C++ – Extension of C– Provides capabilities for object-oriented

programming (OOP)• Objects: reusable software components

– Model items in real world• Object-oriented programs

– Easy to understand, correct and modify

– Hybrid language• C-like style• Object-oriented style• Both

Page 24: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

24

C++ Standard Library

• C++ programs– Built from pieces called classes and functions

• C++ standard library– Rich collections of existing classes and functions

• You can use them instead of re-writing them!

• “Building block approach” to creating programs– “Software reuse”

• Learning C++ essentially is – Learning the language itself– Learning how to use its functions and classes

Page 25: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

25

Visual C++

• Visual C++– Microsoft’s implementation of C++

• Includes extensions• Microsoft Foundation Classes (MFC)• Common library

– GUI, graphics, networking, multithreading, … – Shared among Visual Basic, Visual C++, C#

• .NET platform– Web-based applications

• Distributed to great variety of devices– Cell phones, desktop computers

– Applications in disparate languages can communicate

Page 26: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

26

Other High-level Languages

• FORTRAN – Complex mathematical computations

• Scientific and engineering applications

• COBOL– COmmon Business Oriented Language – 1959: computer manufacturers, government

and industrial computer users– Precise and efficient manipulation of large

amounts of data• Commercial applications

• Pascal – Academic use

Page 27: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

27

Structured Programming

• Structured programming (1960s)– Disciplined approach to writing programs– Clear, easy to test and debug, and easy to

modify

• A program:– a set of procedures (or functions) on a set of

data values stored in the memory

• Emphasis on functions• Function:

– performs a sub task of the program– Abstraction of a behavior

• Separation of the behavior of a ‘thing’ from its representation or implementation

Page 28: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

28

The Key Software Trend: Object Technology

• Objects – Reusable software components that model real

world items– Meaningful software units

• Date objects, time objects, paycheck objects, invoice objects, audio objects, video objects, file objects, record objects, etc.

• Any noun can be represented as an object

– More understandable, better organized and easier to maintain than procedural programming

• OOP promotes thinking about software in a way that models the real world

• OOP supports good software engineering

Page 29: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

29

Object Oriented Programming, OOP

• Emphasis is on – the objects (data) and – how to manipulate them

• A program:– A network of objects with their attributes– Attributes = data and operations

Page 30: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

30

Object Oriented Programming, OOP

• Real-world problem: e.g., Automated banking system• A Bank Account = a real object

Class BankAccount {

Data: int number;

float balance; Operations: deposit(); wihtdraw(); }

• A class:– A definition of something (real thing)

• A class is an entity that defines the attributes of an object

• Attributes include data and operations (methods)

Page 31: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

31

Object Oriented Programming, OOP

• Class:– an entity that defines the attributes of an object

• Object: – An instance of a class

• A bank Account is a class– A particular bank account (yours) is an object

Class Hierarchy• Assume you want to define a new class similar

to a predefined class• Example:

– Bank-Account • Saving Account

International Account

Page 32: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

32

Basics of a Typical C++ Environment

• C++ systems consists of– Program-development environment– Language– C++ Standard Library

Page 33: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

33

Basics of a Typical C++ Environment

Phases of C++ Programs:

1. Edit (any text editor; VC++ and other packages provide built in editor (a source code file usually ends with .cpp, .cc, .c, etc.))

2. Preprocess (to include other files in the file being compiled, definition of symbolic constants, etc. all preprocessor directives start with #, e.g.: # include <filename>)

Loader

PrimaryMemory

Program is created inthe editor and storedon disk.

Preprocessor programprocesses the code.

Loader puts programin memory.

CPU takes eachinstruction andexecutes it, possiblystoring new datavalues as the programexecutes.

CompilerCompiler createsobject code and storesit on disk.

Linker links the objectcode with the libraries,creates a.out andstores it on disk

Editor

Preprocessor

Linker

 CPU

PrimaryMemory

.

.

.

.

.

.

.

.

.

.

.

.

Disk

Disk

Disk

Disk

Disk

Page 34: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

34

Basics of a Typical C++ Environment

Phases of C++ Programs:

3. Compile (generate machine language)

4. Link (functions and data defined elsewhere: e.g., standard or private libraries )

5. Load (load program from disk into memory)

6. Execute (CPU in charge)

Loader

PrimaryMemory

Program is created inthe editor and storedon disk.

Preprocessor programprocesses the code.

Loader puts programin memory.

CPU takes eachinstruction andexecutes it, possiblystoring new datavalues as the programexecutes.

CompilerCompiler createsobject code and storesit on disk.

Linker links the objectcode with the libraries,creates a.out andstores it on disk

Editor

Preprocessor

Linker

 CPU

PrimaryMemory

.

.

.

.

.

.

.

.

.

.

.

.

Disk

Disk

Disk

Disk

Disk

Page 35: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

35

Basics of a Typical C++ Environment

• Input/output– cin

• Standard input stream• Normally keyboard

– cout• Standard output stream• Normally computer screen

– cerr• Standard error stream• Display error messages

Page 36: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

36

Hardware Trends

• Capacities of computers– Approximately double every year or two– Memory used to execute programs– Amount of secondary storage

• Disk storage• Hold programs and data over long term

– Processor speeds• Speed at which computers execute programs

Page 37: MECH 215 Programming for Mech. and Ind. Eng. I

2003 Prentice Hall, Inc. All rights reserved.

37

General Notes About C++and This Book

• Book geared toward novice programmers– Stress programming clarity and simplicity

• Easier to understand, debug and test

– C and C++ are portable languages

• Portability– C and C++ programs can run on many different

computers


Recommended