Presentation is loading. Please wait.

Presentation is loading. Please wait.

Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects.

Similar presentations


Presentation on theme: "Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects."— Presentation transcript:

1 Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects Dynamic objects Functions on objects structured programming (104), modularity (member) variables OOP (104, 151) Data structure: Linear: list, stack, queue Nonlinear: tree, graph Algorithms Algorithms+Data Structures = Programs Niklaus Wirth (171) (member) functions Array, struct pointer objects Data, variable, object Operation, function, procedure, subprogram, module, method operation class C, Pascal C++, Java

2 Slide 2 int main() { int x,y,z; int a,b,c; a=f1(x); b=f2(y); c=f3(z); … } int f1() { } int f2() { } int f3() { } main(), is the first function, and is composed of a sequence of ‘functions’. int main() { A a; B b; C c; a.f1(); b.f2(); c.f3(); … } Class A { Int x; Int f1(); } Class B { Int y; Int f2() } Class C { Int z; Int f3(); } Structured programming: Object oriented programming: a sequence of ‘objects’!

3 Slide 3 Programming paradigms * Modular programming (functions) * Object-oriented programming (classes) * Generic programming (templates)

4 Slide 4 l Pass by value: formal parameters and arguments are different variables. ideal desirable behavior (but not efficient some times) l Pass by reference: they are the same variables, but different names! should carefully handled! Communication between functions:

5 Slide 5 Reference: X& l int& b a; l b is an alternative name for a void f(int& b) {}; int main() { int a; f(a); } int a=10; int& b = a; int& c = a; b = 100; a ??? int& b; 10 a b c Relationship with pointers (later on)!

6 Slide 6 int f(int x) { cout << “value of x = “ << x << endl; x = 4; } main() { int v = 5; f(v); cout << “value of v = “ << v << endl; } Output: Value of x = Value of v = n When a variable v is passed by value to a function f, its value is copied to the corresponding variable x in f n Any changes to the value of x does NOT affect the value of v n Call by value is the default mechanism for parameter passing in C++ 5 5 Call by Value

7 Slide 7 int f(int &x) { cout << “value of x = “ << x << endl; x = 4; } main() { int v = 5; f(v); cout << “value of v = “ << v << endl; } Output: Value of x = Value of v = n When a variable v is passed by reference to a parameter x of function f, v and the corresponding parameter x refer to the same variable n Any changes to the value of x DOES affect the value of v 5 4 Call by Reference

8 Slide 8 int f( const int &x ) { cout << “value of x = “ << x << endl; x = 4; // invalid } main() { int v = 5; f(v); cout << “value of v = “ << v << endl; } Passing variable v by constant reference to parameter x of f will NOT allow any change to the value of x. It is appropriate for passing large objects that should not be changed by the called function. Call by Constant Reference

9 Slide 9 * Call by value n for small objects that should not be changed by the function * Call by constant reference n for large objects that should not be changed by the function * Call by reference n is appropriate for all objects that may be changed by the function, n not recommended!!! rare! Parameter Passing

10 Slide 10 * return by value, n for small objects that should not be changed by the function * return by constant reference, n for large objects that should not be changed by the function * return by reference, n for all objects that may be changed by the function, n not recommended!!! rare! Return Passing

11 Slide 11 Don ’ t do this (very bad!!!) int& foo(){ int local; return local; }

12 Slide 12 Scope of variables The scope of a declaration is the block of code where the identifier is valid for use. n A global declaration is made outside the bodies of all functions and outside the main program. It is normally grouped with the other global declarations and placed at the beginning of the program file. n A local declaration is one that is made inside the body of a function. Locally declared variables cannot be accessed outside of the function they were declared in. Local to a function (the variables in Main are also local, local to ‘main’ function) n It is possible to declare the same identifier name in different parts of the program : local to a block Some code enclosed in braces

13 Slide 13 int main() { int x,y,z; … } void f() { int x; … } void f() { int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; } int x; int main() { x=0; cout << x << endl; int x; x=1; { int x; x=2; cout << x << endl; } cout << x << endl; } Local to blocksLocal to functions Global

14 Slide 14 In a for-loop { int i; for (i=1;i<10;i++) cout << A[i]; } for (int i=1;i<10;i++) cout << A[i]; equivalent

15 Slide 15 Global Variables * Undisciplined use of global variables may lead to confusion and debugging difficulties. * Instead of using global variables in functions, try passing local variables by reference. It is forbidden in structured programming!

16 Slide 16 int MIN; void min(int,int); int main() { int x,y; cin >> x >> y >> endl; min(x,y); cout << MIN; } void min(int a, int b) { if (a<b) MIN=a; else MIN=b; } void min(int,int,int&); int main() { int x,y,mini; cin >> x >> y >> endl; min(x,y,mini); cout << mini; } void min(int a, int b, int& m) { if (a<b) m=a; else m=b; } int min(int,int); int main() { int x,y,mini; cin >> x >> y >> endl; mini=min(x,y); cout << mini; } int min(int a, int b) { int m; if (a<b) m=a; else m=b; return (m); } Summary Global variablePass by reference Pass by value


Download ppt "Slide 1 Where are we, and where to go? Simple types of variables (variables=objects) 3 program structures (assignment, conditional, iteration) Static objects."

Similar presentations


Ads by Google