CS32 Midterm 2 Review Session - CS 32 Midterm 2 Review...

Info icon This preview shows pages 1–17. Sign up to view the full content.

View Full Document Right Arrow Icon
Hosted by Upsilon Pi Epsilon February 24, 2016 CS 32 Midterm 2 Review Session
Image of page 1

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

View Full Document Right Arrow Icon
Midterm #2 Topics Stacks Queues Inheritance Polymorphism Recursion
Image of page 2
Motivation Examples in these slides will probably seem easy Meant as a warmup or refresher Real practice comes from working on practice problems o Link is at the end of these slides
Image of page 3

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

View Full Document Right Arrow Icon
Motivation Feel free to ask questions if you’d like us expand on a topic
Image of page 4
Stacks and Queues Two abstract data types (ADTs) used to store items Stacks follow LIFO (Last in, first out) Queues follow FIFO (First in, first out) The C++ STL has implementations of both of these # include < stack> # include < queue> stack< Type > stack_nam e; queue< Type > queue_nam e;
Image of page 5

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

View Full Document Right Arrow Icon
Stack Example stack< int > stk; stk.push(5); stk.push(33); stk.push(87); stk.pop(); stk.push(19); stk.pop(); stk.pop(); 5 33 19
Image of page 6
Things to Know about Stacks How would you implement your own stack class?
Image of page 7

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

View Full Document Right Arrow Icon
Things to Know about Stacks How would you implement your own stack class? O ne w ay: Use an internal array and an integer index to the “top”. There’s other w ays too.
Image of page 8
Queue Example queue< int > q; q.push(5); q.push(33); q.push(87); q.pop(); q.push(19); q.pop(); q.pop(); 5 33 87 19
Image of page 9

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

View Full Document Right Arrow Icon
Things to Know about Queues How would you implement your own queue?
Image of page 10
Things to Know about Queues How would you implement your own queue? One way: Use a linked list with both head and tail pointers. Is there a more efficient implementation(circular queue)?
Image of page 11

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

View Full Document Right Arrow Icon
Inheritance Define new classes based on old classes Based on an “ is-a ” relationship o Subclass “ is-a ” type of Superclass Example: Jaguar Cat Animal A Cat is-an Animal A Jaguar is-a Cat
Image of page 12
Importance of Inheritance Reuse code o Code in base class is inherited in derived classes Specialization o New code can be added to the derived class to distinguish it from its base parent Overriding o Certain inherited code (defined as virtual functions) can be overridden in the derived class
Image of page 13

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

View Full Document Right Arrow Icon
Reused Code class A nim al { public : A nim al(): fatigueLevel(3) {} void sleep() { fatigueLevel--; } private : int fatigueLevel; }; class Cat : public A nim al { public : Cat() { Cat c; c.sleep(); } We can call the sleep function for our Cat because it is derived from Animal and therefore inherits all the public code in Animal. C Animal Data fatigueLevel: 2
Image of page 14
class Cat : public A nim al { public : Cat(): thirsty( true ) {} void drinkM ilk() { thirsty = false ; } private : bool thirsty; }; C thirsty: false Specialization class A nim al { public : A nim al(): fatigueLevel(3) {} }; int m ain() { Cat c; c.drinkM ilk(); } Animal Data fatigueLevel: 3 We can add Cat-specific functionality to our Cat class to specialize it. The member variable thirsty and the function drinkMilk are examples of that.
Image of page 15

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

View Full Document Right Arrow Icon
class Cat : public A nim al { public : Cat(): thirsty( true ) {} void drinkM ilk() { thirsty = false ; } private : bool thirsty; }; Overriding class A nim al { public : A nim al(): fatigueLevel(3) {} void { fatigueLevel--; } private : int fatigueLevel; }; To allow a function in a base class to be overridden, you must insert the keyword virtual before it.
Image of page 16
Image of page 17
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