1 / 40

Friday, February 1, 2007

Friday, February 1, 2007. Minds are like parachutes. They only function when they are open. - Sir James Dewar, Scientist (1877-1925). Please note!. TA: Mansoor Akhtar Email: mmansoor @lums.edu.pk Email is NOT mansoor@lums.edu.pk.

bardia
Download Presentation

Friday, February 1, 2007

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Friday, February 1, 2007 Minds are like parachutes. They only function when they are open. - Sir James Dewar, Scientist (1877-1925)

  2. Please note! TA: Mansoor Akhtar Email: mmansoor@lums.edu.pk Email is NOT mansoor@lums.edu.pk

  3. char wordsb[11][80]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" }; int i=1; while(strcmp(wordsb[i],"")) { cout<<wordsb[i]<<" "; i+=3; }

  4. char *words2[11]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" }; int i=1; while(strcmp(words2[i],"")) { cout<<words2[i]<<" "; i+=3; } Memory Drawing!

  5. char *words2[11]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" }; int i=1; while(words2[i][0]) { cout<<words2[i]<<" "; i+=3; }

  6. char *words2[11]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" }; int i=1; while(*words2[i]) { cout<<words2[i]<<" "; i+=3; }

  7. char *words2[11]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" }; int i=1; while(*(*(words2+i)+0)) { cout<<words2[i]<<" "; i+=3; }

  8. Multiple Indirection • int **balptr2ptr; //variable that is a pointer to a pointer • balptr2ptr is a pointer to an int pointer Pointer Pointer Variable address address value

  9. Multiple Indirection int main() { int x, *p, **q; x = 10; p = &x; q = &p; cout << q <<" "<<&p<<endl; cout << *q <<" "<<p<<" "<<&x<<endl; cout << **q <<endl; return 0; }

  10. Multiple Indirection int main() { int x, *p, **q; x = 10; p = &x; q = &p; cout << q <<" "<<&p<<endl; cout << *q <<" "<<p<<" "<<&x<<endl; cout << **q <<endl; // prints the value of x return 0; }

  11. Multiple Indirection 0x0012FEC8 0x0012FEC8 0x0012FED4 0x0012FED4 0x0012FED4 10

  12. void f1(int* &num) { cout<<*num<<endl; num++; num++; cout<<*num<<endl; } int main(void){ int a[8]={1,2,3,4,5,6,7,8}; int *aptr=a; f1(aptr); cout<<*aptr<<endl; return 0;} //OUTPUT?

  13. void f1(int** num) { cout<<*(*num)<<endl; (*num)++; (*num)++; cout<<*(*num)<<endl; } int main(void){ int a[8]={1,2,3,4,5,6,7,8}; int *aptr=a; int **aptr_ptr; aptr_ptr = &aptr; f1(aptr_ptr); cout<<*aptr<<endl; return 0; } //OUTPUT?

  14. int **ppi=? //what comes here?

  15. pointers – another example char *words[11]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" }; char **ptr=words; ptr++; while(**ptr) { cout<<*ptr<<" "; ptr+=3; }

  16. SELF TEST: const • Variables declared with const modifier cannot have their values changed during the execution of program. const float f=56.4; • The variable can be used in expressions. • Most common use is to create const pointer parameters • A const pointer prevents the object pointed to by a pointer parameter from being modified by a function.

  17. SELF TEST: const const char*sPtr sPtr is a Pointer to a character constant

  18. SELF TEST: const void code(const char *str); int main() { code("this is a test"); cout<<endl; return 0; } void code(const char *str) { while(*str) { cout << (char) (*str+1); str++; } }

  19. SELF TEST: const // This is wrong. void code(const char *str) { while(*str) { *str = *str + 1; cout << (char) *str; str++; } } // Error ?

  20. SELF TEST: const // This is wrong. void code(const char *str) { while(*str) { *str = *str + 1; //can’t modify *str cout << (char) *str; str++; } } // Error ?

  21. SELF TEST: const // const references cannot be modified. void f(const int &i); int main() { int k = 10; f(k); return 0; } void f(const int &i) { i = 100; cout << i; } // Error ?

  22. SELF TEST: const void printCharacters(const char *); main() { char string[] = "print characters of a string"; cout << "The string is:" << endl; printCharacters(string); cout << endl; return 0; } void printCharacters(const char *sPtr) { for ( ; *sPtr != '\0'; sPtr++) // no initialization cout << *sPtr; }

  23. static Variables • Variables of type static are permanent variables within their own functions or file. • They differ from global variables because they are not known outside their function or file. static local variables • When the static modifier is applied to a local variable, they maintain their values between function calls. (That is the value is not lost when the function returns) • Local static variables are initialized only once, when program execution begins.

  24. static Variables • To declare a static variable, precede its type with the word static. e.g. static int count=450;

  25. static Variables void fs(void){ static int s=2; s++; cout<<s<<endl; } int main() { fs(); fs(); fs(); return 0; }

  26. static Variables 3 4 5

  27. static Variables int r_avg(int i); int main() { int num; do { cout << "Enter numbers (-1 to quit): "; cin >> num; cout << "Running average is: " << r_avg(num); cout << '\n'; } while(num > -1); return 0; }

  28. static Variables int r_avg(int i) { int sum=0, count=0; sum = sum + i; count++; return sum / count; }

  29. static Local Variables int r_avg(int i) { static int sum=0, count=0; /* initialization of static variables occurs only once, not each time the function is entered*/ sum = sum + i; count++; return sum / count; }

  30. static Global Variables (First File) int r_avg(int i); void reset(); int main() { int num; do { cout << "Enter numbers (-1 to quit, -2 to reset): "; cin >> num; if(num==-2) { reset(); continue; } cout << "Running average is: " << r_avg(num); cout << '\n'; } while(num != -1); return 0; }

  31. static Global Variables // ---------------------- Second File ---------------------- static int sum=0, count=0; int r_avg(int i) { sum = sum + i; count++; return sum / count; } void reset() { sum = 0; count = 0; }

  32. static Global Variables • local static variable is known only to the function or block of code in which it is declared • global static variable is known only to the file in which it resides

  33. int abs ( int n ); long int labs ( long int n ); double fabs ( double x );

  34. Function Overloading // abs() is overloaded three ways. int abs(int i); double abs(double d); long abs(long l); int main(){ int x= -234; long y= 5678; double d= -123.78; cout << abs(x) << "\n"; cout << abs(y) << "\n"; cout << abs(d) << "\n"; return 0; }

  35. Function Overloading int abs(int i) { cout << "using integer abs()\n"; if(i<0) return -i; else return i; } double abs(double d) { cout << "using double abs()\n"; if(d<0.0) return -d; else return d; } long abs(long l) { cout << "using long abs()\n"; if(l<0) return -l; else return l; }

  36. Function Overloading // Overload a function three times. void overlaodedf(int i); // integer parameter void overlaodedf(int i, int j); // two integer parameters void overlaodedf(double k); // one double parameter int main(void){ int x=34, y=56; double d=123.78; overlaodedf(x); overlaodedf(x,y); overlaodedf(d); return 0; }

  37. Function Overloading void overloadedf(int i) { cout << "i is " << i << '\n'; } void overloadedf(int i, int j) { cout << " i is " << i; cout << ", j is " << j << '\n'; } void overloadedf(double k) { cout << "k is " << k << '\n'; }

  38. Default Function Arguments void mystrcat(char *s1, char *s2, int len = 0); int main() { char str1[80] = "This is a test"; char str2[80] = "0123456789"; mystrcat(str1, str2, 5); // concatenate 5 chars cout << str1 << '\n'; strcpy(str1, "this is a test"); // reset str1 mystrcat(str1, str2); // concatenate entire string cout << str1 << '\n'; return 0; }

  39. Default Function Arguments // A custom version of strcat(). void mystrcat(char *s1, char *s2, int len) { // find end of s1 while(*s1) s1++; if(len==0) len = strlen(s2); while(*s2 && len) { *s1 = *s2; // copy chars s1++; s2++; len--; } *s1 = '\0'; // null terminate s1 }

  40. Default Function Arguments void myfunc(double num = 0.0, char ch = 'X') { . . . } myfunc(198.234, 'A'); // pass explicit values myfunc(10.1); // pass num a value, let ch default myfunc(); // let both num and ch default

More Related