Really ann ann aliasing is when we use two different

This preview shows page 16 - 23 out of 82 pages.

// really ann = ann; !!! Aliasing ” is when we use two different references/pointers to refer to the same variable. It can cause unintended problems!
Image of page 16

Subscribe to view the full document.

17 The Assignment Operator The fix: Our assignment operator function must check to see if a variable is being assigned to itself , and if so, do nothing class PiNerd { ... PiNerd &operator=(const PiNerd &src) { delete [] m_pi; m_n = src.m_n; m_pi = new int[m_n]; for (int j=0;j<m_n;j++) m_pi[j] = src.m_pi[j]; return *this; } }; And we’re done! if (&src == this) return *this; // do nothing If the right-hand variable’s address Is the same as the left-hand variable’s address Then they’re the same variable ! We simply return a reference to the variable and do nothing else!
Image of page 17
18 Copy Constructor/ Assignment Review Question: which of the following use the copy constructor and which use the assignment operator ? int main() // #1 { PiNerd a(4), b(3); b = a; } int main() // #2 { PiNerd c(5), d(c); PiNerd e = d; } // #3 PiNerd func(void) { PiNerd g(15); return(g); } int main() { PiNerd f = func(); }
Image of page 18

Subscribe to view the full document.

19 Time for your favorite game! Programming Language Inventor Or Serial Killer See if you can guess who uses a keyboard and who uses a chainsaw!
Image of page 19
Linked Lists 20
Image of page 20

Subscribe to view the full document.

Linked Lists… Why should you care? Linked Lists are used in everything from video games to search engines . Any time you don’t know how many items you’ll need to store ahead of time, you use ‘em. So pay attention! And virtually every job interview will grill you on them.
Image of page 21
Arrays are great… But… 22 Arrays are great when you need to store a fixed number of items int main() { int array[ 100 ]; } But what if you don’t know how many items you’ll have ahead of time? int main() { // might have 10 items or 1M int array[ 1000000 ]; } Then you have to reserve enough slots for the largest possible case. And what if you need to insert a new item in the middle of an array ? Andrew Betty David Elaine Frank Zappa names[0] names[1] names[2] names[3] names[4] names[999998] names[999999] Carey We have to move every item below the insertion spot down by one ! And it’s just as slow if we want to delete an item ! Yuck! int main() { int numItems, *ptr; cin >> numItems ; ptr = new int[ numItems ]; } Even new / delete don’t really help! It takes nearly 1M steps to add a new item!
Image of page 22

Subscribe to view the full document.

So Arrays Aren’t Always Great 23 Hmm… Can we think of an approach from “real life” that works better than a fixed-sized array? What can we think of that: allows you to store an arbitrary number of items makes it fast to insert a new item in the middle makes it fast to delete an item from the middle How about organizing the items as we would in a Scavenger Hunt ? ? Clue: The first item is by the tree Clue: The next item is by the house Clue: The next item is by the tower Clue: This is the last item ! Using this approach we can store an arbitrary number of items ! There’s no fixed limit to the number of chests and clues we can have!
Image of page 23
You've reached the end of this preview.
  • Winter '16

{[ 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