1 / 30

Stacks and Queues & Inheritance

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. Stacks and Queues. Stack : Last-in-first-out

gilon
Télécharger la présentation

Stacks and Queues & Inheritance

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks and Queues & Inheritance 2014 Spring CS32 Discussion JungseockJoo

  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. Inheritance • 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 Base-class • A “BaseballPlayer” is a “Person” • An “Employee” is a “Person” • A “Supervisor” is an “Employee” • A “Supervisor” is a “Person” 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 • A base-class defines functions/variables to share with its derived classes • Person.age() • Person.gender() Derived-class

  18. Inheritance Base-class • A derived-class defines its own specific functions/variables • BaseballPlayer.team() • Employee.company() • Supervisor.subordinates() • It can also modify inherited functions, if needed. Derived-class

  19. Example

  20. Example

  21. Automatic Conversion

  22. Automatic Conversion • So we can do..

  23. Inheritance of Members • All public members are inherited. • Intget_age(); • Intm_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. Virtual Function • Overriding functions: • Multiple definitions with the same signature • We need to choose a specific one to run

  26. Virtual Function • Non-virtual functions: • According to the type of pointer or reference

  27. Virtual Function • Virtual functions • According to “actual” type

  28. Virtual Function

  29. Pure Virtual Function

More Related