157
Programming in C++ The Turbo C++ Environment C++ Program Structure Modular Programming with Functions C++ Control Structures Advanced Data Types Classes

1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

Embed Size (px)

Citation preview

Page 1: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

1

Programming in C++

The Turbo C++ Environment C++ Program Structure Modular Programming with Functions C++ Control Structures Advanced Data Types Classes

Page 2: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

2

Turbo C++ Environment

Windows based product Integrated Development Environment (IDE)

– editor– compiler– linker– debugger

Page 3: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

3

Structure of a C++ Program

preprocessor directives

main function header

{declare statements

statements

}

Page 4: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

4

Using the Turbo C++ IDE

tool bars menu editor

Page 5: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

5

Using the Turbo C++ IDE (2)

compiling linking executing

Page 6: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

6

Developing Programs

Understand the problem Design a solution to the problem

– including test cases and the solutions to the test cases

Implement and document– Translate the solution to a programming

language

Page 7: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

7

Developing Programs (2)

Verify– Test and debug the solution

» using test cases from design phase

Maintain

Page 8: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

8

Problem Solving (1)

consider a trapezoid -- 4 sided figure in which two sides are ||, the area is 1/2 the product of the height and the sum of the lengths of the two bases.

b2

h

Area = (b1 + b2)h/2

b1

Page 9: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

9

Problem Solving -- Trapezoid Pseudocode

input b1

input b2

input height

bases = b1 + b2

area = bases * h /2

output area

Page 10: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

10

Problem Solving (2)

consider finding the area and circumference of a circlepi = 3.14159

area = pi * radius2

circumference = 2 * pi * radius

Page 11: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

11

Problem Solving -- Circle Functions Pseudocode

pi = 3.14159

input radius

circum = 2 * pi * radius

area = pi * radius * radius

output area

output circum

Page 12: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

12

Problem Solving (3)

consider converting temperatures from Centigrade to Fahrenheit (or vice versa) where

c = 5/9(f-32)

f = 9/5c + 32

Page 13: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

13

Problem Solving --Temperature Conversion Pseudocode

input temp

input scale

if scale = = ‘f’newtemp = 5/9 (temp-32)

elsenewtemp = 9/5 temp + 32

output newtemp

Page 14: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

14

Problem Solving (4)

consider sales commissions based upon the number of sales made during the time period$8 per sale for < 15 sales

$12 per sale = 15 sales

$16 per sale > 15

Page 15: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

15

Problem Solving -- Commission Pseudocode

quota = 15

input number_sales

if number_sales < quotarate = 8

else if number_sales == quota

rate = 12

else rate = 16

com = rate * number_sales

output com

Page 16: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

16

Problem Solving -- Commission Pseudocode Multiple Salespeople

quota = 15

input number_salespeople

Page 17: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

17

Problem Solving -- Pseudocode Multiple Salespeople (2)

loop number_salespeople times

input number_sales

if number_sales < quotarate = 8

else if number_sales == quota

rate = 12

else rate = 16

com = rate * number_sales

output com

Page 18: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

18

Exercise -- GO

Develop a series of problems for the students to do using each of the statement types

Page 19: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

19

Introduction to the C++ Language

keywords– C++ is case-sensitive

identifiers– can not be keywords

comments– enclosed in /* */ multi-line – start with // single line

Page 20: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

20

Preprocessor Statements

library header files -- #include< > -- system library

#include <iostream.h>

“ “ -- personal library#include “apstring.h”

Page 21: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

21

Data Types and Declarations

declare statement– allocates memory and assigns “name”– data type name [= initial value];

int -- 2 bytes float -- 4 bytes double -- 8 bytes char -- enclosed in ‘ ‘

Page 22: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

22

User Defined Data Types

class -- mechanism to establish new data types

ap classes– string

» apstring.h apstring.ccp

– bool» bool.h

Page 23: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

23

Example Declare Statements

int a; int a,b,c; float x,

y; double average = 0.0;

char answer = ‘Y’; bool another; bool more = false; apstring name; apstring

class = “C++”;

Page 24: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

24

Input and Output Statements

#include <iostream.h>

cout -- output

<< insertion character

cin -- input

>> extraction character

Page 25: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

25

Using APSTRING Class

#include “apstring.h”– entire path

create project – place apstring and program in project– you will need a different project for each

program

Page 26: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

26

Input and Output Statements (2)

COUT -- control codes– way of inserting placement control– \n -- new line – \t -- tab

iomanip.h– contains more formatting methods

Page 27: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

27

Arithmetic in C++

operator precedence( )

*, /, % (left to right)

+, - (left to right) integer arithmetic

– operations involving integers yielding integer results

– truncation on integer division– % -- modulo operator

Page 28: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

28

Arithmetic in C++ (2)

mixed mode arithmetic– operands of different data types– hierarchy double/float/int

» highest mode is used

» determined on a operation by operation basis

Page 29: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

29

Assignment Statements

assignment operator =– operator precedence and mixed mode

arithmetic hold combination operators

+=, -=, *=, /=, %= variable = expression;

Page 30: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

30

Increment and Decrement Statements

special operators which add or subtract one from a variable– more efficient (generates inc, dec)

a++; ==> a = a+1; a--; ==> a = a -1; postfix (a++;) (a--;)

– done after the expression is evaluated prefix (++a;) (--a;)

– done prior to evaluating the expression

Page 31: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

31

Type Casting

changes the evaluation data type of the expression– does not change the data type of the variable

(data type)– (int) – (float)– (apstring)

Page 32: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

32

Programming Problems

convert distance in miles to distance in kilometers and meters– 1 mile = 1.61 km, 1 km = 1000 meter

convert a temperature in Celsius to Kelvin– -273.15oC = 0oK

convert a temperature in Fahrenheit to Kelvin– -459.67oF = 0oK

Page 33: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

33

Mathematical Functions (math.h)

code reuse sqrt, pow, exp, log, log10 abs, ceil, floor trigonometric functions

Page 34: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

34

Programming Problems

determine the volume of a sphere with an input radius– volume = (4 * pi * radius3)/3

determine the area of a triangle when given length of two sides and the included angle in degrees– degrees = 180 * radians / pi– area = side1 * side2 * sin (radians) / 2

Page 35: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

35

Programming Problems (2)

determine the distance from a point on the Cartesian plane to the origin– distance = sqrt (x2 + y2)

Page 36: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

36

Exercise -- GO

Implement the sequential problems developed in the first exercise

Page 37: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

37

Modular Programming with Functions

designed in small segments each segment implemented as a function

– sqrt, pow, sin self contained

– requires input through parameters– sends output through name

Abstraction– know what the function does and what it needs to do its task– not how the function works

Page 38: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

38

Modular Programming with Functions (2)

allows for reuse eliminates redundancy allows for team development simplifies logic

Page 39: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

39

Form of a Function

[return data type] Function name (parameter list){

[declarations]

statements[return ]

}

Page 40: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

40

Types of Functions

no input, no return value – void

input but no return value both input and output no input but returns a value

Page 41: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

41

Example -- Circle Functions

calculate the area and circumference of a circle of an input radius– input radius– calculate area– calculate circumference– output results

invoke the functions– use name and parameters in an expression

functions must be defined before they can be used

Page 42: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

42

Example -- Pythagorean Triples

Pythagorean Triple are the three sides of a right triangle a,b,c– a2 + b2 = c2

given m and n, such that m>n we can generate the triples– a = m2 - n2

– b= 2mn– c = m2 + n2

Page 43: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

43

Call by Value

on invocation the value of the actual parameter is copied into the formal parameter

when the function terminates the value IS NOT copied back to the actual parameter

can not change the value of a parameter within the function

Page 44: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

44

Example Call by Value

#include <iostream.h>

int test (int n){

int i = 5;n +=i;return (n);

}

void main (void){

int n=1, i;

i = test (n);cout << i << “ = “

<< n << endl;}

Page 45: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

45

Example Call by Value (2)

main test

n

i

n

i

1

6

1 6

5

Page 46: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

46

Functions -- Pass by Reference

returns 0 or 1 value through name need to return more than 1

– swap the values of two variables change the values of parameters

– bank deposit or check pass the “name” of the parameter rather than

its value so that the function uses the same memory location as the actual parameter

Page 47: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

47

Reversing Order -- Swap

if (num1 < num2)

{

temp = num1;

num1 = num2;

num2 = num1;

}

Page 48: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

48

Reference Parameters

Parameter which shares the memory of the actual parameter rather than declare new memory and copy the actual’s value into it

Parameter declaration int & x; – x is an alias for the actual integer parameter

double & y– y is an alias for the actual double parameter

Page 49: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

49

Function Swap

void swap (int & num1, int & num2)

{ int temp;

temp = num1;

num1 = num2;

num2 = temp;

}

if a > b

swap (a,b); to invoke the function

Page 50: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

50

Call by Value vs Reference

Use reference vs return type– all input– when need to return more than 1 value– always have return type void

Use value– all other cases– no side effects

Page 51: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

51

Exercise

modify circle.cpp to use reference where appropriate

modify pyth.cpp to have compute_sides

Page 52: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

52

Programming Problem -- Functions

program to convert Fahrenheit to Celsius, Fahrenheit to Kelvin– input the temperature in Fahrenheit – use functions

» input Fahrenheit temperature

» convert to Celsius

» convert to Kelvin

» output the results

Page 53: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

53

Programming Problem -- Functions

Translate US prices from pennies per pound to Canadian prices dollars per kilogram– 1 pound = .4536 kilograms– 1 dollar US = 1.26 dollars Canadian

Input 5 words, echoing each as it is input and display the average length (number of characters) per word

Page 54: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

54

Exercise -- GO

Implement all previous programs using modular design and reference and value parameters as appropriate

Page 55: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

55

Function Prototypes

a function must be declared before it can be used

placed functions at top of program create prototype (declaration of interface), place

it at the top and the functions’ implementation can be placed after the main function

[return value] function name (parameter list);float get_radius ();

Page 56: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

56

Overloading

function names can be overloaded– different interfaces– compiler can determine which to execute

operators can be overloaded as well

Page 57: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

57

Selection Structures

execute a group of statements based upon a condition being true or false

if (condition)statement;

if (condition){ statement(s);

} conditions -- relational operators

<, >, <=, >=, = =, !=

Page 58: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

58

Simple Selection Examples

if (hours > 40)cout << “overtime!\n”;

if (cost > 30000){

tax = (cost - 30000) * tax_rate;

cout << “\n for a car costing “ << cost <<

“a luxury tax of “ << tax << “ is due ”;

}

Page 59: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

59

Selection -- IF ELSE if (condition)

statement;

else

statement; if (condition)

{ statements;

}

else

{statements;

}

Page 60: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

60

If ELSE -- Examples

if (x>y) max = x;

else

max = y;

Page 61: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

61

If ELSE -- Examples (2)

if (hours <= 40) gross_pay = wage * hours;

else

{

overtime_hours = hours -40;

overtime_pay = wage * overtime_hours * 1.5;

gross_pay = wage * 40 + overtime_pay;

}

Page 62: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

62

Programming Example

find the reciprocal of an integer undefined for 0

– attempt to divide by 0 will cause program termination

recip.cpp

Page 63: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

63

Programming Exercise

Modify the program which finds the roots of the quadratic equation so that it will not error terminate– quad.cpp

Page 64: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

64

Logical Operators

combine two or more relational operators to create complex relations

AND -- && OR -- || NOT -- ! precedence && before ||

Page 65: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

65

Conditional Operators -- Examples

if (temp_type = = ‘F’ || temp_type = = ‘f’){

centigrade = convert_cent (temp);

kelvin = convert_kelvin (temp);

} If (num > 0 && num < 10)

{

cout << “single digit number\n”;

}

Page 66: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

66

Short Circuiting

efficient evaluation of Boolean expression AND

– the first relational expression which evaluates false terminates the evaluation-- result false

OR– the first relational expression which evaluates

as true terminates the evaluation -- result true

Page 67: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

67

Short Circuiting (2)

determine if a number is divisible by another number

if the second number is 0 -- error termination

if (a != 0 && b % a == 0)

if a = 0 the second expression is not evaluated

Page 68: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

68

Programming Example

determining leap years leap years occur every 4 years, if the year is

divisible by 4– only valid for non-centennial years

centennial year (divisible by 100) which is divisible by 400

Page 69: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

69

BREAK statement

allows program to leave a control structure form -- break;

Page 70: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

70

Multiple Selection -- Switch

test the value of a single integer type and perform different blocks of statements based upon the value

Page 71: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

71

Multiple Selection -- Switch Form

switch (expression)

{ case value 1:

statement(s);

break;

case value 2:

statement (s);

break; ....

[default:

statement(s);

break;]}

Page 72: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

72

Example Switch Statement

determine if a value is -1, 0, or 1-4cin >> value;

switch (value)

{

case -1:

cout << “value = -1\n”;

break;

case 0:

cout << “value = 0\n”;

break;

Page 73: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

73

Example Switch Statement Con’t

case 1:

case 2:

case 3:

case 4:

cout << “value in range 1-4\n”;

break;

default:

cout << “value is < -1 or > 4\n”;

}

Page 74: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

74

Example Programming Problem

color compliments clswitch.cpp

Page 75: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

75

Programming Problem

complete temperature conversion program accepts as input a temperature and a type

and converts it to the other two temperature types

prints an error message if unknown type accepts both upper and lower case input

Page 76: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

76

Exercise -- GO

Implement the selection statement problem solving problems

Page 77: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

77

Repetition Statements

ability to repeatedly execute blocks of statements

two types of loops– count controlled

» executed a set number of times

– event driven» executed until a certain event occurs

» pre-test and post-test loops

Page 78: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

78

While Loop

formwhile (condition)

{

statement(s);

} event driven loop

Page 79: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

79

While Loop (2)

pre-test (0) loop– test the condition

» if true execute the loop

» if false exit loop

» loop can be executed 0 times

Page 80: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

80

Example While Loop

i = 5;

while (i > 0)

{ cout << i << endl;

i--;

}

Page 81: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

81

Programming Example

taking averages enter values to be averaged until sentinel is

entered (0)– event which terminates loop

ave.cpp

Page 82: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

82

Controlling Input

0 is in the set to be averaged– must use some key defined value to signal end

of input– CRTL Z

get()– cin.get()– accepts a single value as input– prompt for CRTL (^) Z

Page 83: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

83

Do While Loop

event driven loop always executes at least once (1 loop) post test loop form

do{

statement(s);

}while (condition);

Page 84: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

84

Do While Loop (2)

executes the loop tests the condition

– if true executes the loop again– if false exits the loop

Page 85: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

85

Do While Example

add the numbers from 1 to 5

sum = 0;

i = 1;

do{

sum += i;

i ++;

}while (i <= 5);

Page 86: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

86

Programming Example

display square of input value user prompt to continue squares.cpp

Page 87: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

87

Programming Example -- Circle Functions

robust programming– user friendly/user forgiving

Area and Circumference of circle– radius can not be <=0– present error message and re-prompt for input

until it is valid– circleif.cpp

Page 88: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

88

Programming Exercise -- Pythagorean Triples

robust example– m > n and both > 0

give meaningful error message

Page 89: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

89

For Loop

counted loop -- set number of times iterates through a set of values

for (initial expression;

condition;

loop expression)

{ statement(s);

}

Page 90: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

90

For Loop (2)

initial expression -- starting point, executed once before the loop begins

condition -- evaluated each time through the loop (pre test) – exit -- false– execute -- true

loop expression -- statement(s) executed at the bottom of the loop

Page 91: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

91

Example For Loop - I

Countdown

for (i = 1; i<=5; ++i)

{

cout << i << endl;

}

Page 92: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

92

Example For Loop - II

sum numbers 1-5

for (sum = 0, i = 1; i <= 5; ++i)

{

sum += i;

}

Page 93: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

93

Programming Examples

Factorials– fact.cpp– change fact to be integer (see what happens)

temperature conversions– temps.cpp

generating random numbers– random.cpp

Page 94: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

94

Boolean Variables

Turbo C++ does not have Boolean– bool.h -- apclass – 0 false, 1 true

bool flag– if flag (if 0 false, non 0 true)– while !flag

flags.cpp

Page 95: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

95

Programming Exercise

maintain check book balance modular $15 service fee for bad check

– display message final balance on exit

Page 96: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

96

Nesting Control Structures

both selection and repetition statements can be nested for complex execution

if else if– else matches closest un-elsed if

all looping structures can be nested regardless of type

Page 97: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

97

Example If else if -- Sales Quotas

if (num_sales < quota)

rate = low_rate;

else if (num_sales = quota)

rate= ave_rate;

else rate = high_rate;

Page 98: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

98

Example Nested Loops

cout << “enter the number to sum to, 0 to end”;

cin >> num;

while (num != 0)

{ for (sum=0, i=1; i<=num;++i)

sum += num;

cout << “the sum of the numbers 1 - ....

cout << “enter the number to sum to ...

cin >> num);} /*end while*/

Page 99: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

99

Nesting Control Structures Programming Examples

counting number of letter grades– aven.cpp

printing multiplication tables– table.cpp

circle functions– circleof.cpp

Page 100: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

100

Programming Exercise

Modify the average program so that more than 1 set of averages can be determined

Modify the Pythagorean triples so that an unlimited number of triples can be generated

Modify finding roots of a quadratic equation so that all root types are determined

Page 101: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

101

Enumeration Types

user defined data type– enum statement– define the domain

enum bool {false, true};– bool -- name of data type– false, true -- domain

integers– false = 0, true =1

Page 102: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

102

Lines in Cartesian Plane

perpendicular, parallel or intersecting slope enumeration type can be used

– parameters– return types

lines.cpp

Page 103: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

103

Exercise -- GO

Implement any remaining problem solving programs.

Be sure have a complete set identifying all structures including enumeration types.

Page 104: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

104

Composite Data Structures

construct that can access more than one data item through a single name

Array -- homogenous data type Structure -- heterogeneous data type

Page 105: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

105

Arrays\Vectors

collection of data components all of same data type are contiguous accessed

– entire array (name)– individual component (subscript)

Page 106: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

106

Declaring Arrays int x[5]

– declares a 5 element array of integers» x[0], x[1], x[2], x[3], x[4]

int x[2][5] -- two dimensional array int x [2] [5] [5] -- three dimensional array size must be declared at compile time

– can not int size, int x[size]– can

» #define max_size 100» int x[max_size]

Page 107: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

107

Referencing Arrays

elements– float ave_temp [12]

» ave_temp [0] -- Jan

» ave_temp [11] -- Dec

» ave_temp [i+2]

no arrays bounds checking– “fast” code

Page 108: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

108

Initializing Arrays int x[5] = {12,-2,33,21,31}; int height [10] = {60,70,68,72,68};

– rest 0 float g[] = {3.2,5.7};

– size is set to 2 a 250 element array all to 1

int x[250];

for (i =0; i<=249; i++)

x[i] = 1;

Page 109: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

109

Using Arrays

data must be passed more than once– array1.cpp

implement vectors or matrices– array2.cpp

data comes in haphazard order– string example

Page 110: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

110

Passing Arrays to Functions

pass an element – treated as any single variable of that type

» pass by value

pass the entire array– use the name without any subscripting– pass by reference

» pass the address and the actual memory locations of the actual array are used by the function

» any change made to the elements of the array by the function WILL be noted in the main program

Page 111: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

111

Programming Problem

Input a set of exam scores for a class – calculate and display

» average

» high grade

» low grade

» those grades which were above the average

– have number of grades entered determined by the # of values input rather than prompt for class size

Page 112: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

112

Programming Problem

Using an enumeration type for months of the year– calculate the average rainfall– display those months with < average rainfall

amounts

Page 113: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

113

Structures

Heterogeneous data type– logically related set of items which can be

accessed either on an individual item basis or all at once through structure’s name

– fields can be of any data type (different ones), user defined as well

Page 114: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

114

Example Structure

struct GRADES

{ apstring name;

int midterm;

int final;

float assigns;

float sem_ave;

char letter_grade;};

GRADES student1, student2;

Page 115: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

115

Operations on Structures

Assignment– entire structures done by common elements, in

order– single element -- data type

Initialization– on declare

» FRACTION num1 = {1,2};

» GRADES student1 = {“john Doe”,90,80,70,80};

Page 116: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

116

Structures and Functions

An element is passed to a structure in the same way any simple variable is passed– by value (default) or by reference (forced)– student.cpp

An entire structure is passed– by value (default) – by reference (force) employee.cpp

A function can return a structure variable

Page 117: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

117

“Arrays” and Structures

Structures can contain vectors, apstring– apstring name– apvector<int> exams(3)

vectors of structures– apvector<GRADES> class(60);

» 60 students in class

» class[0].name class[0].final

» class[59].name class[59].final

Page 118: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

118

Hierarchical Structures

Structures can contain structures

typedef struct

{char last [15];

char first [15];

char middle;} NAME;

typedef struct

{NAME stu_name;

…} STUDENT;

Page 119: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

119

Arrays\Vectors

collection of data components all of same data type are contiguous accessed

– entire array (name)– individual component (subscript)

Page 120: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

120

Declaring Vectors

#include “a:apvector.h” apvector<int> v1(10);

– declares a 10 element integer vector– v1[0], v1[1], v1[2]….v1[9]

apvector<int> v2(10,0);– declares a 10 element integer vector – all elements are initialized to 0– v2[0]=0, v2[1]=0…..v2[9]=0

Page 121: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

121

Declaring Vectors (2)

apvector<apstring> (25);– declares a vector of 25 strings– each is “empty” string

can be user defined data types

Page 122: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

122

Accessing Elements

v1[1]– second element of the vector

v1[9]– last element of the vector

v1[1] += 2; high = v1[3];

Page 123: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

123

Assignment -- APVECTOR

Apvector<int> v1(10), v2(20); v1 = v2;

– v1 will be “reallocated” at a size of 20– v1[0] = v2[0]– ….– v1[19] = v2[19]

corresponding elements will be assigned

Page 124: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

124

Member Functions -APVECTOR

User defined data type -- class length() -- capacity of vector

– size changes as needed– returns current size as an integer– object.length()

» v1.length() => 20

» v1 still ranges from 0-19

for (i=0;i<v1.length();i++)

cout << v1[i] << endl;

Page 125: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

125

Vectors as Parameters

elements are considered same as any single variable

entire vector– pass by value or by reference– more efficient to pass by reference– avoid side effects

» const reference parameter

Page 126: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

126

Using Vectors

data must be passed more than once– vect1.cpp

implement vectors or matrices– vect2a.cpp

data comes in haphazard order– string example

enumeration types and vectors– rainenum.cpp

Page 127: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

127

Matrices

two dimensional array problems with C++ arrays are doubled in

two dimensions APMATRIX

– #include “a:apmatrix.h”– can automatically be “resized”– subscript checking

Page 128: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

128

Declaring Matrices

apmatrix<int> imat (3,3)– imat[0][0] ....imat [2][2]

apmatrix<int> imat2(3,3,0)– all elements are initialized to 0

can be any system or user defined data type

Page 129: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

129

Referencing Elements

imat[1][2] = 7; score = imat [i][j]; if subscript is out of bounds (either of them)

program error terminates

Page 130: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

130

Assignment -- APMATRIX

apmatrix<int> imat2(10,10); imat = imat2;

– imat 3x3– imat2 10x10– after assignment imat 10x10– assigns corresponding elements

Page 131: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

131

APMATRIX--Member Functions

numrows() -- returns the number of rows– imat.numrows() ==> 10

numcols() -- returns the number of columns– imat.numcols() ==> 10

for (r=0;r<imat.numrows();r++)

for (c=0;c<imat.numcols();c++)

cout << imat[r][c];

Page 132: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

132

Programming Problem

Create “resuable” functions for matrix addition and multiplication– matrix.h – matrix.cpp– use_matrix.cpp

Page 133: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

133

ADT -- complex numbers

struct COMPLEX

{ double real;

double imag;};

operations -- input, output, add, subtract, mult, divide, absolute value

package together in include file

Page 134: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

134

Class -- User Defined Data Type

encapsulate data and functions information hiding

– public vs private can be inherited

– structures can not

Page 135: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

135

Public VS. Private

client programs can use the member functions which “come with” a class through the public interface

client program CAN NOT access any function or data member declared private– information hiding– if can’t access it, can’t modify it– more maintainable -- fewer side effects

Page 136: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

136

Class Definition

class class_name

{public:

member functions

private:

data members

};

Page 137: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

137

Data Members

Pieces of information which the class maintains– states

» date -- month, day, year» complex number --- real, imaginary» fraction -- numerator, denominator» student -- name, ssn, address, etc

Private-- only the functions of the class have access to them

Page 138: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

138

Member Functions

Services provided by the class to manipulate the data members

Public -- can be used by any “client” program

Have access to the data members Constructors, Accessors, Mutators, and

Operations

Page 139: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

139

Constructors

Automatically invoked by the system when an object of the class is declared

specify the initial values to be given to the data members

function with the same name as the class itself with no return type of any kind

can be overloaded

Page 140: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

140

Accessors

Return the value of a data member Const functions as they do not change the

value of any data member Necessary since no “outside” function can

access the data members

Page 141: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

141

Mutators

Modify one or more of the data members used by client programs to modify the data

members, since client programs can not access the data members directly

Page 142: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

142

Operations

Provide services of the class perform calculations, etc using data

members necessary since the data members are not

accessible to the client programs

Page 143: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

143

Date Class

Data members– int day, int month, int year

Constuctor– allow date to be set when the object is declared– or to use default values

small implementation of the class– demonstration purposes only

Page 144: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

144

Interface of the Date class

Definition of the class available to client programs/ers .h file declaration of the data members interfaces of the member functions the object receiving the message to invoke the

member function is the one that the function operates upon

Page 145: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

145

Date Class Implementation

.cpp file the implementation of all member functions scope resolution operator ::

– since the function implementations are in a separate file need to tie them back to the class or they will not have access to the data members

Page 146: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

146

Client Program

Declares an object of the data type– Date today;

Invoke a member function– object.function_name();– object which receives the message is the one on

which the function the function operators» v1.resize();

» v1.length();

Page 147: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

147

Constructor

Default initial values for parameters– if the parameters are omitted the initial value of

the fraction on declaration is 1/1 initialization list

– special form of the constructor which allows the data members to be set within the interface (.h file)

Page 148: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

148

Fraction Class

Represents rational numbers

constructor – initializes object to 1/1

accessors– reduce

– print_fraction

Mutators– input_fraction

operations– add, subtract, multiply,

divide

gcd– needed by class to do

its work

Page 149: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

149

Member vs Friend functions

Member function must be invoked with object receiving message

friend function stands separate– it still has access to the data members– does not need an object to be invoked– used for binary operators which do not modify

their parameters (overloading) defined in interface as friend

Page 150: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

150

Complex Number Class

class COMPLEX

{public:

COMPLEX(int r=0,int i=0);

int real() const;

int imaginary() const;

COMPLEX add_complex (const COMPLEX &l, const COMPLEX &r);

Page 151: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

151

Complex Number Class Con’t

COMPLEX sub_complex(const COMPLEX &l, const COMPLEX &r);

COMPLEX mult_complex(const COMPLEX &l, const COMPLEX &r);

COMPLEX div_complex(const COMPLEX &l, const COMPLEX &r);

Page 152: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

152

Complex Number Class -- Con’t

void set_real (double);

void set_imag(double);

private:

double real;

double imag;

};

Page 153: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

153

Member vs Friend functions

Member function must be invoked with object receiving message

friend function stands separate– it still has access to the data members– does not need an object to be invoked– used for binary operators which do not modify

their parameters (overloading) defined in interface as friend

Page 154: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

154

Inlining

Used to provide an implementation within the interface

Only use on “small simple functions” which either simply set data members or simple calculations which return values

Page 155: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

155

This pointer

Refers to the object receiving the message

*this ==> value– used in functions which modify the object

receiving the message +=,-=,*=,/=

Page 156: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

156

File I/O

Input and output are done through streams: istream and ostream (fstream.h)– cin -- istream -- iostream.h– cout -- ostream -- iostream.h

4 steps to using files– declare an object of the appropriate stream

open the stream– connects the external file with the stream (buffer)– stream.open(filename);

Page 157: 1 Programming in C++ u The Turbo C++ Environment u C++ Program Structure u Modular Programming with Functions u C++ Control Structures u Advanced Data

157

File I/O(2)

Input >> but from the stream declared rather than from cin

output << but from the stream declared rather than cout

close file– stream.close();