Lecture+15 - Engineering 101 Engineering 101 Lecture 15...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Engineering 101 Lecture 15 10/25/07 Static Variables, Scope, and Lifetime Quote of the Day Quote of the Day Our greatest glory is not in never falling, but in getting up every time we do. ­ Confucius Exam 2 Overview Exam 2 Overview Exam 2 Overview Exam 2 Overview 1024+256+64+32+4+1 = 1381 256+128+64+8+4 = 460 000000011001 010110011001 110001000111 + 1 = 110001001000 Exam 2 Overview Exam 2 Overview 8 35 29 16 Exam 2 Overview Exam 2 Overview -17 15 9 2 6 -6 -9 -16 14 8 1 5 -5 -8 Exam 2 Overview Exam 2 Overview Y 4 3 2 1 x == (y*2+10) || (10­y*2) X 2, 18 4, 16 6, 14 8, 12 Y 9 8 7 6 x == (y*2­10) || (30­y*2) X 8, 12 6, 14 4, 16 2, 18 Y X 5 0 20 y == 5 Y X 10 10 … 10 0 10 x == 10 Y X 4 10 … 10 0 10 x == 10 Exam 2 Overview int main() { ofstream outfile("graph.txt"); ofstream for (int y = 10; y >= 0; y--) { for for (int x = 0; x <= 20; x++) for if (y > 5) { if if ((x == y*2-10) || (x x == 10) == outfile << "X"; outfile else else outfile << "."; outfile } else if (y == 5) else outfile << "X"; outfile else { else if ((x == y*2+10) || (x if x == 10) == outfile << "X"; outfile else else outfile << "."; outfile } } outfile << endl; outfile } for (int y = 5; y > 0; y--) { for for (int x = 0; x <= 20; x++) for if (x == 10) if outfile << "X"; outfile else else outfile << "."; outfile } outfile << endl; outfile } outfile.close(); outfile.close(); return 0; return } { == 20 - (y*2-10)) || == 20 - (y*2+10)) || { And now for something a bit And now for something a bit different… Characters can be converted to integers. We denote char literals by enclosing them in single quotes like: ‘F’ There are a number of whitespace characters to denote spaces ‘ ’, tabs ‘\t’, newlines ‘\n’, etc. Last Lecture: char type holds Last Lecture: char type holds individual characters Last Lecture: Reading and Writing Last Lecture: Reading and Writing char’s Characters can be read in from input streams and written to output streams char c; cin >> c; cout << c << endl; Usually whitespace char’s are ignored during input unless you specifically turn this feature off. cin.unsetf(ios::skipws); Last Lecture: Overflow Example Last Lecture: Overflow Example Normally, at most seven fruits are displayed, regardless of the current screen, but since the level number is stored in a single byte, level 256 (100h) rolls over to 0h in the subroutine, and 256 fruit are drawn, corrupting the bottom of the screen and the entire right half of the maze. Usually when an identifier is declared the computer sets aside memory in which to store the associated data. The memory is then available for use. It can be read and altered during the course of the program. However, sometimes we may want to treat certain data specially. Modifications to the Modifications to the Usage of Data Constants Constants If you want a variable to never change in the context of the code you can define it to be a const const double speedoflight = 3e8; Note that const does not replace the type, but rather it modifies the data type. Aliases (References) Aliases (References) We can declare a new variable that is a stand­in for an other variable. double v = 5.0; double & w = v; cout << w << endl; The above code will print 5.0 because v and w are the same variable. Aliases (References) Aliases (References) We have already used such variables implicitly when we do pass­by­reference. We can make a reference that is a const. That means that the reference version cannot be changed. double v=5.0; const double & w = v; v = v + 1.0; w = w + 1.0; This can be useful for passing data without having to copy it, but also without allowing changes. Scope Scope Each identifier has a context in which it is meaningful. Outside that context it cannot be referred to or used. This context is called its scope. Scope Scope An identifier in a compound statement has local scope. It is in scope from the time it is declared to the end of the enclosing braces. A function parameter is in local scope from the beginning of the function body. Scope Scope void f ( double x) { int i = 1; cout << i << “ “; int j = 2; cout << j << endl; cout << x << endl; } x i j Scope Scope You cannot declare two identifiers with the same name on the same compound statement. You can declare an identifier with the same name in a different compound statement. If there are different possible identifiers that could be referred to, the most local version is always chosen. The less local versions are “hidden”. Scope Scope main () { int i = 5; { cout << i << endl; // This will print 5 int i = 2; cout << i << endl; // This will print 2 } } Lifetime Lifetime The lifetime is the span of time during which a particular data object can be accessed. For identifiers in a compound statement this lasts from the line of the declaration to the time when the closing brace for the compound statement is reached. Lifetime Lifetime The lifetime of parameters in functions or procedures is the time spent in the function or procedure body until control returns to the caller. When the lifetime expires the data object is returned to memory for general use. Referring to the identifier after this time can produce strange results. Global Constants Global Constants Some objects have local scope. A good example are functions and procedures themselves. You can make identifiers have global scope. This is a bad idea for non­constant identifiers, since they can change in a procedure or function without explicitly being passed in. int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; } int main () { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope and Lifetime Scope and Lifetime What does this program do? int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; } First s and t int main () are declared { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope Scope s 1 t 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; } main( ) int main () begins { execution swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope Scope s 1 main t 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; } main calls int main () swap( s, t ) { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope Scope s 1 main swap t 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; swap is passed return; s and t by } reference as int main () x and y { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope main s 1 t 2 Swap x y int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; swap prints t. return; But there is no } local “t” so it int main () prints global t { swap(s,t); cout <<s << ‘ ‘ << t << endl; return 1; } Scope s 1 main Swap x y t 2 t? cout 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; Now a new local t } Is created and int main () a value is assigned { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope s 1 main Swap x t1 y t 2 cout 2 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; swap prints t. } But this time int main () it prints local t { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope s 1 main Swap x t1 y t 2 cout 2 1 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; Then x and y } Are swapped int main () { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope main s 2 t 2 Swap x t1 y cout 2 1 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; Then x and y } Are swapped int main () { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope main s 2 t 1 Swap x t1 y cout 2 1 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; return; The procedure } returns int main () { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope s 2 main Swap x t1 y t 1 cout 2 1 int s=1, t=2; void swap(int& x, int& y){ cout << t << endl; int t = x; cout << t << endl; x=y; y=t; Then, returning return; to the main the } global s and t are int main () printed { swap(s,t); cout << s << ‘ ‘ << t << endl; return 1; } Scope s 2 t 1 cout 2 1 21 What will be the What will be the 1st cout in the code? 1­ 2 10 2­ 2 12 3­ 5 10 4­ 3 13 What will be the What will be the 1st cout in the code? 1­ 2 10 2­ 2 12 3­ 5 10 4­ 3 13 What will be the What will be the 2nd cout in the code? 1­ 2 10 2­ 2 12 3­ 4 14 4­ 3 13 What will be the What will be the 2nd cout in the code? 1­ 2 10 2­ 2 12 3­ 4 14 4­ 3 13 What will be the What will be the 3rd cout in the code? 1­ 2 10 2­ 2 14 3­ 4 14 4­ 3 13 What will be the What will be the 3rd cout in the code? 1­ 2 10 2­ 2 14 3­ 4 14 4­ 3 13 #include <iostream> using namespace std; int a=5, b=10; void proc(int & a){ a = a + 1; b = b + 1; return; } int main(){ cout << a << “ “ << b << endl; proc(b); int a=2; cout << a << “ “ << b << endl; proc(a); cout << a << “ “ << b << endl; return 1; } Example 1 Example 1 Next Lecture Next Lecture Strings and Generic Types ...
View Full Document

This note was uploaded on 03/12/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Fall '07 term at University of Michigan.

Ask a homework question - tutors are online