26
1/26 Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q&A Conclusion References Files CSC-201 - Computer Science I Lecture #7: Chapter 9 Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge Dr. Chuck Cartledge October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am October 4, 2016 at 11:20am

CSC-201 - Computer Science I Lecture #7: Chapter 9ccartled/Teaching/2016-Fall-TCC/Lectures/007.pdf · Schedule Miscellanea Chap. 9Break Test preps.Hands onQ & AConclusionReferencesFiles

  • Upload
    doannhi

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

  • 1/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    CSC-201 - Computer Science ILecture #7: Chapter 9

    Dr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck CartledgeDr. Chuck Cartledge

    October 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20amOctober 4, 2016 at 11:20am

  • 2/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Table of contents I

    1 Schedule

    2 Miscellanea

    3 Chap. 9

    4 Break

    5 Test preps.

    6 Hands on

    7 Q & A

    8 Conclusion

    9 References

    10 Files

  • 3/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Schedule for the semester:

    Wk. Date Topic Wk. Date Topic1 08/22 Chaps. 1 2 X 9 10/17 Chap. 102 08/29 Chaps. 3 4 X 10 10/24 Chaps. 10 113 09/05 Chap. 5 X 11 10/31 Chaps. 10 114 09/12 Test Chap. 6 X 12 11/07 Test Chap. 145 09/19 Chap. 7 X1 13 11/14 Chap. 146 09/26 Chap. 8 X 14 11/28 Chap. 187 10/03 Chap. 9 15 12/05 Chap. 188 10/10 Test Chap. 10 16 12/12 Exam

    Notes:

    1 Class canceled due to electrical fire in instructors home

  • 4/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Programming assignment #003

    File Based Address Book

    Objectives are fairly straightforward:

    Accept commands from the user

    Read strings from a data file

    Search strings for data from user

    Output strings to the screen in aparticular format, or outputmessage

    Submit your source code.

    This is a single person effort (not ateam effort).

    Due by start of class 5 Oct. 2016.

  • 5/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Programming assignment #003

    Q & A time.

    Q: Whats a light-year?A: One-third less calories than aregular year.

    Now is the time to ask questions.

  • 6/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Corrections and additions since last lecture.

    Graded all homework to date

    Homework for chapters 7and 8 due before class today

    Started gradingprogramming assignment#003

  • 7/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    Nothing lives forever

    We can create programs that aregreat and wonderful, but:

    1 They all have a begining andan end.

    2 Each part of them has theirown life.

    3 Each part of them can haveparents and children.

    Image from [1].

    Today we will talk about the life of variables. Called scope

  • 8/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    Definitions

    The scope of an identifier (or named constant) is the region ofprogram code in which it is legal to use that identifier for anypurpose.Types of scope:

    Local : The scope of an identifier that is declared inside ablock (this includes function parameters) extends fromthe point of declaration to the end of the block

    Global : The scope of an identifier that is declared outside of allnamespaces, functions, and classes extends from point ofdeclaration to the end of the entire file containing theprogram code.

    Scope[2]

  • 9/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    Details about the scoping rules

    1 Function names have global scope

    2 A function parameters scope is identical to the scope of a localvariable declared in the outermost block of the function body

    3 A global variables (or constants) scope extends from itsdeclaration to the end of the file, except as noted in rule 5

    4 A local variables (or constants) scope extends from itsdeclaration to the end of the block in which it is declared,including any nested blocks, except as noted in rule 5

    5 An identifiers scope does not include any nested block thatcontains a locally declared identifier with the same name(local identifiers have name precedence)

  • 10/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    What does all that mean from a practical sense?

    1 An identifier exists at global scope, or inside a set of curlybraces

    2 The compiler looks in the current context, then outwards untilthe identifier definition is found

    3 If there are identifier name conflicts, the closest one wins

  • 11/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    Namespaces are another wrinkle.

    Only one entity can exist with a particular name in aparticular scope. This is seldom a problem for localnames, since blocks tend to be relatively short, andnames have particular purposes within them, such asnaming a counter variable, an argument, etc...But non-local names bring more possibilities for namecollision, especially considering that libraries may declaremany functions, types, and variables, neither of themlocal in nature, and some of them very generic.Namespaces allow us to group named entities thatotherwise would have global scope into narrower scopes,giving them namespace scope. This allows organizing theelements of programs into different logical scopes referredto by names.

    T. Staff [3]

    The scope of an identifier declared in a namespace definitionextends from the point of declaration to the end of the namespacebody, and its scope includes the scope of a using directivespecifying that namespace

  • 12/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    How declare and use a namespace

    How to create a namespace indentifier:

    namespace csc201nameSpace{

    int i;

    int j;

    }

    How to use a namespace identifier:1 Use a qualified name consisting of the namespace, the scope

    resolution operator :: and the desired the identifiercsc201nameSpace::i = 56;

    2 Write a using directive locally or globallyusing namespace csc201nameSpace;

    3 Write a using declarationusing std::abs;

    alpha = abs(beta);

  • 13/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    Memory allocation

    Somethings in your source code will cause memory allocation,other things wont.Those that cause allocation, consume computer resources duringprogram execution.Those that done cause allocation, are used by the compiler while itbuilds the executable program.

  • 14/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    Image from [2].

  • 15/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    Image from [2].

  • 16/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Scope and lifetime

    Lifetime of a Variable

    Some last longer than others.

    The lifetime of a variable is the time during program executionin which an identifier actually has memory allocated to it

    There are different types of variables

    Local variables are allocated and alive while the function isexecuting. They are lost when the function terminates.Global variables are allocate and alive while the the program isexecuting. They are lost when the program terminates.Automatic variables are allocated at block entry, and lost atblock exit.Static variables remain allocated throughout the life of theprogram.

  • 17/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    More about functions

    Our friends, pass parameters

    Basically, two types of pass parameters.

    Pass-by-value: the value of the variable (its contents) arepassed to the called function

    Pass-by-reference: the address of the variable is passed tothe called function

    Things to remember:

    Pass-by-value: the called function CANNOT change thingsin the calling function

    Pass-by-reference: the called function CAN change thingsin the calling function

    Look at readAddressBook01.cpp from the programmingassignment.

  • 18/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    More about functions

    Should a function return a value or not?

    1 If it must return more than one value or modify any of thecallers arguments, do not use a value-returning function

    2 If it must perform I/O, do not use a value-returning function

    3 If there is only one value returned, and it is Boolean, avalue-returning function is appropriate

    4 If there is only one value returned, and that value will be usedimmediately in an expression, a value-returning function isappropriate

    5 When in doubt, use a void function; you can recode anyvalue-returning function as a void function by adding an extraoutgoing parameter

    6 If both void and value-returning are acceptable, use the oneyou prefer

  • 19/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Break time.

    Take about 10 minutes.

  • 20/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Some ideas

    Broad things from different chapters

    1 Loops and loops and looping again(different types and how are theydifferent)

    2 Control structures (different types,different use cases)

    3 Functions (their parts, theirrequirements, ways to returnthings)

    4 Scoping (how long do differentthings live, are visible, and why)

    5 Lots of vocabulary and orderingthings

    6 More vocabulary and basic syntax

  • 21/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Some ideas

    There will be an exam.

    Cover chapters 6 through 9(inclusive)

    Exam has not been writtenyet

    Exam should take about anhour(ish)

    When finished with exam,leave room with answers

    Lecture will start after examends

    Any questions? Now is the time to ask.

  • 22/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Practice exercises

    Programs to load into CodeBlocks and get running:

    1 scope01.cpp to look at scoping, and get our hands dirty

    Others, if you have time.

  • 23/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Q & A time.

    Q: Why do mountain climbersrope themselves together?A: To prevent the sensible onesfrom going home.

  • 24/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    What have we covered?

    Covered chapter 9Began to understand how somethings are short lived, while othersare longerTalked about functions in differentforms and flavors

    Next time: Chapter 9 homework before class, Chapter 10

  • 25/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    References I

    [1] Astronmical pictures, http://www.astrogb.com/index.htm,2016.

    [2] Sylvia Sorkin, Programming and problem solving with c++,Teacher Resource Jones and Barlett Learning, 2014.

    [3] TutorialsPoint Staff, Name visibility,http://www.cplusplus.com/doc/tutorial/namespaces/,2016.

    http://www.astrogb.com/index.htmhttp://www.cplusplus.com/doc/tutorial/namespaces/

  • 26/26

    Schedule Miscellanea Chap. 9 Break Test preps. Hands on Q & A Conclusion References Files

    Files of interest

    1 scope01.cpp (looking at variable

    scoping)

    2 nameSpace.cpp (looking at

    namespace variable scoping)

    3 static.cpp (looking at static

    variables)

    4 readAddressBook01.cpp (looking at

    pass by reference and value)

    scope01.cpp#include

    #defineDEBUG_PROGRAM

    #ifdefDEBUG_PROGRAM

    #defineDEBUG(a){std::clog