Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 121-200 Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup.

Similar presentations


Presentation on theme: "CSCE 121-200 Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup."— Presentation transcript:

1 CSCE 121-200 Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup and Jennifer Welch

2 CSCE 121:509-512 Set 6: Miscellaneous Range-Based Loops Simpler way to iterate over all elements of a vector: vector v; // push_back items into v for (int x : v) { // for all items x in v cout << x; for (auto x : v) { // compiler figures out type of x cout << x; 2

3 CSCE 121:509-512 Set 6: Miscellaneous Declarations A declaration – introduces a name into a scope (informally, a part of the program; more later) – Specifies a type for the named object – Might include an initializer A name must be declared before it can be used int a = 7; // int variable double sqrt(double); // function, double arg and ret vector v; // vector of doubles 3

4 CSCE 121:509-512 Set 6: Miscellaneous Placement of Functions Since names must be declared before being used, you must declare functions used in main before main actually appears. If function f1 calls function f2, then f2 must be declared before f1 is defined. See nested-calls.cpp 4

5 CSCE 121:509-512 Set 6: Miscellaneous Definitions A definition is a declaration that also fully specifies the entity int a = 7; vector v; // empty vector of doubles double sqrt(double) { … } // function with a body Examples of declarations that are not definitions: double sqrt(double); // function w/o a body T x; // user-defined type T is specified elsewhere 5

6 CSCE 121:509-512 Set 6: Miscellaneous Declaration vs. Definition You cannot define something twice (in the same scope) – Example: you cannot define variable “a” twice in a function, or function “sqrt” twice in a file You can declare something twice – Example: you can declare a function at one point and then later define it (we’ll see a use for this later) 6

7 CSCE 121:509-512 Set 6: Miscellaneous Why Both Declarations and Definitions? To refer to something, we need only its delaration Often we want the definition “elsewhere”: – Later in a file – In another file Declarations specify interfaces – To your own code – To libraries In larger programs, put all declarations in header files to ease sharing 7

8 CSCE 121:509-512 Set 6: Miscellaneous Kinds of Declarations Variables Constants (coming up very soon) Functions Namespaces (coming up very soon) Types (coming up soon) Templates (coming up later) 8

9 CSCE 121:509-512 Set 6: Miscellaneous Header Files Header file contains declarations (of functions, types, constants, etc.) giving an interface to other parts of the program Provides abstraction: don’t have to know details of a function to use it The actual functions, types, etc. are defined in other source code files (yours or libraries) The construct #include “std_lib_facilities_4.h” is a “preprocessor directive” that adds the contents of the file to your program 9

10 CSCE 121:509-512 Set 6: Miscellaneous 10 // declarations: double sqrt(double); … #include ”mymath.h" //definitions: double sqrt(double x) { /* code to calculate square root */ } … #include ”mymath.h" … double x = 122; double y = sqrt(x); … mymath.h: mymath.cpp: use.cpp:

11 CSCE 121:509-512 Set 6: Miscellaneous Scope A scope is a region of program text – Global scope: outside any language construct – Class scope: inside a class (coming soon) – Local scope: between { and } – Statement scope: e.g., in a for-statement A name in a scope is visible (can be used) in that scope and in all nested scopes – But only after the declaration – Exception: class members can be used before they are declared 11

12 CSCE 121:509-512 Set 6: Miscellaneous Scope Example See scope.cpp 12

13 CSCE 121:509-512 Set 6: Miscellaneous Benefits of Scoping A scope keeps names local Prevents my variables, functions, etc. from interfering with yours Real programs have thousands of entities, so we need help from the programming language Locality is good! Keep names as local as possible – Avoid global variables! 13

14 CSCE 121:509-512 Set 6: Miscellaneous Constants To declare a variable whose value should not be changed after initialization: – If value can be determined at compile-time: constexpr double pi = 3.14159; – If value cannot be determined at compile-time (e.g., it depends on user input): const double cd = 8.7; These “variables” must be assigned a value when declared and cannot be assigned to later Use named constants instead of literals! (E.g., 299792458 is unclear but speed_of_light is good) 14

15 CSCE 121:509-512 Set 6: Miscellaneous Namespaces A namespace is a scope with name The :: syntax is used to specify which namespace you are using and which (of many possible) objects with the same name you are referring to This lets different scopes use the same names without clashes 15

16 CSCE 121:509-512 Set 6: Miscellaneous C++ Standard Library Facilities of the C++ standard library include: input/output, strings, vectors, and much more It’s really a collection of libraries – E.g.,,, All these libraries are in the std namespace To use them in your program, do this: #include To use names in one of these, prefix with std::, e.g., std::cout 16

17 CSCE 121:509-512 Set 6: Miscellaneous using Declarations and Directives To avoid the tedium of prefixing names with std:: Using declaration for one particular name: using std::cout; cout << “hi”; Using directive for all names in the namespace: using namespace std; cout << “enter value for x”; cin >> x; 17

18 CSCE 121:509-512 Set 6: Miscellaneous Acknowledgments Slides are based on those for the textbook: http://www.stroustrup.com/Programming/8_functions.ppt 18


Download ppt "CSCE 121-200 Introduction to Program Design and Concepts J. Michael Moore Spring 2015 Set 6: Miscellaneous 1 Based on slides created by Bjarne Stroustrup."

Similar presentations


Ads by Google