Download presentation
Presentation is loading. Please wait.
Published byAldo Marple Modified over 9 years ago
1
Expression Evaluation, Scope, Parameter Passing, Data Organization, Program Control, Binding By Ramon Quiusky CS 490 Fall 2003
2
Expression Evaluation Expressions are evaluated according to the precedence and order of their operators. Consider the following examples:
3
Ex. 1 Multiplication has the highest priority. Addition has the next highest precedence Left shift has the lowest precedence.
4
Ex. 2
5
Scope Variables that are declared within the body of a function definition are said to be local to that function or to have that function as their scope. A variable within a function definition can have the same name of a variable within another function of main function definition (Static Scope).
6
5 Types of Scope 1. Local scope: a name declared within a block is accessible only within that block and blocks enclosed by it. { int i; }
7
2. Function scope: Labels are the only names that have function scope. They can be used anywhere within a function, but are not accessible outside that function. 3. File scope: Any name declared outside all blocks or classes has file scope. It is accessible anywhere in the translation unit after its declaration. Also known as Global Variables.
8
4. Class scope: Names of class members have class scope. Class member functions can be accessed only by using the member- selection operators (. or –>) or pointer-to-member operators (.* or –>*) on an object or pointer to an object of that class. 5. Prototype scope: Names declared in a function prototype are visible only until the end of the prototype. The following prototype declares two names (szDest, szSource); these names go out of scope at the end of the prototype: char *strcpy( char *szDest, const char *szSource );
9
Parameter Passing Two main parameter types: 1. Call-by-value 2. Call-by-reference But first, what are formal parameters and arguments?
10
Formal Parameter: a kind of blank space holder that is filled in with something when the function is called. They are listed in a function prototype and used in the body of a function definition. Argument: something that is used to fill in a formal parameter. When a function call is execute, the arguments are “plugged in” for the formal parameters.
11
Example: //libraries (I/O, math, etc) //prototype: int sum(int a, int b); //======= //main function //function call: cout << “Sum = “ << sum(24, 25) << endl; //======= int sum(int a, int b) { return (a+b); }
12
1. Call-by-value: a formal parameter that when listed in the prototype of a function there is no ampersand (“&”) attached to its type specification ex: int sum(int a, int b);
13
2. Call-by-value: a formal parameter that when listed in the prototype of a function there is an ampersand (“&”) attached to its type specification
14
#include void get_number(int& input1, int& input2); void show_results(int output1, int output2); int main() { int first_num, second_num; get_number(first_num, second_num); show_results(first_num, second_num); return 0; } void get_number(int& input1, int& input2) { cin >> input1; cin >> input2; } void show_results(int output1, int output2) { cout << "\n\nnumber: " << output1 << " " << output2 << endl<< endl; }
15
Functions can also be used as parameters. Ex.: //function call: sum(get_first_num(), get_sec_num()); int sum(int a, int b); { return (a+b);} int get_first_num() {int a; cin>>a; return a;} int get_sec_num() {int b; cin>>b; return b;}
16
Constructs for Data Organization What is a data structure? An organization of information, usually in memory, for better algorithm efficiency, such as queue, stack, linked list, heap, dictionary, and tree, or conceptual unity, such as the name and address of a person. It may include redundant information, such as length of the list or number of nodes in a subtree.algorithmefficiencyqueuestack linked listheapdictionarytreelistnodessubtree
17
Static Data Structure: size determined when a program is compiled Dynamic Data structure: size determined when a program is actually running rather than at compilation time Pointers enable us to define this type of data structure
18
Other types of data structures: external memory data structure passive data structure active data structure persistent data structure recursive data structure See http://www.nist.gov/dads/ for definitionshttp://www.nist.gov/dads/
19
Program Organization A collection of program units constitutes an executable program. Program units can contain other smaller units. A program consists of the following (from a Fortran Program): Main program External subprogram (subroutine or function) Module Block data
20
Program Control It is good practice to break a program down into pieces that can be thought of independently. Once the program has been completed, we can think of its execution as being a series of these pieces that work together in a certain sequence. These pieces then pass the control of the program between each other.
21
Examples if-else Statements while Loops for Loops continue Statement break Statement
22
Nesting Control Statements if (my_money > cost_of_CD) then buy_CD Else { if (see_a_friend) then borrow_money else get_a_job }
23
Binding Linking between messages and methods Early Binding: Static binding. Binding of functions at compile time AddRealNums (x, y) x & y are of type real Late Binding: Late Binding. Binding of message selector to the appropriate method at run time AddNumber(x, y) x & y are number objects
24
Static Type: what can be determined at compile time Dynamic Type: actual type as determined during run time.
25
Static Binding Vs Dynamic Binding Static binding: binding based on static type More efficient Less flexible Static type checking leads to safer programs Dynamic Binding: More flexible Less efficient May need dynamic type checking
26
Resources Data Structures and other objects using C++ - Savitch Problem Solving with C++ - Savitch Java: How to Program – Deitel & Deitel http://www.csd.uu.se/kurs/oop/ht98/Lectures/D5/html/Dynamic _Binding/sld019.htm http://www.csd.uu.se/kurs/oop/ht98/Lectures/D5/html/Dynamic _Binding/sld019.htm http://msdn.microsoft.com/library/ http://www.nist.gov/dads/
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.