Read progress: finish read 1.6, 1.7&Exercise skipped 2.9 Exercise skipped
org next: ch 3.2 Iterator
Header file (& Namespace) & Program text file
ref: p11, p54 param default value, 57 inline func, 63 set up a header file
header file: declaration of obj/func (can be mtp dec [p63]) + def of an inline func
- included in each program text file that wish to use the func/obj
- Set up a Header File [2.9]
- without
extern
, obj dec in header file interpreted as the def const
obj no needextern
: only visible inside the file it defined- header file: standard vs user-provided (
.h
suffix &" "
|< >
)
- without
program text file definition of a func (can only be one def [p63])
- func def need to accsss obj/func in other files? — include header files
- compiled once and is linked to…the using files
1 |
|
example: declaration and definition of display()
Arrays, Vectors & List
1 | int elem_seq[seq_size] = { ... }; |
Pointers
- evaluation of an object’s name: evaluates to its associated value
- address-of op(
&
), deref*
(null pointer:pi = 0
)
1 | fibonacci.empty() // vector<int> fibonacci |
Function
Invoking a Func
- program stack: (with memory to) hold the value of each function parameter & local objects
- function completes — this area of memory is discarded (popped from the program stack)
- pass by value -> pass by reference
Pass by Ref
1 | // ref |
- A function can return only one value — second parameter of type ref
- example: final implementation of
fibon_elem()
…
- example: final implementation of
- reference cannot be reassigned to refer to another obj…manipulation of a reference acts on the object
- declare a parameter as a reference…modify directly the actual object being passed…eliminate copying a large obj
- recommend not passing built-in types by ref
Default Param Values
1 | void bubble_sort(vector<int> &vec, ofstream *ofil = 0) // place the default value in header file (greater visibility) |
- rules about providing default param…
Obj Memory Management
- Returning the address of one of these local obj: run-time program errors (Returning elems by value is OK…)
- extent (time) & scope (region)…
- Local Static Obj —
fibon_seq(int size)
example…1
2
3static vector<int> elems; // memory... persists across function invocations
...
return &elems; // can safely return elems's address - dynamic extent & heap memory:
new
expr (allocated at run-time) &delete
expr (memory leak if nodelete
)1
2
3
4
5
6int *pi;
pi = new int(1024);
int *pia = new int[24]; // pia: address the first element of this array
// array elems remain uninited (no syntax for init an array of elements allocated on the heap)
delete pi; // delete a single obj
delete [] pia; // delete an arr of objs
Pointers to Functions
1 | const vector<int>* (*seq_ptr)(int); // "seq_ptr" point to any func with return type "vector<int>*" & param "int" |
- use enumerator as index
1 | enum ns_type { ... } // By default, the first enumerator is assigned a value of 0... |
Inline Function
- inline function: request to the compiler to expand the function at each call point,
- prefixing its definition with the
inline
- prefixing its definition with the
Template Functions
1 | template <typename elemType, ...> |
Containers
- Standard Template Library (STL), (seq | assoc) container classes, generic algos …
- container ops… [3.3]
begin()/.end()
(e.g. vector, list) [p71]size()
,empty()
,clear()
- Sequential Containers:
vector
,list
,deque
[3.4]push_/pop_back()
,push_/pop_front()
—insert()/erase()
front/back()
- 5 ways to define a sequential container obj…
- Generic Algos &
#include <algorithm>
… [3.5]- Four possible generic search algorithms…
max_element()
,copy()
,sort()
Pointer Arithmetic
1 | // array: (passed) as a pointer to its first elem, can still indexing as bf |
- Pointer arithmetic presumes that the elements are contiguous (in a contiguous area of memory, e.g. arr/vector)
find()
example [p66~70]
Pointers -> Iterators
1 | vector<string> svec; |
- if vec empty,
svec.begin()/.end()
are equal - double colon [
::
]: nested type - iterator sam syntax as pointer, e.g. deref by
*iter
,iter->size()
const vector & const iterator
1 | const vector<string> cs_vec; |
reimplementation of display()
, find()
…
Other note
Basics
How to Write a C++ Program
int main()
,main
not a language keyword…,return 0
…inserted auto
Defining and Initializing a Data Object
=
& constructor syntax for init [p16]
Expressions
- integer division, conditional op (
?:
) - conditional expr: 0 =>
false
, non-0 =>true
[p19] - arithmetic expr:
false
=>0
,true
=>1
[p22]
Conditional & Loop Stmt
if
stmt: stmts with | without{}
[p23],if-else if-else
[p24],switch
(case
label: constant expr,break
) [p25]while
,break
,continue
[1.4.2]for
loop [p29]
Function
- bubble sort
- terminate the program:
exit(-1);
&#include <cstdlib>
- Function Overloading [2.6]
Engineering principle
- trust is not a sound engineering principle
- better to communicate between functions using parameters rather than use objects defined at file scope
- to solve a communications problem between functions:
- introduce an object at file scope: complicate the independence and understandability of individual functions
- using parameters [p52]
Miscellaneous
- fundamental data types…
- character literal: printing & nonprinting… [p12]
- three floating point size types
float
double
long double
double usr_score = 0.0;
, character type & escape seq,const double pi = 3.14159;
- template class
- element indexing
[]
& off-by-one err - function prototype [p39]
- ineluctable modality of the computer &
#include <limits>; int max_int = numeric_limits<int>::max()
- forward declaration
- randomize:
rand()
andsrand()
functions:#include <cstdlib>