220 likes | 352 Views
This document provides a comprehensive overview of fundamental C++ programming concepts, focusing on functions and pointers. It explores different argument-passing mechanisms, such as call by value and call by reference. The text includes practical examples of swapping numbers, converting strings, and using dynamic memory allocation. Key practices for avoiding memory leaks and handling pointers effectively are highlighted to enhance coding efficiency. Additionally, this resource is beneficial for understanding the intricacies of variable manipulation in C++.
E N D
Thursday, January 18, 2007 The question of whether computers can think is just like the question of whether submarines can swim. -Edsger W. Dijkstra (1930 – 2002)
Self Test : String tokenizing: • Skip multiple spaces between words.
void swap(int* sa, int *sb);//prototype int main() { int a=3; int b=5; cout<<a<<" "<<b<<endl; swap(&a, &b); cout<<a<<" "<<b<<endl; return 0; } void swap(int* sa, int *sb){ int temp=*sa; *sa=*sb; *sb=temp; }
Calling Functions There are three ways in C++ to pass arguments to a function: • Call by value • Call by reference with pointer arguments • Call by reference with reference arguments
Call Functions with pointers void f(int *j); int main() { int i=10; int *p; p = &i; // p now points to i f(p); cout << i; // i is now ? return 0; } void f(int *j) { *j = 100; //pointee of j is assigned 100 }
Call Functions with pointers void f(int *j); int main() { int i=10; f(&i); cout << i; return 0; } void f(int *j) { *j = 100; //pointee of j is assigned 100 }
Functions int cubeByValue(int n); // prototype main() { int number = 5; cout <<"The original value of number is " << number << endl; number = cubeByValue(number); cout << "The new value of number is " << number << endl; return 0; } int cubeByValue(int n) { return n * n * n; //cube local variable n }
Calling Functions with Pointers void cubeByReference(int *nPtr); // prototype main() { int number = 5; cout << "The original value of number is " << number << endl; cubeByReference(&number); cout << "The new value of number is " << number << endl; return 0; } void cubeByReference(int *nPtr) { // cube number in main *nPtr = *nPtr * *nPtr * *nPtr; }
WHAT IS WRONG WITH THE FOLLOWING? void swap(int *pa, int *pb);//prototype int main() { int a=3; int b=5; cout<<a<<" "<<b<<endl; swap(&a, &b); cout<<a<<" "<<b<<endl; return 0; } void swap(int *pa, int *pb){ int *temp; temp=pa; pa=pb; pb=temp; }
Calling Functions with Pointers void convert(char *sPtr); main() { char string[] = "characters and $32.98"; cout << "The string before conversion is: " << string << endl; convert(string); cout << "The string after conversion is: " << string << endl; return 0; } void convert(char *sPtr) { while (*sPtr != '\0') { if (*sPtr >= 'a' && *sPtr <= 'z') *sPtr = toupper(*sPtr); ++sPtr; // increment sPtr to point to the next character } }
Dynamic allocation The new operator int *x_ptr; x_ptr=new int; OR int *x_ptr=new int; //heap
Dynamic allocation int *x_ptr=new int; *x_ptr=73; int *x2_ptr=new int; *x2_ptr=65;
Dynamic allocation What is wrong here? int *x_ptr=new int; *x_ptr=73; int *x2_ptr; *x2_ptr=65;
Dynamic allocation int *x_ptr=new int; *x_ptr=73; int *x2_ptr; x2_ptr=x_ptr; *x2_ptr=65;
Dynamic allocation movie…
Dynamic allocation //What is wrong here? int *x_ptr=new int; *x_ptr=73; int *x2_ptr=new int; x2_ptr=x_ptr; *x2_ptr=65;
Dynamic allocation //What is wrong here? int *x_ptr=new int; *x_ptr=73; int *x2_ptr=new int; x2_ptr=x_ptr; *x2_ptr=65; //memory leak
Dynamic allocation int *my_ptr=new int; *my_ptr=76; cout<<*my_ptr; delete my_ptr;
Dynamic allocation int *my_ptr=new int; *my_ptr=76; cout<<*my_ptr; delete my_ptr; int *my_ptr=new int(76); cout<<*my_ptr; delete my_ptr;
Dynamic allocation Another example…
Dynamic allocation double *p=new double[10];/*10 element array*/ int i, size=10; for (i=0; i<size; i++){ p[i]=2.0*i; } for (i=0; i<size; i++){ cout<<p[i]<<endl; } delete []p;