Recursion - Lecture 8: Recursion PIC 10B Todd Wittman...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Lecture 8: Recursion PIC 10B Todd Wittman Operator= vs. Copy Constructor s Copy Constructor rows = right.rows; cols = right.cols; elements = new int[rows*cols]; for (int i=0; i< rows*cols; i++) elements[i] = right.elements[i]; } s Assignment Operator IntMatrix& IntMatrix::operator= rows = right.rows; cols = right.cols; delete[] elements; elements= new int[rows*cols]; for (int i=0; i<rows*cols; i++) elements[i] = right.elements[i]; } return *this; } •The operator= copies the data in one object into another pre-existing object. •The copy constructor also makes a copy, but it creates a new object from scratch. •What are the differences in code? •When is each called?
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stuck in an Infinite Loop s Recursion is the process of a function calling itself. s A dumb example is: int main( ) { cout << “The force be with you!\n”; main(); } s This program creates an infinite loop, printing the message forever (or until the computer runs out of memory) . s Infinite loops are a common mistake in recursion. s A good recursive function should stop. Counting Down. .. s Print out the numbers from N down to 1. void countDown (int N) { cout << N << “\n”; countDown(N-1); return; } Ex N=3 countDown(3) countDown(2) countDown(1) countDown(0 ) ... cout<<3 cout<<2 cout<<1 cout<<0 countDown(2) countDown(1) countDown(0) countDown(-1) s We're stuck in an infinite loop again. Why does this
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

Recursion - Lecture 8: Recursion PIC 10B Todd Wittman...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online