Top Banner
Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo
30

Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Dec 18, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Stacks and Queues & Inheritance

2014 Spring CS32 DiscussionJungseock Joo

Page 2: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Stacks and Queues

• Dynamic 1-dimensional data structures– Insertion & deletion of elements (push & pop)– Take place only in either side of list

Page 3: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Stacks and Queues

• Stack : Last-in-first-out• Queue : First-in-first-out

Page 4: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Stacks and Queues

• Q: Can we just use a linked-list with restrictions on insertions and deletions?

Page 5: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Stacks and Queues

• Q: Can we just use a linked-list with restrictions on insertions and deletions?

• A: That is a stack (or queue)

Page 6: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Stacks and Queues

• Usually – not always,– You start with an empty stack (or queue)– Insertion/deletion is incremental.– By the end of your program, they become empty

again because you have all the items processed.

Page 7: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Implementation

• By a linked list– void push( const ItemType& new_item );• Place the new_item at the “end” of current list• “end” – top in stacks, back in queues

– void pop();• Delete the top item (stack) or the front item (queue)• Destruct the item

– Then adjust the pointers of items and # of items accordingly

Page 8: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Implementation

• By a linked list– ItemType& top();• Only in stack, returns the top item

– ItemType& front();• Only in queue, returns the front item

Page 9: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Implementation

• Q: I need to access items in the middle• A: Then you don’t use stacks or queues

Page 10: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Stack vs. Queue

• When do you use what?– Depends on the particular order of items to

process.– E.g., Depth-first-search vs. Breadth-first-search

Page 11: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance

Page 12: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance

• To organize related classes (or objects) in a hierarchy.

Page 13: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance

• Why?– To reduce code redundancy– By sharing the same functions/variables among a

group of classes.• Sharing is directed, so inherited• From base-class to derived-class

– Polymorphism

Page 14: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance

• A “BaseballPlayer” is a “Person”• An “Employee” is a “Person”• A “Supervisor” is an “Employee”• A “Supervisor” is a “Person”

Page 15: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance

• A “BaseballPlayer” is a “Person”• An “Employee” is a “Person”• A “Supervisor” is an “Employee”• A “Supervisor” is a “Person”

Base-class

Derived-class

Page 16: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance

• A “BaseballPlayer” is a “Person”• An “Employee” is a “Person”• A “Supervisor” is an “Employee”• A “Supervisor” is a “Person”

Base-class

Derived-class

Page 17: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

InheritanceBase-class

Derived-class

• A base-class defines functions/variables to share with its derived classes– Person.age()– Person.gender()

Page 18: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance

• A derived-class defines its own specific functions/variables– BaseballPlayer.team()– Employee.company()– Supervisor.subordinates()

• It can also modify inherited functions, if needed.

Base-class

Derived-class

Page 19: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Example

Page 20: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Example

Page 21: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Automatic Conversion

Page 22: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Automatic Conversion

• So we can do..

Page 23: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Inheritance of Members

• All public members are inherited.– Int get_age();– Int m_age;– They can be used in derived classes.– Not constructor, destructor, assignment operator..

Page 24: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Function Overriding

• When you want to replace an existing function:

Page 25: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.
Page 26: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Virtual Function

• Overriding functions:– Multiple definitions with the same signature– We need to choose a specific one to run

Page 27: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Virtual Function

• Non-virtual functions:– According to the type

of pointer or reference

Page 28: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Virtual Function

• Virtual functions– According to “actual”

type

Page 29: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Virtual Function

Page 30: Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo.

Pure Virtual Function