Friday, February 1, 2007

Preview:

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 mansoor@lums.edu.pk. - PowerPoint PPT Presentation

Citation preview

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

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;

}

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!

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;

}

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

int i=1;

while(*words2[i])

{

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

i+=3;

}

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;

}

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

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

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

0x0012FEC8 0x0012FEC80x0012FED4 0x0012FED4 0x0012FED410

Multiple Indirection

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?

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?

int **ppi=?

//what comes here?

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

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

const char*sPtr

sPtr is a Pointer to a character constant

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

}

}

SELF TEST: const

// This is wrong.

void code(const char *str)

{

while(*str) {

*str = *str + 1;

cout << (char) *str;

str++;

}

}// Error ?

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 ?

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 ?

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;

}

SELF TEST: const

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

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

e.g.

static int count=450;

static Variables

void fs(void){

static int s=2;

s++;

cout<<s<<endl;

}

int main() {

fs();

fs();

fs();

return 0;

}

static Variables

3

4

5

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;

}

static Variables

int r_avg(int i)

{

int sum=0, count=0;

sum = sum + i;

count++;

return sum / count;

}

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

}

static Local Variables

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)

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

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

int abs ( int n );

long int labs ( long int n );

double fabs ( double x );

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

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

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

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

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

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

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