Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Stacks and Queues & Inheritance 2014 Spring CS32 Discussion Jungseock Joo."— Presentation transcript:

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

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

3 Stacks and Queues Stack : Last-in-first-out Queue : First-in-first-out

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

5 Stacks and Queues Q: Can we just use a linked-list with restrictions on insertions and deletions? A: That is a stack (or queue)

6 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.

7 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

8 Implementation By a linked list – ItemType& top(); Only in stack, returns the top item – ItemType& front(); Only in queue, returns the front item

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

10 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

11 Inheritance

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

13 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

14 Inheritance A “BaseballPlayer” is a “Person” An “Employee” is a “Person” A “Supervisor” is an “Employee” A “Supervisor” is a “Person”

15 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

16 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

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

18 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

19 Example

20

21 Automatic Conversion

22 So we can do..

23 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..

24 Function Overriding When you want to replace an existing function:

25

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

27 Virtual Function Non-virtual functions: – According to the type of pointer or reference

28 Virtual Function Virtual functions – According to “actual” type

29 Virtual Function

30 Pure Virtual Function


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

Similar presentations


Ads by Google