Programming Abstraction in C++ Eric S. Roberts and Julie Zelenski - - PowerPoint PPT Presentation

programming abstraction in c
SMART_READER_LITE
LIVE PREVIEW

Programming Abstraction in C++ Eric S. Roberts and Julie Zelenski - - PowerPoint PPT Presentation

Structure of a C++ program Variables, values, and types Statements Functions Programming Abstraction in C++ Eric S. Roberts and Julie Zelenski Stanford University 2010. Structure of a C++ program Variables, values, and types Statements


slide-1
SLIDE 1

Structure of a C++ program Variables, values, and types Statements Functions

Programming Abstraction in C++

Eric S. Roberts and Julie Zelenski

Stanford University 2010.

slide-2
SLIDE 2

Structure of a C++ program Variables, values, and types Statements Functions

Chapter 1. An Overview of C++

slide-3
SLIDE 3

Structure of a C++ program Variables, values, and types Statements Functions

Chapter 1. An Overview of C++

You should read Chapter 1. We won’t teach the basic syntax and constructs. We’ll just highlight some of the common programming idioms and C++ characteristics.

slide-4
SLIDE 4

Structure of a C++ program Variables, values, and types Statements Functions

Outline

1

Structure of a C++ program

2

Variables, values, and types

3

Statements

4

Functions

slide-5
SLIDE 5

Structure of a C++ program Variables, values, and types Statements Functions

Outline

1

Structure of a C++ program

2

Variables, values, and types

3

Statements

4

Functions

slide-6
SLIDE 6

Structure of a C++ program Variables, values, and types Statements Functions

Structure of a C++ program

Comments Program: Operation of the program as a whole. Function: What the function does. /* multiline * comments */ // single line comments

slide-7
SLIDE 7

Structure of a C++ program Variables, values, and types Statements Functions

Structure of a C++ program (cont.)

Library inclusions #include "private library" #include <system library> header files containing definitions.

slide-8
SLIDE 8

Structure of a C++ program Variables, values, and types Statements Functions

Structure of a C++ program (cont.)

Library inclusions #include "private library" #include <system library> header files containing definitions. constant definitions

slide-9
SLIDE 9

Structure of a C++ program Variables, values, and types Statements Functions

Structure of a C++ program (cont.)

Library inclusions #include "private library" #include <system library> header files containing definitions. constant definitions function prototypes

slide-10
SLIDE 10

Structure of a C++ program Variables, values, and types Statements Functions

Structure of a C++ program (cont.)

Library inclusions #include "private library" #include <system library> header files containing definitions. constant definitions function prototypes main

slide-11
SLIDE 11

Structure of a C++ program Variables, values, and types Statements Functions

Structure of a C++ program (cont.)

Library inclusions #include "private library" #include <system library> header files containing definitions. constant definitions function prototypes main function definitions

slide-12
SLIDE 12

Structure of a C++ program Variables, values, and types Statements Functions

Example

Program comments

/* * File: powertab.cpp * ------------------ * This program generates a table comparing * values of the functions nˆ2 and 2ˆn. */

slide-13
SLIDE 13

Structure of a C++ program Variables, values, and types Statements Functions

Example

Program comments

/* * File: powertab.cpp * ------------------ * This program generates a table comparing * values of the functions nˆ2 and 2ˆn. */

Library inclusions

#include "genlib.h" #include <iostream> #include <iomanip>

slide-14
SLIDE 14

Structure of a C++ program Variables, values, and types Statements Functions

Example (cont.)

section comment

/* Constants * --------- * LOWER_LIMIT -- starting value for the table * UPPER_LIMIT -- final value for the table */

slide-15
SLIDE 15

Structure of a C++ program Variables, values, and types Statements Functions

Example (cont.)

section comment

/* Constants * --------- * LOWER_LIMIT -- starting value for the table * UPPER_LIMIT -- final value for the table */

constant definitions

const int LOWER_LIMIT = 0; const int UPPER_LIMIT = 12;

slide-16
SLIDE 16

Structure of a C++ program Variables, values, and types Statements Functions

Example (cont.)

function prototype

/* Private function prototypes */ int RaiseIntToPower(int n, int k);

slide-17
SLIDE 17

Structure of a C++ program Variables, values, and types Statements Functions

Example (cont.)

main program

int main() { cout << " | 2 | N " << endl; cout << " N | N | 2 " << endl; cout << "----|-----|------" << endl; for (int n = LOWER_LIMIT; n <= UPPER_LIMIT; n++) { cout << setw(3) << n << " |"; cout << setw(4) << RaiseIntToPower(n, 2) << " |"; cout << setw(5) << RaiseIntToPower(2, n) << endl; } return 0; }

slide-18
SLIDE 18

Structure of a C++ program Variables, values, and types Statements Functions

Example (cont.)

function comments

/* * Function: RaiseIntToPower * Usage: p = RaiseIntToPower(n, k); * --------------------------------- * This function returns n to the kth power. */

slide-19
SLIDE 19

Structure of a C++ program Variables, values, and types Statements Functions

Example (cont.)

function definition

int RaiseIntToPower(int n, int k) { int result; result = 1; for (int i = 0; i < k; i++) { result *= n; } return result; }

slide-20
SLIDE 20

Structure of a C++ program Variables, values, and types Statements Functions

Example (cont.)

function definition

int RaiseIntToPower(int n, int k) { int result; result = 1; for (int i = 0; i < k; i++) { result *= n; } return result; }

Style: Page 6

slide-21
SLIDE 21

Structure of a C++ program Variables, values, and types Statements Functions

Outline

1

Structure of a C++ program

2

Variables, values, and types

3

Statements

4

Functions

slide-22
SLIDE 22

Structure of a C++ program Variables, values, and types Statements Functions

Variables and values

Declaration: four properties type: (int i; double x; char c; ...) name: Naming conventions

start with a letter or underscore, others are letters, digits, or underscores, no spaces or special characters No reserved keywords (Table 1-1, p. 11) Case sensitive

slide-23
SLIDE 23

Structure of a C++ program Variables, values, and types Statements Functions

Variables and values

Declaration: four properties type: (int i; double x; char c; ...) name: Naming conventions

start with a letter or underscore, others are letters, digits, or underscores, no spaces or special characters No reserved keywords (Table 1-1, p. 11) Case sensitive

Examples variables: totalTime functions: RaiseIntToPower constants: UPPER LIMIT

slide-24
SLIDE 24

Structure of a C++ program Variables, values, and types Statements Functions

Variables and values

Declaration: four properties (cont.) life time: How long a varible persists. The lifetime of a variable declared in a function (local variable) is the time when the function is active scope: accessibility. The scope of a local variable extends to the end of the block where it is declared.

slide-25
SLIDE 25

Structure of a C++ program Variables, values, and types Statements Functions

Variables and values

Declaration: four properties (cont.) life time: How long a varible persists. The lifetime of a variable declared in a function (local variable) is the time when the function is active scope: accessibility. The scope of a local variable extends to the end of the block where it is declared. We rarely, if ever, use global variables (declared outside any function).

slide-26
SLIDE 26

Structure of a C++ program Variables, values, and types Statements Functions

Variables and values

Variables must be declared before they are used.

slide-27
SLIDE 27

Structure of a C++ program Variables, values, and types Statements Functions

Variables and values

Variables must be declared before they are used. All values have a type, and every variable has a declared type. Example: 2 (int), 2.0 (double)

slide-28
SLIDE 28

Structure of a C++ program Variables, values, and types Statements Functions

Variables and values

Variables must be declared before they are used. All values have a type, and every variable has a declared type. Example: 2 (int), 2.0 (double) Local variable can be declared anywhere with a block of statements. Example:

for (int i = 0; ...) { ... }

slide-29
SLIDE 29

Structure of a C++ program Variables, values, and types Statements Functions

Data types

Two attributes: Domain and operations.

slide-30
SLIDE 30

Structure of a C++ program Variables, values, and types Statements Functions

Data types

Two attributes: Domain and operations. Atomic types integer: short, int, long floating-point: float, double, long double text: char (ASCII code, Table 1-2, p. 14), string Boolean: bool

slide-31
SLIDE 31

Structure of a C++ program Variables, values, and types Statements Functions

Operations

Precedence and associativity (Table 1-4, p. 17). Example: 7 + 6 / 3 * 2 or 7 + ((6 / 3) * 2) In general, put extra parentheses.

slide-32
SLIDE 32

Structure of a C++ program Variables, values, and types Statements Functions

Operations

Precedence and associativity (Table 1-4, p. 17). Example: 7 + 6 / 3 * 2 or 7 + ((6 / 3) * 2) In general, put extra parentheses. Mixing types (automatic conversion, Table 1-5, p. 18). Example: 9 / 4.0 Values are promoted to the richer type.

slide-33
SLIDE 33

Structure of a C++ program Variables, values, and types Statements Functions

Operations

Precedence and associativity (Table 1-4, p. 17). Example: 7 + 6 / 3 * 2 or 7 + ((6 / 3) * 2) In general, put extra parentheses. Mixing types (automatic conversion, Table 1-5, p. 18). Example: 9 / 4.0 Values are promoted to the richer type. Type casts: int num, den; double (num) / den;

slide-34
SLIDE 34

Structure of a C++ program Variables, values, and types Statements Functions

Operations

Assignments: multiple assignments (n1 = n2 = 0). It works because an assignment is an expression that has as its result the value assigned. shorthand assignments (x += 2)

slide-35
SLIDE 35

Structure of a C++ program Variables, values, and types Statements Functions

Operations

Assignments: multiple assignments (n1 = n2 = 0). It works because an assignment is an expression that has as its result the value assigned. shorthand assignments (x += 2) Increments and decrements (i++, ++i, j--) Be sure you understand their meanings. (P . 22)

slide-36
SLIDE 36

Structure of a C++ program Variables, values, and types Statements Functions

Operations

Assignments: multiple assignments (n1 = n2 = 0). It works because an assignment is an expression that has as its result the value assigned. shorthand assignments (x += 2) Increments and decrements (i++, ++i, j--) Be sure you understand their meanings. (P . 22) Boolean relational operators: ==, !=, <, >, <=, >= short-circuit evaluation: if ((y != 0) && (x % y == 0)) logical operators: !, &&, || bitwise operators: &, | Don’t confuse Boolean logic with bitwise operators.

slide-37
SLIDE 37

Structure of a C++ program Variables, values, and types Statements Functions

Outline

1

Structure of a C++ program

2

Variables, values, and types

3

Statements

4

Functions

slide-38
SLIDE 38

Structure of a C++ program Variables, values, and types Statements Functions

Simple I/O

Simplified I/O #include "simpio.h" Stream manipulators, Table 1-3, p. 16 #include <iomanip>

slide-39
SLIDE 39

Structure of a C++ program Variables, values, and types Statements Functions

Simple I/O

Simplified I/O #include "simpio.h" Stream manipulators, Table 1-3, p. 16 #include <iomanip> cout << "Enter an integer: " << endl; int n1 = GetInteger(); cout << "Enter a floating-point: " << endl; float x = GetReal();

slide-40
SLIDE 40

Structure of a C++ program Variables, values, and types Statements Functions

Statements

Simple statements a = b + c; Compound statements (blocks): indentation (four spaces) { y = x; x += 1; } Terminator ;

slide-41
SLIDE 41

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: if

if (condition) statement The test must always be enclosed in parentheses. if (condition) statement else statement

if (n % 2 == 0) { cout << "That number is even." << endl; } else { cout << "That number is odd." << endl; }

slide-42
SLIDE 42

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: if

if (condition) statement The test must always be enclosed in parentheses. if (condition) statement else statement

if (n % 2 == 0) { cout << "That number is even." << endl; } else { cout << "That number is odd." << endl; }

Any non-zero expression is true if (x) means the same as if (x != 0)

slide-43
SLIDE 43

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: switch

switch (d) { case 0: cout << "zero"; break; case 1: cout << "one"; break; case 2: cout << "two"; break; case 3: cout << "three"; break; case 4: cout << "four"; break; case 5: cout << "five"; break: case 6: cout << "six"; break; case 7: cout << "seven"; break; case 8: cout << "eight"; break; case 9: cout << "nine"; break; default: Error("Illegal call to PrintOneDigit"); }

slide-44
SLIDE 44

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: switch

switch (d) { case 0: cout << "zero"; break; case 1: cout << "one"; break; case 2: cout << "two"; break; case 3: cout << "three"; break; case 4: cout << "four"; break; case 5: cout << "five"; break: case 6: cout << "six"; break; case 7: cout << "seven"; break; case 8: cout << "eight"; break; case 9: cout << "nine"; break; default: Error("Illegal call to PrintOneDigit"); }

use break and default

slide-45
SLIDE 45

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: while

Digit sum

sum = 0; while (n > 0) { sum += n % 10; n /= 10; }

slide-46
SLIDE 46

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: while

Digit sum

sum = 0; while (n > 0) { sum += n % 10; n /= 10; }

Solving the loop-and-half problem with while (true) and break

while (true) { ... if (value == sentinel) break; ... }

slide-47
SLIDE 47

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: while

Digit sum

sum = 0; while (n > 0) { sum += n % 10; n /= 10; }

Solving the loop-and-half problem with while (true) and break

while (true) { ... if (value == sentinel) break; ... }

Programming style: Use at most one break in any given loop.

slide-48
SLIDE 48

Structure of a C++ program Variables, values, and types Statements Functions

Example

Echo an integer until −1 const int SENTINEL = -1; while (true) { cout << " ? "; int value = GetInteger(); if (value == SENTINEL) break; cout << value << endl; }

slide-49
SLIDE 49

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: for

for (int t = 10; t >= 0; t--) { cout << t << endl; }

slide-50
SLIDE 50

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: for

for (int t = 10; t >= 0; t--) { cout << t << endl; }

The expressions init, test, and step are each optional, but the semicolons must appear. If init is missing, no initialization; If test is missing, assumed to be true; If step is missing, no action between loop cycles.

slide-51
SLIDE 51

Structure of a C++ program Variables, values, and types Statements Functions

Control statements: for

for (int t = 10; t >= 0; t--) { cout << t << endl; }

The expressions init, test, and step are each optional, but the semicolons must appear. If init is missing, no initialization; If test is missing, assumed to be true; If step is missing, no action between loop cycles. Use for loop for straightforward iterative tasks; while loop for indefinite iteration.

slide-52
SLIDE 52

Structure of a C++ program Variables, values, and types Statements Functions

Outline

1

Structure of a C++ program

2

Variables, values, and types

3

Statements

4

Functions

slide-53
SLIDE 53

Structure of a C++ program Variables, values, and types Statements Functions

Functions

Prototype and definition must match exactly.

slide-54
SLIDE 54

Structure of a C++ program Variables, values, and types Statements Functions

Functions

Prototype and definition must match exactly. function-calling mechanism

1

Evaluate arguments;

2

Creat a frame on the stack for local variables, including arguments;

3

Copy the values of the arguments in order;

4

Execute the function;

5

Return the value of the function, if any;

6

Discard the frame for the function;

7

Continue the calling function with the returned value, if any.

slide-55
SLIDE 55

Structure of a C++ program Variables, values, and types Statements Functions

Function (cont.)

Call by value.

void SetToZero(int x) { x = 0; } ... x = 1; SetToZero(x); cout << x << endl;

slide-56
SLIDE 56

Structure of a C++ program Variables, values, and types Statements Functions

Function (cont.)

Call by value.

void SetToZero(int x) { x = 0; } ... x = 1; SetToZero(x); cout << x << endl;

x x 1 1

slide-57
SLIDE 57

Structure of a C++ program Variables, values, and types Statements Functions

Function (cont.)

Call by value.

void SetToZero(int x) { x = 0; } ... x = 1; SetToZero(x); cout << x << endl;

x x 1

slide-58
SLIDE 58

Structure of a C++ program Variables, values, and types Statements Functions

Function (cont.)

Call by value.

void SetToZero(int x) { x = 0; } ... x = 1; SetToZero(x); cout << x << endl;

x 1

slide-59
SLIDE 59

Structure of a C++ program Variables, values, and types Statements Functions

Function (cont.)

Call by reference.

void SetToZero(int & x) { x = 0; } ... x = 1; SetToZero(x); cout << x << endl;

slide-60
SLIDE 60

Structure of a C++ program Variables, values, and types Statements Functions

Function (cont.)

Call by reference.

void SetToZero(int & x) { x = 0; } ... x = 1; SetToZero(x); cout << x << endl;

FFB4 x x FFC0 1 FFC0

slide-61
SLIDE 61

Structure of a C++ program Variables, values, and types Statements Functions

Function (cont.)

Call by reference.

void SetToZero(int & x) { x = 0; } ... x = 1; SetToZero(x); cout << x << endl;

FFB4 x x FFC0 FFC0

slide-62
SLIDE 62

Structure of a C++ program Variables, values, and types Statements Functions

Function (cont.)

In general If you only use the value of an argument in the function (on the right-side of assignments), call it by value. If you want to reflect the change of the value of an argument in the function to the caller (on the left-side of assignments), call it by reference.

slide-63
SLIDE 63

Structure of a C++ program Variables, values, and types Statements Functions

Example

Program decomposition: Input-Computation-Output

slide-64
SLIDE 64

Structure of a C++ program Variables, values, and types Statements Functions

Example

Program decomposition: Input-Computation-Output Solving quadratic equations ax2 + bx + c = 0, a = 0. Textbook formula x = −b ± √ b2 − 4ac 2a

slide-65
SLIDE 65

Structure of a C++ program Variables, values, and types Statements Functions

Example

Program decomposition: Input-Computation-Output Solving quadratic equations ax2 + bx + c = 0, a = 0. Textbook formula x = −b ± √ b2 − 4ac 2a Computer method x1 = 2c −b − sign(b) √ b2 − 4ac , x2 = c ax1 .

slide-66
SLIDE 66

Structure of a C++ program Variables, values, and types Statements Functions

Example (cont.)

void SolveQuadEqn(double a, doule b, double c, double &x1, double &x2) { if (a == 0) Error("Coefficient a is zero."); double disc = b * b - 4 * a * c; if (disc < 0) Error("Solutions are complex."); if (disc == 0) { x1 = x2 = -b / (2 * a); } else { x1 = 2*c / (-b - sign(b)*sqrt(disc)); x2 = c / (a * x1); } }