CPS 6, Ramm - Summer Semester I - 5/18/01 #2
- Announce
- Roll
- Lab #1 Today
- Assignment #1
Chapter 2: C++ Programs: Form and Function
- Functions: Dia & Mond
diamond.cpp
tree.cpp
- Flow of Control (again)
- Output, Stream Insertion Operator
<<
- Literals: string, integer, real, expressions
cout << "product = " << "12" << endl;
cout << "product = " << 12 << endl;
cout << "product = " << "3 * 4" << endl;
cout << "product = " << 3 * 4 << endl;
literals.cpp
What is evaluated? What is not?
- Syntax/Grammar vs. Semantics/Meaning
- Style (Important!)
- Functions with Parameters
Flexibility/Versatility
rect5by.cpp
page.cpp
- Functions with several parameters
announce.cpp
Order of Parameters
- Functions Invoking Functions
diamond2.cpp
- Order of Functions
mondias.cpp
- Function Prototypes - Syntax
type function(type name, type name, ...);
type function(type, type, ...);
Semicolon: " ; " required!
- Using Function Prototypes
diamondsp.cpp
- Let's Write a Program (class activity)
Requirements:
- Use functions
- Have functions use functions
- Minimize repetition
- Some kind of "graphic" output
- Possibly a multi-story building?
Questions:
- Do we need prototype statements?
- What language features could we use?
- More on Style
- Indenting
- Braces on separate lines
- Good Identifiers
- Identifiers/Syntax
- letters, digits, underscore
- do not start with digit
- case sensitive
- arbitrary length
- do not use reserved words
- Identifiers/Style
- meaningful identifiers
- user defined functions & types start: uppercase
- parameters or variables start: lowercase
- constants (later): all uppercase
- use uppercase to start multi-part words (not _)
- Complexity: Measuring Computer Resources
- Looking Up a Phone Number Given a Name
- Looking Up a Name Given a Phone Number
- So Far: Previous Programs Fixed, Once Written
- More typically:
- Input: information fed to program (yum yum)
- Process: information is processed (indigestion?)
- Output: results of processing displayed (oh oh !?)
- Divide and Conquer
- Iterative Enhancement
(Planning for our Multi-Story Building)
- The Input Stream
cin
cin >> person
- Extraction Operator
>>
- Variables/Objects
- Definition/Creation/Memory
Type/Class
type name;
type name1, name2, ... name N;
- Modify Paging Program
page2.cpp
- Processing Numbers
Example: Collision at -40
tempscale.cpp
- Type:
int Numbers
- max, min
- micros, workstations
- Type:
double Numbers
- Arithmetic Operators/Operands
- + addition
- - subtraction
- * multiplication
- / division
- % modulus/remainder
- Operator Precedence
- ( )
- *, /, %
- +, -
- left to right
- Automatic
int to double conversion
- Classes and Types
- Objects and Variables