27
Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Embed Size (px)

Citation preview

Page 1: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Copyright 2003 Scott/Jones Publishing

Brief Version of

Starting Out with C++, 4th Edition

Chapter 1

Introduction to Computers and Programming

Page 2: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 2 4th Ed. Home Page Appendices

Contents

1.3 Programs and Programming Languages

1.4 What Is a Program Made of?

1.5 Input, Processing, and Output

1.6 The Programming Process

1.7 Procedural and Object-Oriented Programming

Page 3: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 3 4th Ed. Home Page Appendices

Software – Programs That Run on a Computer

• Categories of software:

– Operating system:

• programs that manage the computer hardware and the programs that run on them. Ex: Windows, UNIX, Linux

– Application software:

• programs that provide services to the user. Ex: word processing, games, programs to solve specific problems

Page 4: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 4 4th Ed. Home Page Appendices

1.3 Programs and Programming Languages

• Program: a set of instructions to a computer to perform a task

• Programming Language: a language used to write programs

Page 5: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 5 4th Ed. Home Page Appendices

Programs and Programming Languages

• Types of languages:

– Low-level: used for communication with computer hardware directly. Often written in binary machine code (0’s/1’s) directly.

– High-level: closer to human language

Page 6: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 6 4th Ed. Home Page Appendices

Table 1-1Language Description

C A structured, general purpose language developed at Bell Labs. C offers

both high-level and low-level features.

C++ Based on the C language, C++ offers object-oriented features not found in C. Also invented at Bell Laboratories.

Java An object-oriented language invented at Sun Microsystems. Java may be used to develop programs that run over the Internet, in a web browser.

BASIC Beginners All-purpose Symbolic Instruction Code. A general programming language originally designed to be simple enough for beginners to learn.

COBOL Common Business-Oriented Language. A language designed for business applications.

RPG A programming language for business applications used primarily on IBM midrange and mainframe computers.

FORTRAN Formula Translator. A language designed for programming complex mathematical algorithms.

Pascal A structured, general purpose language designed primarily for teaching programming.

Page 7: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 7 4th Ed. Home Page Appendices

From a High-level Program to an Executable File

a) Code the program with a text editor or using an IDE.

b) Run preprocessor to convert source file directives to source code program statements.

c) Run compiler to convert source program statements into machine language instructions.

d) Run linker to connect hardware-specific code to machine instructions, producing an executable file.

- Steps b–d are often performed by a single command or button click.

- Errors detected at any step will prevent execution of following steps.

Page 8: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 8 4th Ed. Home Page Appendices

From a High-level Program to an Executable File

Source Code

Preprocessor

ModifiedSource Code

Compiler

Object Code

Linker

Executable Code

Page 9: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 9 4th Ed. Home Page Appendices

C and C++

• Both languages were developed at Bell Labs

• C was written by Dennis Ritchie in 1972

• C++ was written by Bjarne Stroustrup in the early 1980’s

• C++ extends the C language to permit object-oriented programming

Page 10: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 10 4th Ed. Home Page Appendices

1.4 What Is a Program Made Of?

• Common elements in programming languages:

– Keywords– Programmer-Defined Symbols– Operators– Punctuation– Syntax

Page 11: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 11 4th Ed. Home Page Appendices

Language Elements, Table 1-2Language Element

Description

Keywords Words that have a special meaning. Keywords may only be used for their intended purpose. AKA reserved words.

Programmer-Defined Symbols

Words or names defined by the programmer. They are symbolic names (identifiers) that refer to variables or programming routines.

Operators Operators perform operations on one or more operands. An operand is a piece of data, like a number.

Punctuation Punctuation characters that mark the beginning or ending of a statement, or separate items in a list.

Syntax

Rules that must be followed when construction a program. Syntax dictates how key words and operators may be used, and where punctuation symbols must appear.

Page 12: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 12 4th Ed. Home Page Appendices

Example Program#include <iostream>#include <string>using namespace std;

int main() {

string name;

cout << "What is your name? "; cin >> name;

cout << "Hello there, " << name;

return 0;}

Page 13: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 13 4th Ed. Home Page Appendices

Program 1-1// This program calculates gross pay.

#include <iostream>using namespace std;

int main(){

double hours, rate, pay;

cout << “How many hours did you work? ”;cin >> hours;

cout << “How much do you get paid per hour? ”;cin >> rate;

pay = hours * rate;cout << “You have earned $” << pay << endl;

return 0;}

Page 14: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 14 4th Ed. Home Page Appendices

Program Output

How many hours did you work? 10How much do you get paid per hour? 15

You have earned $150

Page 15: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 15 4th Ed. Home Page Appendices

Key Words

• Also known as reserved words

• Have a special meaning in C++

• Can not be used for another purpose

• Examples in program: – using, namespace, int

Page 16: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 16 4th Ed. Home Page Appendices

Programmer-Defined Symbols( Identifiers )

• Names made up by the programmer

• Not part of the C++ language

• Used to represent various things: – variables (memory locations), functions, etc.

• Example in program: – name, hours, rate, calcPay()

Page 17: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 17 4th Ed. Home Page Appendices

Operators

• Used to perform operations on data

• Many types of operators:Arithmetic: +, -, *, /, %

Assignment: =

• Examples in program: << stream insertion operator

>> stream extraction operator

Page 18: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 18 4th Ed. Home Page Appendices

Punctuation

• Characters that mark the end of a statement, or that separate items in a list

• Examples in program:– ; , ~ () []

Page 19: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 19 4th Ed. Home Page Appendices

Syntax

• The rules of grammar that must be followed when writing a program

• Controls the use of:

– key words

– operators

– programmer-defined symbols

– punctuation

Page 20: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 20 4th Ed. Home Page Appendices

Lines and Statements

cout << "How many hours did you work? ";

cout << "How many hours " << "did you work? ";

There are 2 statements above.• The first statement uses one line• the second statements spans two lines• the semicolon ( ; ) is the statement terminator

Page 21: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 21 4th Ed. Home Page Appendices

Variable Definitions

• Two types of information: – numbers and characters

• Numbers may be integers or floating-point•

• The statement below creates three variables in memory named hours, rate, and pay that each can store a floating-point number

• double hours, rate, pay;

Page 22: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 22 4th Ed. Home Page Appendices

1.5 Input, Processing, and Output

Three steps many programs perform:1) Gather input data:

- from keyboard- from files on disk drives

2) Process the input data

3) Generate output:- send it to the screen- write it to a file

Page 23: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 23 4th Ed. Home Page Appendices

1.5 Input, Processing, and Output

• Outputcout << "Enter your hours and rate " << "separated by a space: ";

• Input:cin >> hours; cin >> rate;

• Processing:pay = hours * rate;

• Outputcout << “You have earned $” << pay;

Page 24: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 24 4th Ed. Home Page Appendices

1.6 The Programming Process

• The programming process consists of several steps, which include:– Design– Code– Test– Debug– Document

Page 25: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 25 4th Ed. Home Page Appendices

1.6 The Programming Process

1. Clearly define what the program is to do.Inputs / Processing / Outputs

2. Develop logic using a flowchart or pseudocode.

3. Desk check for logical errors.4. Enter/Edit the code and compile it.5. Correct any errors found during compilation.

Repeat steps 4 and 5 as many times as necessary.

Page 26: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 26 4th Ed. Home Page Appendices

The Programming Process

6. Run the program with test data for input.

7. Correct any errors found while running the program. Repeat steps 4 through 7 as many times as necessary.

8. Validate the results of the program.

Page 27: Copyright 2003 Scott/Jones Publishing Brief Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming

Chapter 1 slide 27 4th Ed. Home Page Appendices

1.7 Procedural and Object-Oriented

Programming• Procedural programming: focus is on the

process. Procedures/functions are written to process data.

• Object-Oriented programming: focus is on objects, which contain data and the means to manipulate the data. Messages are sent to objects to perform operations.