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

Slides:



Advertisements
Similar presentations
Recursion 2014 Spring CS32 Discussion Jungseock Joo.
Advertisements

Stacks, Queues, and Linked Lists
Constructors: Access Considerations DerivedClass::DerivedClass( int iR, float fVar) : BaseClass(fVar) { m_uiRating = uiR; } Alternatively DerivedClass::DerivedClass(
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Review of Stacks and Queues Dr. Yingwu Zhu. Our Focus Only link-list based implementation of Stack class Won’t talk about different implementations of.
CPA: C++ Polymorphism Copyright © 2007 Mohamed Iqbal Pallipurath Overview of C++ Polymorphism Two main kinds of types in C++: native and user-defined –User-defined.
Queue RIZWAN REHMAN CENTRE FOR COMPUTER STUDIES DIBRUGARH UNIVERSITY.
Inheritance CS 308 – Data Structures “the mechanism by which one class acquires the properties of another class” the properties of another class”
Inheritance, Polymorphism, and Virtual Functions
PolymorphismCS-2303, C-Term Polymorphism Hugh C. Lauer Adjunct Professor (Slides include materials from The C Programming Language, 2 nd edition,
Virtual Functions Junaed Sattar November 10, 2008 Lecture 10.
LECTURE 8. Polymorphism One interface, multiple methods C++ supports both compile time and runtime polymorphism.
OOP Languages: Java vs C++
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Virtual Functions and Polymorphism Chapter What You Will Learn What is polymorphism? How to declare and use virtual functions for abstract classes.
CSE 425: Object-Oriented Programming II Implementation of OO Languages Efficient use of instructions and program storage –E.g., a C++ object is stored.
1 Classes- Inheritance Multiple Inheritance It is possible to derive a new class from more than one base class. This is called Multiple Inheritance. Under.
Chapter 15 – Inheritance, Virtual Functions, and Polymorphism
Polymorphism. Introduction ‘one name multiple forms’ Implemented using overloaded functions and operators Early binding or static binding or static linking.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Object-Oriented Programming: Polymorphism 1. OBJECTIVES What polymorphism is, how it makes programming more convenient, and how it makes systems more.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Custom Templatized Data Structures.
CMSC 202 Lesson 19 Polymorphism 2. Warmup What is wrong with the following code? What error will it produce? (Hint: it already compiles) for (unsigned.
Programming Languages and Paradigms Object-Oriented Programming (Part II)
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
1 Chapter 7 Stacks and Queues. 2 Stack ADT Recall that ADT is abstract data type, a set of data and a set of operations that act upon the data. In a stack,
1 COMP313A Programming Languages Object Oriented Progamming Languages (3)
Inheritance CS 302 – Data Structures Section 2.4 (pp ) and Section 6.7.
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Stacks.
What Is Inheritance? Provides a way to create a new class from an existing class New class can replace or extend functionality of existing class Can be.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Review of Stacks and Queues Dr. Yingwu Zhu. How does a Stack Work? Last-in-First-out (LIFO) data structure Adding an item Push operation Removing an item.
CS212: Object Oriented Analysis and Design Lecture 17: Virtual Functions.
1 COMP313A Programming Languages Object Oriented Progamming Languages (2)
Reusing Code in C++ Has-a relationship Classes with member objects(containment) The valarray template class Private & protected inheritance Multiple inheritance.
OOP using C Abstract data types How to accomplish the task??? Requirements Details Input, output, process Specify each task in terms of input.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
Overview of C++ Polymorphism
Computer Engineering Rabie A. Ramadan Lecture 6.
STACK Data Structure
Recap Introduction to Inheritance Inheritance in C++ IS-A Relationship Polymorphism in Inheritance Classes in Inheritance Visibility Rules Constructor.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
CS-2851 Dr. Mark L. Hornick 1 Stacks and Queues Behavior vs. Structure.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Abstract classes only used as base class from which other classes can be inherit cannot be used to instantiate any objects are incomplete Classes that.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
CS 215 Final Review Ismail abumuhfouz Fall 2014.
Inheritance and Big O And your first reading assignment
CISC181 Introduction to Computer Science Dr
Class A { public : Int x; A()
C++ Plus Data Structures
Inheritance, Polymorphism, and Virtual Functions
Inheritance & Polymorphism
Polymorphism & Virtual Functions
Pointers and Linked Lists
Polymorphism Lec
Inheritance, Polymorphism, and Virtual Functions
Indirection.
Polymorphism 2 CMSC 202.
Object-Oriented Programming (Part 2)
Dynamic allocation (continued)
CMSC 202 Lesson 19 Polymorphism 2.
Final Exam Review Inheritance Template Functions and Classes
Inheritance in C++ Inheritance Protected Section
Computer Science II for Majors
Presentation transcript:

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 Queue : First-in-first-out

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

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

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.

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

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

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

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

Inheritance

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

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

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

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

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

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

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

Example

Automatic Conversion

So we can do..

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

Function Overriding When you want to replace an existing function:

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

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

Virtual Function Virtual functions – According to “actual” type

Virtual Function

Pure Virtual Function