2013-‑09-‑17 ¡ 1 ¡
Lecture ¡2 ¡
Func1ons ¡
Variable ¡(1 ¡of ¡2) ¡
Consists ¡of: ¡
- Name ¡
– so ¡we ¡can ¡refer ¡to ¡it’s ¡storage ¡loca1on ¡ – at ¡lower ¡level ¡converted ¡to ¡an ¡adress ¡in ¡memory ¡ – can ¡be ¡aliased ¡by ¡way ¡of ¡reference ¡
- Value ¡
– what ¡we ¡store ¡in ¡the ¡loca1on ¡ – will ¡never ¡be ¡empty, ¡not ¡even ¡before ¡we ¡fill ¡it ¡
- Type ¡
– size ¡of ¡storage ¡loca1on ¡ – interpreta1on ¡of ¡stored ¡value ¡
Variable ¡(2 ¡of ¡2) ¡
Three ¡kinds ¡of ¡variables: ¡
- Fundamental ¡(basic) ¡
– stores ¡a ¡value ¡of ¡fundamental ¡type, ¡nothing ¡more ¡
- Object ¡
– stores ¡values ¡1ed ¡to ¡an ¡derived ¡type ¡(struct, ¡class, ¡union, ¡enum) ¡ – opera1ons ¡associated ¡to ¡the ¡type ¡are ¡provided ¡ – more ¡later ¡in ¡the ¡course ¡
- Pointer ¡
– stores ¡just ¡the ¡adress ¡of ¡some ¡other ¡variable ¡ – requires ¡cau1on: ¡what ¡if ¡the ¡adress ¡does ¡not ¡contain ¡said ¡ variable? ¡ – more ¡later ¡in ¡the ¡course ¡
Constants ¡
- A ¡variable ¡can ¡be ¡declared ¡const ¡
- Modifica1on ¡of ¡a ¡const ¡variable ¡will ¡give ¡compila1on ¡
- error. ¡
- The ¡compiler ¡can ¡treat ¡constant ¡variables ¡more ¡
- efficiently. ¡
- The ¡programmer ¡have ¡less ¡worries ¡with ¡constant ¡
variables ¡than ¡other. ¡Big ¡benefit! ¡
- A ¡const ¡variable ¡is ¡much ¡be4er ¡than ¡a ¡literal ¡because ¡
you ¡refer ¡to ¡it ¡by ¡name, ¡and ¡change ¡it ¡at ¡one ¡place. ¡
- Constants ¡use ¡upper ¡case ¡leVers ¡by ¡conven1on. ¡
const int SIZE{1000};
References ¡
- A ¡varaible ¡can ¡be ¡declared ¡to ¡be ¡an ¡alias ¡for ¡an ¡
already ¡exis1ng ¡variable ¡
- The ¡exis1ng ¡variable ¡gets ¡a ¡second ¡name, ¡but ¡
is ¡in ¡all ¡other ¡aspects ¡iden1cal ¡to ¡the ¡new ¡
- The ¡binding ¡occur ¡only ¡once, ¡at ¡ini1aliza1on ¡
string professor{”C. Kessler”}; string& clever_fellow{professor};
Pointer ¡basics ¡
- A ¡variable ¡that ¡store ¡an ¡adress ¡
- Declared ¡as ¡pointed-‑to-‑type* ¡
- Go ¡to ¡that ¡adress ¡by ¡prefixing ¡the ¡* ¡operator ¡
- Get ¡an ¡adress ¡by ¡prefixing ¡the ¡& ¡operator ¡
- Use ¡nullptr ¡for ¡unset/invalid ¡adresses ¡