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

Info icon This preview shows pages 1–4. 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 square6 Copy Constructor IntMatrix::IntMatrix (const IntMatrix& right) { rows = right.rows; cols = right.cols; elements = new int[rows*cols]; for (int i=0; i< rows*cols; i++) elements[i] = right.elements[i]; } square6 Assignment Operator IntMatrix& IntMatrix::operator= (const IntMatrix& right) { if (this != &right) { 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?
Image of page 1

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

View Full Document Right Arrow Icon
2 Stuck in an Infinite Loop square6 Recursion is the process of a function calling itself. square6 A dumb example is: int main( ) { cout << “The force be with you!\n”; main(); } square6 This program creates an infinite loop, printing the message forever (or until the computer runs out of memory) . square6 Infinite loops are a common mistake in recursion. square6 A good recursive function should stop. Counting Down... square6 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) square6 We're stuck in an infinite loop again. Why does this function not work?
Image of page 2
3 Counting Down...
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern