short c++ review cs 302 – data structures. call by value/reference

35
Short C++ Review CS 302 – Data Structures

Upload: beverly-charles

Post on 17-Dec-2015

219 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Short C++ Review CS 302 – Data Structures. Call by value/reference

Short C++ Review

CS 302 – Data Structures

Page 2: Short C++ Review CS 302 – Data Structures. Call by value/reference

Call by value/reference

Page 3: Short C++ Review CS 302 – Data Structures. Call by value/reference

• The argument names in the function header are referred to as formal parameters.

int FindMax(int x, int y){ int maximum;  if(x>=y) maximum = x; else maximum = y;  return maximum;}

Formal Parameters

x, y are called“formal parameters”

Page 4: Short C++ Review CS 302 – Data Structures. Call by value/reference

Actual Parameters• The argument names in the function call are referred

to as actual parameters

 int FindMax(int, int); // function prototype int main(){ int firstnum, secnum, max;  cout << "\nEnter two numbers: "; cin >> firstnum >> secnum;  max=FindMax( firstnum, secnum); // the function is called here  cout << "The maximum is " << max << endl;  return 0;}

firstnum, secnum are called“actual parameters”

Page 5: Short C++ Review CS 302 – Data Structures. Call by value/reference

 

void newval(float, float); // function prototype 

int main(){ float firstnum, secnum;  cout << "Enter two numbers: "; cin >> firstnum >> secnum;  newval(firstnum, secnum);  cout << firstnum << secnum << endl;  return 0;} void newval(float xnum, float ynum){ xnum = 89.5; ynum = 99.5;}

Calling a function by value

Formal parametersreceive a “copy” of the

actual parameters!

Page 6: Short C++ Review CS 302 – Data Structures. Call by value/reference

 

void newval(float&, float&); // function prototype 

int main(){ float firstnum, secnum;  cout << "Enter two numbers: "; cin >> firstnum >> secnum;  newval(firstnum, secnum);  cout << firstnum << secnum << endl;  return 0;} void newval(float& xnum, float& ynum){ xnum = 89.5; ynum = 99.5;}

Calling a function by reference

Formal parametersbecome an “alias” of the

actual parameters!

Page 7: Short C++ Review CS 302 – Data Structures. Call by value/reference

Static vs Dynamic Array Allocation

Page 8: Short C++ Review CS 302 – Data Structures. Call by value/reference

1D Static Array Allocationshort arr[6];

&arr[i] := &arr[0] + (i * sizeof(short))

offset

Page 9: Short C++ Review CS 302 – Data Structures. Call by value/reference

 float find_average(int [], int); void main(){ const numElems = 5; int arr[numElems] = {2, 18, 1, 27, 16};  cout << "The average is " << find_average(arr, numElems) <<

endl;} 

Static 1D arrays as function arguments

Page 10: Short C++ Review CS 302 – Data Structures. Call by value/reference

 

float find_average(int vals[], int n){ int i; float avg;  avg=0.0; for(i=0; i<n; i++) avg += vals[i];  avg = avg/n;  return avg;}

Static 1D arrays as function arguments

Page 11: Short C++ Review CS 302 – Data Structures. Call by value/reference

2D Static Array Allocation•Static 2D arrays are always stored in memory as 1D arrays in contiguous memory.

short arr2D[3][4];

Page 12: Short C++ Review CS 302 – Data Structures. Call by value/reference

 float find_average(int [][2], int, int);  void main(){ const numRows = 2; const numCols = 2; int arr2D[numRows][numCols] = {2, 18, 1, 27}; float average;  average = find_average(arr2D, numRows, numCols);  cout << "The average is " << average << endl;}

Static 2D arrays as function arguments

Page 13: Short C++ Review CS 302 – Data Structures. Call by value/reference

float find_average(int vals[][2], int n, int m)

{

int i,j;

float avg;

 

avg=0.0;

for(i=0; i<n; i++)

for(j=0; j<m; j++)

avg += vals[i][j];

 

avg = avg/(n*m);

 

return avg;

}

Static 2D arrays as function arguments

Page 14: Short C++ Review CS 302 – Data Structures. Call by value/reference

Dynamic 1D Array Allocation/ Deallocation

int *arr;

arr = newnew int[N];

deletedelete [] arr;

Page 15: Short C++ Review CS 302 – Data Structures. Call by value/reference

Dynamic 1D arrays as function arguments

float find_average(int *, int);

void main(){ int *arr, numElems;float average;

cin << numElems;

arr = new int[numElems]; // initialize array

 average = find_average(arr, numElems);  cout << "The average is " << average << endl;

}

Page 16: Short C++ Review CS 302 – Data Structures. Call by value/reference

float find_average(int *vals, int n){ int i; float avg;  avg=0.0; for(i=0; i<n; i++) avg += vals[i];  avg = avg/n;  return avg;}

Dynamic 1D arrays as function arguments

Page 17: Short C++ Review CS 302 – Data Structures. Call by value/reference

Dynamic 2D Array Allocationarr[2][6]

480 482 484 486 488 490

Page 18: Short C++ Review CS 302 – Data Structures. Call by value/reference

Dynamic 2D Array Allocation/Deallocation

 

int **arr2D;

arr2D = new int* [numRows]; // allocation for(i=0; i<numRows; i++) arr2D[i] = new int[numCols]; 

for(i=0; i<numRows; i++) // deallocation  delete [] arr2D[i];delete [] arr2D; 

Page 19: Short C++ Review CS 302 – Data Structures. Call by value/reference

 float find_average(int **, int, int);  void main(){ int **arr2D; float average;  arr2D = new int*[numRows]; for(i=0; i<numRows; i++) arr2D[i] = new int[numCols]; // initialize array

average = find_average(arr2D, numRows, numCols);  cout << "The average is " << average << endl;}

Dynamic 2D arrays as function arguments

Page 20: Short C++ Review CS 302 – Data Structures. Call by value/reference

  

float find_average(int **vals, int n, int m){ int i, j; float avg;  avg=0.0; for(i=0; i<n; i++) for(j=0; j<m; j++) avg += vals[i][j];  avg = avg/(n*m);  return avg;}

2D Dynamic arrays as function arguments

Page 21: Short C++ Review CS 302 – Data Structures. Call by value/reference

Object Composition

Page 22: Short C++ Review CS 302 – Data Structures. Call by value/reference

class rectangle { private: float height; float width; int xpos; int ypos; public: rectangle(float, float); // constructor void draw(); void posn(int, int); void move(int, int); }; rectangle::rectangle(float h, float w){ height = h; width = w; xpos = 0; ypos = 0;}

Page 23: Short C++ Review CS 302 – Data Structures. Call by value/reference

class rectangle { private: float height; float width; int xpos; int ypos; properties pr; public: rectangle(float, float, int, int ); // constructor void draw(); void posn(int, int); void move(int, int); };

Object Composition: objects as members of classes

A class may have objects of other classes as members.

Page 24: Short C++ Review CS 302 – Data Structures. Call by value/reference

Object Composition (cont.)

 class properties { private: int color; int line; public: properties(int, int); // constructor}; properties::properties(int c, int l){ color = c; line = l;}

Page 25: Short C++ Review CS 302 – Data Structures. Call by value/reference

Object Composition (cont.)

rectangle::rectangle(float h, float w, int c, int l):pr(c, l){ height = h; width = w; xpos = 0; ypos = 0;};  void main(){ rectangle rc(3.0, 2.0, 1, 3);  ………}

Page 26: Short C++ Review CS 302 – Data Structures. Call by value/reference

Deep vs shallow copy

Page 27: Short C++ Review CS 302 – Data Structures. Call by value/reference

#include <iostream.h>#include <string.h> class string { private: char *s; int size; public: string(char *); ~string(); void print(); void copy(char *);}; void string::print(){ cout << s << endl;}

Note: no user definedcopy constructor provided!

void string::copy(char *c){ strcpy(s, c);}

Page 28: Short C++ Review CS 302 – Data Structures. Call by value/reference

Constructor/Destructor

string::string(char *c) // constructor

{ size = strlen(c); s = new char[size+1]; strcpy(s,c);} string::~string() // destructor{ delete []s;}

Page 29: Short C++ Review CS 302 – Data Structures. Call by value/reference

 

void main()

{

string str1("George");

string str2(str1); // shallow copy!

 

str1.print(); // what is the output ?

str2.print();

 

str2.copy("Mary");

 

str1.print(); // what is the output now ?

str2.print();

}

Page 30: Short C++ Review CS 302 – Data Structures. Call by value/reference

string::string(const string& old_str){ size = old_str.size; s = new char[size+1]; strcpy(s,old_str.s);}  

Copy-Constructor

Page 31: Short C++ Review CS 302 – Data Structures. Call by value/reference

Overloading Assignment

Page 32: Short C++ Review CS 302 – Data Structures. Call by value/reference

Overloading Assignment

• Default overloading is not enough for classes with pointer members (i.e., shallow copy)

 

 

class string { private: char *s; int size; public: string(char *); ~string(); void operator=(string&); void print(); void copy(char *);};

Page 33: Short C++ Review CS 302 – Data Structures. Call by value/reference

void string::operator=(string& old_str){ delete [] s; //release previously assigned memory size = old_str.size; s = new char[size+1]; //assign new memory strcpy(s, old_str.s);} 

Overloading Assignment

Page 34: Short C++ Review CS 302 – Data Structures. Call by value/reference

Handling multiple assignments(e.g., x=y=z;)

string& string::operator=(const string& old_str) {

if (this != &old_str) { delete [] s; size = old_str.size; s = new char[size+1]; strcpy(s, old_str.s);}return *this; // return ref for multiple assignment}

Page 35: Short C++ Review CS 302 – Data Structures. Call by value/reference

Difference between copy constructor and assignment

• The copy constructor creates a new object.

• The assignment operator works on an already valid object.