intro to c++ lecture 2. machine language ada pascal fortran cobol assembly language smalltalk visual...

101
Intro to C++ Lecture 2

Upload: alexina-rose-stafford

Post on 19-Jan-2016

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Intro to C++Lecture 2

Page 2: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Machine Language

Ada

PascalFortranCOBOL

Assembly Language

Smalltalk

Visual BasicBASIC

JavaC and C++

Programming Languages…

Programming languages are artificial languages created to tell the computer what to do

They consist of vocabulary and a set of rules (grammar/syntax) to write programs

The software development life cycle (SDLC) is an organized method of software development

Page 3: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

C++ Predecessors

Early procedural languages included FORTRAN: Formula Translation ALGOL: Algorithmic Language COBOL: Common Business Oriented Language BASIC: Beginners All-purpose Symbolic

Instruction Code Pascal C Smalltalk, Simula, etc. were the object-oriented

predecessors of C++

Page 4: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

The Evolution

of Programming Languages

Third-Generation and Fourth-Generation (High-Level) Languages

Page 5: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

First-Generation Languages Machine language:

Consists of binary numbers (0s and 1s)

Is the earliest programming language

Is the only language the computer understands without translation It is machine dependent;

each family of processors has its own machine language

Page 6: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Second-Generation Languages Assembly language:

Resembles machine language

Is a low-level language Uses brief abbreviations for

program instructions. Abbreviations are called

mnemonics A program is written in

source code (text file) and translated into machine language by an assembler

Page 7: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Third-Generation Languages

Procedural languages: Are high-level languages that tell the computer

what to do and how to do it Create programs at a high level of abstraction Are easier to read, write, and maintain than

machine and assembly languages Use a compiler or interpreter to translate code Fortran and COBOL are third-generation

languages

Page 8: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Compilers and Interpreters

An interpreter translates source code one line at a time and executes the instruction

A compiler is a program that changes source code to object code, all in one shot

Example of an interpreted language: BASIC; a compiled language: C

Page 9: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

The Great Software Crisis of the 60’s Spaghetti Code and the Great Software Crisis:

goto statements resulted in programs that were difficult to follow

This made them difficult to modify, maintain, and even develop!

This problem led to the software crisis of the 1960s Programs were not ready on time Programs exceeded their budgets Programs contained too many errors Customers were not satisfied Management was also very frustrated!

Page 10: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Structured Programming to the rescue…

Structured programming languages: Were developed to improve software

development Include Algol and Pascal Are well-structured and organized Forbid the use of goto statements Use three fundamental control structures

Sequence, Selection, Repetition

Page 11: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Modular Programming Languages developed in the 70’s…

Modular programming languages: Were developed because of problems in

structured programming languages Are used to create programs that are divided

into separate modules Each module carries out a special function

Require specified input to produce specified output

Page 12: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Fourth-Generation Languages Fourth-generation languages are non-procedural, high-level

specification languages They do not force programmers to follow procedures to produce

results (they’re very close to English, sometimes) Types of fourth-generation languages include:

Report generators Languages for printing database reports (generate a program to generate

the report) Query languages

Languages for getting information out of databases (generate a program to process a query or generate a form)

Also include visually-oriented languages More ambitious 4GL environments attempt to automatically generate whole

systems from the outputs of CASE tools, specifications of screens and reports, and possibly also the specification of some additional processing logic, including data flow diagrams, entity relationship diagrams, entity life history diagrams

Fifth-generation languages These include artificial intelligence and neural networks oriented

languages

Page 13: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

SKIP: Object-Oriented Programming

Object-oriented programming (OOP): Relies on component reusability

The ability to produce program modules that perform a specific task

Eliminates the distinction between programs and data

Uses objects that contain data and procedures

Page 14: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

SKIP: Objects Objects are units of information that contain data as

well as methods that process and manipulate the data Classes of objects:

Hierarchy or category of objects Objects at the top of the category are broader in

scope than the subclass objects Objects near the bottom are narrower in scope

Inheritance refers to an object’s capacity to “pass on” its characteristics to its subclasses

Page 15: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Programming Language Classification Computer languages (not just programming languages!) can be classified into two

general categories: Imperative and Declarative Imperative languages include:

Generic/General Programming Languages They allow user-defined data types, data abstraction, polymorphism, etc.

Procedural Programming Languages Object-Oriented Programming Languages

Declarative languages include: Functional Programming Languages (e.g., Haskell, Scheme, etc.) Logical Programming Languages (e.g., Prolog) Even Markup Languages like HTML

They’re not programming languages because they don’t allow for the three fundamental control structures but they do declare what pages should look like, etc.

Procedural Programming (structured and modular): An important methodology for dealing with complexity is structured programming Structured Programming requires well-organized, structured code that uses the three

fundamental control structures (sequence, selection, repetition) Complex tasks need to be broken down into simpler subtasks this is modular

programming Procedural programming reduces complex problems to collections of simple,

interconnected modules – it combines structured and modular programming! It is up to the programmer to implement procedural programming

Page 16: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

3GL Programming Languages

Most high-level programming languages can be categorized into one of three main categories: Procedural Languages (FORTRAN, BASIC,

COBOL, Pascal, etc.) Object-Oriented Languages (Smalltalk, Effiel,

etc.) Hybrid Languages allow both OO and

Procedural (such as C++)

Page 17: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

3GL: Procedure-oriented (structured) languages Programmers concentrate on the procedures used in the program

Procedure: a logically consistent set of instructions which is used to produce one specific result

3GL: Object-oriented languages Items are represented using self-contained objects

Can be used to create programs for graphical windows environments

4GLs: fourth-generation languages Permit users to access and format information without the need

for writing any procedural code Use high-level English-like instructions to specify what to do, not

how to do it .

3GL vs. 4GL

Page 18: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Procedural Programming Basics

The purpose of most application programs is to process data to produce specific results

Basic procedural operations.

Page 19: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What Can a Procedural Program Do?

A linear, procedural program is structured to instruct a computer to: Read Input Calculate Store data Write Output Work in a sequential progression (Sequence) Compare and branch (Selection) Iterate or Loop (Repetition)

Page 20: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Advantages of Procedural Programming

Easier to test and debug Structured walkthroughs of the code

“goto-less” I.e., they’re well-structured and include the three

fundamental control structures Standard method for solving a problem A single program can be written by more than one

programmer By dividing the program up into modules

Programs can share routines (again, because of modularization)

Page 21: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What’s structured programming? A Structure is One or More Instructions

(Flowchart Symbols) Combined According to Rules E.g., only one point of entry and exit

Structured programming is a set of rules that prescribe good style habits for programmer. An organized, well structured code Easily sharable; easy to debug and test Requires shorter time to develop, test, and update

The key idea is that any numerical algorithm can be composed using the three fundamental control structures: Sequence, selection, and repetition

Page 22: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What Are the Rules for Writing a Structured Program?

3 basic control structures: Sequence Selection (decision) Repetition (looping or iteration)

Page 23: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Modularization

Modular Program: a program consisting of interrelated segments arranged in a logical and understandable form Easier to develop, correct, and modify than other

kinds of programs Module: a small segment which is designed to

perform a specific task A group of modules is used to construct a

modular program

Page 24: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Modular Programming Behind most software development over the last century Encourages subroutines Large programs are divided by functional parts into

subroutines or modules Go to instructions only permitted within subroutines Three objectives when creating routines:

Smaller, simpler routines Breakup complex routines into simple functions

Strong cohesion Routine does one function such as printing

• Instructions are closely related Loose coupling

Non-existent or weak connection between routines• One routine does not depend on other routines

Page 25: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

SUBPROGRAM (function)

SUBPROGRAM1 . . .

SUBPROGRAM1 a meaningful collection of SEQUENCE, SELECTION, REPETITION (LOOP), or SUBPROGRAMS

Page 26: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Introduction to C++

Modules in C++ can be classes or functions Function: accepts an input and produces an

output by processing the input in some fashion A function’s processing is encapsulated and

hidden within the function

Page 27: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

A Multiplying Function

Page 28: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Classes and Functions

Function: encapsulates a set of operations, while a class encapsulates data plus one or more sets of operations

Class: contains both data and functions used to manipulate the data

Identifier: a name given to an element of the language, such as a class or function

Page 29: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

The Software Development Life Cycle (SDLC)

The SDLC was introduced in the 1970s to address problems in creating programs

It provides an organized plan for breaking down the task of program development into manageable parts

Five phases of the SDLC:

1. Defining the problem

2. Designing the program

3. Coding the program

4. Testing, debugging, and formalizing the program

5. Implementing and maintaining the program

Page 30: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Phase 1: Defining the Problem The first step in program development What do you want to solve? What is the objective and question? Get the requirements from the client (possibly other software

engineers) Systems analysts provide program specifications (specs) to the

programmers The specs define:

Input data Processing Output

• Including the appearance of the user interface Software engineers need clear specifications (as opposed to

requirements): Clear, specific statement of goal Expected output Expected input

Page 31: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Phase 2: Designing the Program Programmers create the program’s design

Top-down design focuses on the program’s main goal (main routine), then breaks the program into manageable components (subroutines/modules)

Control structures are used to see how each subroutine will do its job

Makes programming easier to develop, debug, and maintain

Developing an algorithm, which is a step-by-step description of how to arrive at a solution

Program design tools: Structure charts – show the top-down design Flow charts – show the logic of program Pseudocode – alternative to flow charts

Page 32: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Structured Design Structured programming implements structured design Control structures are logical constructs that specify

how the instructions in a program are to be executed Three fundamental types of control structures:

Sequence control structure – Instructions are executed in the order in which they appear

Selection control structures – The program branches to different instructions depending on whether a condition is met; IF…THEN…ELSE

Repetition control structure – The program repeats the same instructions over and over; DO-WHILE and DO-UNTIL

Page 33: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Structure Chart

Flowchart

Structure Chart and Flowchart

Page 34: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Phase 3: Coding the Program

Coding requires the translation of the algorithm into specific program instructions

An appropriate programming language is chosen, and the code is typed according to its syntax rules

Documentation is created for future use Best if the documentation is done in-line using a tool like

JavaDoc, Doxygen, etc. The variable names and definitions, a description of the

files needed, and the layout of the output are produced A user manual is developed to explain how the

program works

Page 35: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Phase 4: Testing and Debugging the Program

Testing and debugging eliminate all errors Syntax and logic errors are corrected Debugging is the process of eliminating errors

Page 36: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Phase 5: Implementing and Maintaining the Program

The program is: Tested by users Thoroughly documented Maintained and evaluated regularly

Page 37: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Algorithms

Algorithm: A step-by-step sequence of instructions that must terminate Pseudocode

Use of English-like phrases to describe an algorithm Formula

Use of mathematical equations to describe an algorithm

Flowchart Use of diagrams that employ symbols to describe an

algorithm

Page 38: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Program Design Process

AL-KHOWARIZMI

Grace M. HopperFirst “Bug”

Algorithm Design(underlying logic of program)

Program Composition

Debug & test(error free & reliable)

Documentation

Maintenance

75% of costs!

Page 39: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What’s an Algorithm?

A predetermined series of instructions for carrying out a task in a finite number of steps I.e., Baking A Cake!

Two commonly used tools to help document the program logic (the algorithm): Flowcharts and Pseudocode.

Generally, Flowcharts work well for small problems but Pseudocode is used for larger problems

Page 40: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

How to make a plan …?

Do a hand calculation (brainstorming!)

Make a flowchart

Visio or dia

Write the Pseudocode

Page 41: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Flowchart: Symbolic Representation of Algorithms

Flowchart: A graphic representation of an algorithm often used in the design phase of programming to work out

the logical flow of a program Uses symbols to represent each logical step of the

algorithm. The order in which the steps are carried out is indicated by

connecting “flow lines” Use a few, basic flowchart symbols Follow UML (Unified Modeling Language)

Standard defined by OMG group: http://www.omg.org/technology/documents/formal/uml.htm

Page 42: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Basic Flowchart Symbols Some basic symbols:

Page 43: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Basic Control Structures a sequence is a series of statements that execute one after

another

selection (branch) is used to execute different statements depending on certain conditions

repetition (looping) is used to repeat statements while certain conditions are met.

a subprogram (module) is used to break the program into smaller units

Page 44: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

SEQUENCE

Can contain any symbol except for the decision or loop symbol

Steps are executed in sequence with no instruction changing the order

Flow lines connect each instruction

Page 45: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

SELECTION (branch)

Page 46: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

REPETITION (loop)

Page 47: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Logical steps

Written in English and symbols, terms, etc.

Indenting for logical structures

begin some game display instructions pick a number between 1 and 100

repeat turn until number is guessed or seven turns are completed input guess from user respond to guess end repeat

end some game display end message

Looks like BASIC (Chris Clark)

Pseudocode

Page 48: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Pseudocode for three control constructs

Page 49: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What is Pseudocode? An alternative to flowcharting Represents logic in an English-like manner Rules

Avoid the use of words peculiar to a particular programming language

Indent lines to make the pseudocode easy to read and understand

Show key words (Move, Write, Read) in a different color, font, or capitalized

Punctuation is optional End every If with EndIf Begin every loop with a Loop instruction; End every loop with

EndLoop Main routine is to be shown first Terminate all routines with an End instruction (e.g., EndMain)

Page 50: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Write an algorithm in pseudocode that finds the average of two numbers

Example 1

Page 51: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

StartAverageOfTwo0. Input: Two numbers1. Add the two numbers2. Divide the result by 23. Return the result by step 2

End

SKIP: Average of two

Page 52: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Write an algorithm to change a numeric grade to a pass/no pass grade.

Example 2

Page 53: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Pass/NoPassGradeInput: One number

1. if (the number is greater than or equal to 70)then 1.1 Set the grade to “pass”else 1.2 Set the grade to “nopass”End if

2. Return the gradeEnd

SKIP: Pass/No Pass Grade

Page 54: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Flowchart & Pseudocode

Start

Sum=0Count = 0

InputGrade

More grades?

Sum = Sum + GradeCount = Count + 1

Average = Sum/Count

Stop

No

Yes

FlowchartPseudocode

BEGIN Average Grade sum=0 count = 0 DO INPUT grade IF grade < 0 EXIT sum = sum + grade count = count +1 END DO IF count > 0 THEN average = sum/count ELSE average = 0 END IFEND Average Grade

Page 55: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Your first program in C++

hello.cpp compiler a.out

Writing a program in Linux involves three steps…

Page 56: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Details of a Typical C++ EnvironmentPhases of C++ Programs:

1. Edit

2. Preprocess

3. Compile

4. Link

5. Load & Execute 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 57: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Code (write) the program

Carefully enter the code

Remember most C++ statements end with a ;

Commands are in lower case

Page 58: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

DEMO: 1. Edit

Start up an editor (emacs) to edit a file called hello.cpp Type the following into the file:

#include <iostream>

using namespace std;

int main(void){

cout << “Hello World” << endl;

return 0;

}

Page 59: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

DEMO: 2. Compile

Compile your program g++ hello.cpp

This tells the computer to call the compiler to create the program

The compiler is a program that takes the code that you write and translates it into machine language

Machine code is made up of many simple instructions composed of 0’s and 1’s

Page 60: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

DEMO: 3. Execute

You should now have a file called a.out inside your directory This file is called an executable a.out holds the binary version of your code

(the computer doesn’t understand words, just 0’s and 1’s)

To run the program type: ./a.out

Page 61: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What does the code mean?

#include <iostream> using namespace std; Allows us to use functions that other people

have written to facilitate input and output Processed by the pre-processor (it’s called a

pre-processor directive) Literally copies and pastes the libraries of

functions that others wrote This is what makes Java and MFC so powerful!

Page 62: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What does the code mean?

int main(void) Remember, every C++ program is made of one or

more functions A function is a piece of code that accomplishes one

specific task Every executable C++ program has one function

called main() This is where execution (the actual running) of the

program (i.e., the computer instructions) starts…

Page 63: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What does the code mean?

int main(void) When you type ./a.out to run the program you

are essentially telling the computer to call this main() functionCalling a function means to execute the code in

the function

Page 64: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What does the code mean?

int main(void) The int is the return type of the function.

In other words when it finishes what kind of result does it give back. A whole number? A character?

int means a whole number. We want to give this number back to tell us if the

program was sucessfully completed

Page 65: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What does the code mean?

int main(void) The void in the brackets is the parameter list

for this function The parameter list indicates what this function

needs to be given from the outset for it to work void indicates that we give nothing to this

function.

Page 66: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What does the code mean?

cout << “Hello World” << endl; This line tells the computer to print something to

the screen If you want to print something to the screen you

use the cout function The endl adds an “end line” and flushes the

buffer

Page 67: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What does the code mean?

return 0; This is the last line in our program It basically means that the program is now done return means to go back to where the

program was called from 0 is used to indicate that it was successfully

completed (a general convention)

Page 68: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

What does the code mean?

Note also that the code of main() is contained within the { }

These {} group code together. In this case it tells us that all the code between them

are part of the main function

#include <iostream>using namespace std;int main(void){ cout << “Hello World” << endl; return 0;}

Page 69: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Some Important Terms A source program (.cpp file type) consists of the program statements

comprising a C++ or other programming language program. An object program (.obj file type) is the result of compiling a source

program. Header files (.h file type) contain constant, variable, and function

declarations needed by a program. An executable program is a program that can be run by a computer. Linking adds code from libraries to your file. Collects the object code

from all files in the workspace and puts them into one executable program.

A compiler is a program that translates a source program into an object program.

An interpreter is a program that translates individual source program statements, one at a time, into executable statements. Each statement is executed immediately after translation.

Page 70: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Compiling / Building

(.cpp file)source source codecode compiler

(.obj file)object codeobject code

(.h files)

linked to libraries

executable executable filefile

object code from other source files

Page 71: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Error, error… does not compute! Syntax Errors – Typing Errors

Errors in spelling and grammar (syntax). “Doag. Bites, Man”

You can use the compiler or interpreter to uncover syntax errors.

You must have a good working knowledge of error messages to discover the cause of the error.

Semantic Errors – Logic or Meaning Errors Errors that indicate the logic used when coding the program

failed to solve the problem. “Man bites dog.”

You do not get error messages with logic errors. Your only clue to the existence of logic errors is the

production of wrong solutions. Run-time Errors (Exceptions)

Code does something illegal when it is run (hence runtime) E.g., divide by zero

Syntax refers to the structure of a program and the rules about that structure

Page 72: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Procedural programming produced significant improvements in software quality and development time…

Page 73: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

So…Why are Programmers Now Using Object-Oriented Programming?

Increasing dependence on information processing is creating a crisis

Volume of information is increasing faster than the ability to create software

Programmers cannot generate software to keep pace with the potential of new hardware

Page 74: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Some Programming Issues are Not Adequately Addressed by Procedural Programming

It is rarely possible to anticipate the design of a completed system before it’s actually implemented Systems are works in progress

Development of GUIs is very complicated in traditional procedure-oriented programming languages

Sharing data across routines is difficult and error-prone Focus of structured programming is on the modularity and procedures Data interactions are ignored Allowing modules to share all their data and interact freely creates subroutines that

are very dependent on each other--therefore, we don’t have independent routines

Page 75: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

So, What’s the Answer to the Data Sharing Problem?

Modularize the data along with the procedures by giving each subroutine its own local data that it alone can read and write

INFORMATION HIDING

Page 76: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Information Hiding Allows the Programmer to Work with Modules or Procedures Developed by Others at an Abstract Level Abstraction Internal details are hidden from the user Data and procedures are not exposed to

procedures not needing to know them

Page 77: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Another Advantage of Object-Oriented Programming is Reusability

Building a house Electrical system Plumbing system Heating/air

conditioning system

Objects in Windows Buttons Menus Fields

Page 78: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

How Procedural Programming Looks at a Problem

Procedures Data acted upon by the procedures are

maintained separately from the procedures Data assumes a secondary role!

Program

Procedure

Procedure Data

Page 79: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

How Object-Oriented Programming Looks at a Problem?

Objects Data Methods Scope

Methods surround the data and protect data from other objects

Encapsulation and Information Hiding!

Met

hod Method

Method

Met

hod

Data

OBJECT

Page 80: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Objects Objects are things (nouns) Objects have attributes (properties)

Adjectives that describe an object Objects have methods (behaviors)

Verbs that specify what the object can do Events are used to trigger behaviors Objects interact with each other by passing

messages amongst themselves

Page 81: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Why Do We Still Write Procedural Programs?

Way of breaking a program into routines Object-oriented programming works at a

higher level of abstraction than procedural programming does

After objects have been identified, methods are designed using procedural techniques We are concentrating on methods or

procedures

Page 82: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…
Page 83: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Sample Cobol program

Common Business-Oriented Language (COBOL) COBOL:

The earliest (1959) high-level language

The most widely used business language

A proven way to do accounting, inventory, billing, and payroll

Requires programmers to explain what the program is doing at each step

Page 84: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Sample Fortran program

Formula Translator (Fortran) Fortran:

Began in the 1950s Is suited to scientific, mathematical, and engineering applications

Is used to solve complex equations

Features simplicity, economy, and ease of use

Page 85: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Sample Ada program

Ada Ada:

Named after Augusta Ada Byron

Incorporates modular programming

The required language for the U.S. Defense Department

Suitable for control of real-time systems (missiles)

Page 86: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Sample BASIC program

Beginner’s All-Purpose Symbolic Instruction Code (BASIC)

BASIC: An easy-to-use language available on personal

computers Widely taught in schools as a beginner’s programming

language Designed as an interpreted language

Page 87: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Visual Basic (VB)

Visual Basic: Is widely used in program development packagesUses event-driven programmingEnables the programmer to develop an application by using on-screen graphical user interfaces

Sample Visual Basic

Page 88: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Sample Pascal programPascal

Pascal: Is named after Blaise Pascal Encourages programmers to write well-structured programs Widely accepted as a teaching language Has been updated to reflect new approaches to

programming

Page 89: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Sample C program

C C:

Was developed by AT&T’s Bell Labs in the 1970s

Combines high-level programming language with assembly language

Programmers manipulate bits of data within a processing unit

Difficult to learn and programming is time consuming

Page 90: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Sample Smalltalk program

Smalltalk

Smalltalk: Developed in the 1970s by

Xerox Corp “100% pure” object-

oriented programming language

Not often chosen for software development

Page 91: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

C++ C++:

Incorporates object-oriented features

Is widely used for professional program development

Sample C++ program

Page 92: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Java

Java: Developed by Sun Microsystems An object-oriented, high-level

programming language with a twist First true cross-platform

programming language Gained acceptance faster than any

other programming language A simplified version of C++

Page 93: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Java

Java, continued : Java is designed to run on any computer platform Java Virtual Machine enables cross-platform use Java applets or small programs are downloaded

to computers through networks Weaknesses include:

The security risk in downloading applets The speed in running the programs

Page 94: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Sample Java Program

Page 95: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Web-Based Languages Markup languages:

Hypertext markup language (HTML) sets the attributes of text and objects within a Web page

Extensible markup language (XML) is used for sharing data and objects in a Web environment

Scripting languages: VBScript is used to write short programs (scripts)

that are embedded in Web pages JavaScript is used to write scripts on Web pages

Visual Studio .NET: Used for the development of scripts and programs

that are accessible from the Web

Page 96: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

The Programming Life Cycle

Define the problem

Make or buy software?

Design the program by making an algorithm

Code (write) the program

Document the program

Compile and execute the program

Syntax and run time errors

Test (debug) the program

Logic errors

Page 97: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Summary• A programming language is an artificial language

consisting of a vocabulary and a set of rules• Machine language is the lowest-level programming

language• Assembly language contains symbols for

programming instructions• Third-generation (high-level) languages require

programmers to specify the procedures to be followed• Object-oriented languages combine procedures and

data

Page 98: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Summary, continued• The SDLC’s six phases are:

• Defining the program• Designing the program• Coding the program• Testing, debugging, and formalizing the program• Implementing and maintaining the program

• Top-down programming makes programs easier to debug and maintain

• Debugging requires finding and correcting syntax errors and logic errors

Page 99: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Flowcharts for the three constructs

Page 100: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Alternative Control Structure Flowcharts

Sequence Selection Repetition (Looping)

Page 101: Intro to C++ Lecture 2. Machine Language Ada Pascal Fortran COBOL Assembly Language Smalltalk Visual Basic BASIC JavaC and C++ Programming Languages…

Pseudocode! What is pseudocode?

Pseudocode is basically short, English phrases used to explain specific tasks within a program's algorithm. Pseudocode should not include keywords in any specific computer languages. Indentation can be used to show the logic in pseudocode as well.

Why is pseudocode necessary? Writing pseudocode WILL save you time later during the construction & testing phase of a program's

development let’s you “think out” the program before you code it. How do I write pseudocode?

Consists mainly of executable statements

Original Program Specification: Write a program that obtains two integer numbers from the user. It will print out the sum of those numbers.

Variables required (names and types): int1: (integer) to store first integerint2: (integer) to store the second integersum: (integer) to store the sum of the numbers

Pseudocode: Prompt the user to enter the first integer int1Prompt the user to enter a second integer int2Compute the sum of the two user inputs sum = int1 + int2Display an output prompt that explains the answerDisplay the result

If you can’t write it in pseudocode, you won’t be able to write it in C++!