1. Trang chủ
  2. » Công Nghệ Thông Tin

Introduction to Cplusplus

99 325 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 99
Dung lượng 536 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

 States…  data type for the return value  identifier for function  list of arguments between parenthesis none for this function... a See the movie and buy a soda, b See the movie,

Trang 1

Introduction to C++

Computer Science I

Trang 2

instrument of science….”

 Samuel Johnson

Trang 3

Q: What is C++

 C++ is a compiled, object-oriented language

 It is the “successor” to C, a procedural

language

 (the “++” is called the successor operator in C++)

 C was derived from a language called B which was in turn derived from BCPL

 C was developed in the 1970’s by Dennis

Ritchie of AT&T Bell Labs

 C++ was developed in the early 1980’s by

Bjarne Stroustrup of AT&T Bell Labs.

 Most of C is a subset of C++

Trang 4

People & Programs

 User: an individual who runs, or

executes, a program

 Programmer: an individual who creates,

or writes, a program

Trang 5

 Statements

 Or executable statements, representing

actions the computer will take on the user’s behalf

Trang 6

 Names for various entities used in a program; used for

 Variables : values that can change frequently

 Constants : values that never changes

 Functions : programming units that represents complex operations

 Parameters : values that change infrequently

Trang 7

Tells the compiler what to do before

compiling

 This one includes source code from another file

Trang 9

 States…

 data type for the return value

 identifier for function

 list of arguments between parenthesis (none for this

function)

Trang 10

 They represent the start and end

of the function

Trang 11

 Main body of function (or main part)

 “//”

represents the start of a comment

Trang 12

 specifies the value the function returns

 All (almost) declarations and

statements end with a semi-colon

“;”

Trang 13

do anything

!

Trang 14

 The identifier number is declared as being of data type int, or integer

Trang 15

 Note the direction of

“<<“

 endl represents

an line

Trang 16

 Note the direction of

“>>”

Trang 17

 You had better, since this will be the first

program you’ll try!

Trang 18

ns right now!

Trang 20

 When a variable is declared, space is

allocated in the computer’s memory for the variable

 Each data type requires a different number of bytes in memory for storing a variable

int - 2

float - 4

double - 8

char, bool - 1

Trang 21

 When a variable is

assigned a value,

the value is placed

into the variable’s

memory location

10

Total

Total = 2 + 3 + 5;

Trang 25

computed average

Do it!

 You have 20 minutes!

Trang 26

Computer Science I

Trang 28

Q: What is a function?

 It has

 arguments

 a name (identifier)

 a value it

returns

 a body int foo(int x)

{ int result;

result = x*x + 5*x + 7; return result;

}

Trang 29

Procedural Abstraction

Think “ Black Box” !

 When using a function, you only need

to be concerned with what it does, nothow it does it

 When writing a function, you need to

be concerned with the how

Trang 30

Example: Cube it!

int result;

result = x;

result = x*result; result = x*result; return result;

}

Trang 31

 Looks like a programming language

 Has all the structure of a programming language

 Has a verrrrrry loose syntax

Trang 32

 Example:

function foo (x)

result ← x 2 + 5x + 7 return result

 That’s it!

 Sloppy, ain’t it?

Trang 33

Decision Statements

Computer Science I

Trang 35

Decisions in Programs

 Without decision statements (or other dynamic control structures), programs are static

 Static programs do exactly the same things each time they are executed

 Dynamic programs do not

Trang 36

Boolean Algebra

 Based on values that are either True or

False

 True and False values are often

represented by 1’s and 0’s, respectively

Trang 37

Logical Operations: And

Trang 38

 Note: Also True

when A and B are

both True

T F

T T T

F T F

Trang 39

Logical Operations: Exercises

A = True, B = True, C = False

1 A ∨ B

2 A ∧ C

3 A ∨ B ∧ C

4 (A ∧ B) ∨ (A ∧ C)

Trang 40

Relational Operations

 A < B “A less than B”

 A > B “A greater than B”

 A = B “A equal to B”

 A ≤ B “A less than or equal to B”

“A not greater than B”

 A ≥ B “A greater than or equal to B”

“A not less than B”

 A ≠ B “A not equal to B”

“A less than or greater than B”

Trang 43

Try this!

Problem:

❏ You’d like to go see a movie.

❏ The movie costs $8.00, a soda costs $2.50 and a large popcorn costs $4.50.

❏ Based on the amount of money in your

pocket, determine whether you could

(a) See the movie and buy a soda,

(b) See the movie, and buy soda and

popcorn, or

(c) Stay home

Trang 45

 Cost of movie and soda

 Cost of movie, soda and popcorn

 Way to select one of the three options(that is, make a decision!)

Trang 47

How about a diagram?

 This is

called a

flowchart

Money < $15.00 Stay home

Movie & soda Movie, soda & popcornMoney < $10.50

Trang 48

How about a diagram?

 Boxes

represent

actions

Money < $15.00 Stay home

Movie & soda Movie, soda & popcornMoney < $10.50

Trang 49

How about a diagram?

Movie & soda Movie, soda & popcornMoney < $10.50

Trang 50

How about a diagram?

 Arrows

show flow

Money < $15.00 Stay home

Movie & soda Movie, soda & popcornMoney < $10.50

Trang 51

How about a diagram?

Movie & soda Movie, soda & popcornMoney < $10.50

Trang 52

How would I write this?

 Using Pseudocode

 Wait!

 What the CENSORED is Pseudocode?

Trang 53

 Looks like a programming language

 Has all the structure of a programming language

 Has a verrrrrry loose syntax

Trang 54

 Example:

get x result <- x 2 + 5x + 7 print result

 That’s it!

 Sloppy, ain’t it?

Trang 55

One more time!

 Pseudocode

If (Money < $10.50) then

Stay homeelse If (Money < $15.00) then

Movie, sodaelse Movie, soda, popcorn

Trang 56

How would I write this?

 First, we need to decide how to

organize our solution

 Should we “hard code” the costs of the movie, soda and popcorn into the

algorithm?

 Should we input these values?

 Let’s take another look at that problem!

Trang 57

How would I write this?

 The problem

statement tells us

the individual costs

 So, let’s assume

they’re fixed or

constant

 No need to ask the

user for them

Problem:

❏ You’d like to go see a movie.

❏ The movie costs $8.00, a soda costs $2.50 and a large

popcorn costs $4.50.

❏ Based on the amount of money

in your pocket, determine whether you could

(a) See the movie and buy a soda,

(b) See the movie, and buy soda and popcorn, or

(c) Stay home

Trang 58

How would I write this?

 Another question: Should we pre-compute

the cost of each option?

 Or, should we let the program do this?

 Since we’ve already stated that the item costs are fixed, it would seem logical to pre-

compute the cost of each option

 Movie: $8.00

 Movie & soda: $10.50

 All three: $15.00

Trang 59

How would I write this?

 Next, we need to make sure we have a complete algorithm

Input Money

If (Money < $10.50) then

Display “Stay home.”

else If (Money < $15.00) then

Display “Go to a movie;buy a soda.” else Display “Go to a movie; buy a

soda and popcorn.”

 Almost done!

Trang 60

How would I write this?

 Determine how we wish to organize our program

 Do we want one function?

 Or, should we create a few functions?

 Let’s two functions: One to input Money from the user

 And a second to determine the outcome

Trang 61

How would I write this?

 Here’s the prototypes for the functionsint getMoney()

void showResults(int myMoney)

Trang 64

Multiway Branching

 Consider the following problem:

Each year, a local middle school

requires students to purchase supplies based on their grade level 6th graders need pencils and five notebooks 7th

graders also need a calculator 8th

graders add to this a 3-ring binder with loose leaf paper

Trang 65

Multiway Branching

 We could use a nested If statement to handle this, but there is an alternative

 Whenever we need to represent a decision

step, with n possible outcomes, where

 the outcomes form subsets of each other,

and/or

 the outcomes are chosen based upon

unique scalar values for a control

expression,

 we can use a Case (switch) structure

Trang 66

Multiway Branching

 Case

When Grade = 8th

3-ring binderloose leaf paperWhen Grade = 7th

calculatorWhen Grade = 6th

pencils

5 notebooks

Trang 67

 When the switch is encountered, control jumps to the

matching case statement and continues until either a break is found or the end of the switch

Trang 68

Multiway Branching

 Here’s an example with a few break’s

cout << “Your lunch period comes “;

Trang 69

Exercise :

 Create a program that will inform the user which advisor they should go to based on their major code number

Well? Get started!

Trang 70

Computer Science I

Trang 71

Q: What is a Loop?

 A control structure that allows for a sequence of steps to be repeated a certain number of times

 This sequence of steps is called the

body of the loop

Trang 75

repeated as long

as the condition

is true

Trang 76

is bypassed, and flow continues with the next part of the

algorithm

Trang 77

Example: Sequential search

k ← 0 found ← False while (k<size) ∧ ( ¬ found)

do if A[k] = target

then found ←

True

else k = k + 1

Trang 78

Example: Sequential search

Trang 79

 Strings are arrays of characters

 How would you determine if a string fragment is part of a larger string?

(needle in a haystack?)

Trang 80

Computer Science I

Trang 81

Q: What is an array?

 An array is a data structure consisting

of one or more indexed members

 An array is like a row of mailboxes at the post office

 Each box is numbered in sequence

(indices), and …

 Each box contains the same type of stuff (datatype)

Trang 82

An array could be drawn like

Trang 83

An array could be drawn like

Trang 84

An array could be drawn like

Trang 85

In pseudocode we use …

 X[j]

Where X is the name of the array

(identifier), and …

j is an index indicating which position

in the array we wish to address

Trang 86

An array is declared by …

int X[10];

Where int is the common datatype for all

elements in the array,

elements are in the array

 Indices for a C++ array always begin with 0

Trang 87

Example: Student Grades

// Declare array

double stGrades[7];

: // Assign value to array element stGrades[5] = 87;

: // Display array element

cout << stGrades[3] << endl;

Trang 88

 Create a program that will ask the user for three (3) numbers, determine the average, and then display the original numbers and the average

 Hint: You might wish to use a loop as well as an array!

Trang 89

Computer Science I

Trang 90

What is a string?

 A string is a sequence of characters

 Example :

nc9*hNB98B&^v*&G

 Blank spaces are characters

 Each character requires one byte of storage

in memory

 Each character is represented by a one byte character code, usually an ASCII code

Trang 92

length represents the length of the

string, or how many characters are in the sequence

Trang 94

 cout is an output stream

 cin is an input stream

Trang 95

How about file I/O?

Input stream declaration

Output stream declaration

Trang 96

How about file I/O?

Stream use

Closes files

Trang 97

What if a file doesn’t exist?

Trang 98

How about formatting?

showpos right/left

Number of decimal places Number of digits

Trang 99

More member functions

 get()

 put()

 eof()

Ngày đăng: 23/10/2014, 15:07

TỪ KHÓA LIÊN QUAN

w