Friday, February 1, 2007

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

description

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 [email protected]. - PowerPoint PPT Presentation

Transcript of Friday, February 1, 2007

Page 1: 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)

Page 2: Friday, February 1, 2007

Please note!

TA: Mansoor Akhtar

Email: [email protected]

Email is NOT [email protected]

Page 3: Friday, February 1, 2007

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;

}

Page 4: Friday, February 1, 2007

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!

Page 5: Friday, February 1, 2007

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;

}

Page 6: Friday, February 1, 2007

char *words2[11]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" };

int i=1;

while(*words2[i])

{

cout<<words2[i]<<" ";

i+=3;

}

Page 7: Friday, February 1, 2007

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;

}

Page 8: Friday, February 1, 2007

int **balptr2ptr; //variable that is a pointer to a pointer

balptr2ptr is a pointer to an int pointer

Multiple Indirection

address

Pointer

address

Pointer

value

Variable

Page 9: Friday, February 1, 2007

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;

}

Multiple Indirection

Page 10: Friday, February 1, 2007

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 xreturn 0;

}

Multiple Indirection

Page 11: Friday, February 1, 2007

0x0012FEC8 0x0012FEC80x0012FED4 0x0012FED4 0x0012FED410

Multiple Indirection

Page 12: Friday, February 1, 2007

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?

Page 13: Friday, February 1, 2007

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?

Page 14: Friday, February 1, 2007

int **ppi=?

//what comes here?

Page 15: Friday, February 1, 2007

char *words[11]={"Mechanics", "this", "help", "please", "is", "writing", "count", "natural", "select", "thought", "" };

char **ptr=words;ptr++;while(**ptr) {

cout<<*ptr<<" ";ptr+=3;

}

pointers – another example

Page 16: Friday, February 1, 2007

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

parametersA const pointer prevents the object pointed to by a

pointer parameter from being modified by a function.

SELF TEST: const

Page 17: Friday, February 1, 2007

const char*sPtr

sPtr is a Pointer to a character constant

SELF TEST: const

Page 18: Friday, February 1, 2007

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++;

}

}

SELF TEST: const

Page 19: Friday, February 1, 2007

// This is wrong.

void code(const char *str)

{

while(*str) {

*str = *str + 1;

cout << (char) *str;

str++;

}

}// Error ?

SELF TEST: const

Page 20: Friday, February 1, 2007

// This is wrong.

void code(const char *str)

{

while(*str) {

*str = *str + 1; //can’t modify *str

cout << (char) *str;

str++;

}

}// Error ?

SELF TEST: const

Page 21: Friday, February 1, 2007

// 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 ?

SELF TEST: const

Page 22: Friday, February 1, 2007

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;

}

SELF TEST: const

Page 23: Friday, February 1, 2007

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 variablesWhen 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.

static Variables

Page 24: Friday, February 1, 2007

To declare a static variable, precede its type with the word static.

e.g.

static int count=450;

static Variables

Page 25: Friday, February 1, 2007

void fs(void){

static int s=2;

s++;

cout<<s<<endl;

}

int main() {

fs();

fs();

fs();

return 0;

}

static Variables

Page 26: Friday, February 1, 2007

3

4

5

static Variables

Page 27: Friday, February 1, 2007

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;

}

static Variables

Page 28: Friday, February 1, 2007

int r_avg(int i)

{

int sum=0, count=0;

sum = sum + i;

count++;

return sum / count;

}

static Variables

Page 29: Friday, February 1, 2007

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;

}

static Local Variables

Page 30: Friday, February 1, 2007

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;

}

static Global Variables (First File)

Page 31: Friday, February 1, 2007

// ---------------------- 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;

}

static Global Variables

Page 32: Friday, February 1, 2007

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

static Global Variables

Page 33: Friday, February 1, 2007

int abs ( int n );

long int labs ( long int n );

double fabs ( double x );

Page 34: Friday, February 1, 2007

// 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; }

Function Overloading

Page 35: Friday, February 1, 2007

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;

}

Function Overloading

Page 36: Friday, February 1, 2007

// 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;

}

Function Overloading

Page 37: Friday, February 1, 2007

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';

}

Function Overloading

Page 38: Friday, February 1, 2007

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;

}

Default Function Arguments

Page 39: Friday, February 1, 2007

// 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

}

Default Function Arguments

Page 40: Friday, February 1, 2007

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

Default Function Arguments