Similar documents
C++ Support for Abstract Data Types


! stack, queue, priority queue, dictionary, sequence, set e.g., a Stack is a list implements a LIFO policy on additions/deletions.

CHAPTER 4 ESSENTIAL DATA STRUCTRURES


Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Glossary of Object Oriented Terms

The Preprocessor Variables, Functions, and Classes Less important for C++ than for C due to inline functions and const objects. The C++ preprocessor h

Cpt S 223. School of EECS, WSU

KITES TECHNOLOGY COURSE MODULE (C, C++, DS)

DATA STRUCTURES USING C

Variable Base Interface

Integrating the C++ Standard Template Library Into the Undergraduate Computer Science Curriculum

16 Collection Classes

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Abstract Data Type. EECS 281: Data Structures and Algorithms. The Foundation: Data Structures and Abstract Data Types

LINKED DATA STRUCTURES

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

Linked Lists, Stacks, Queues, Deques. It s time for a chainge!

BCS2B02: OOP Concepts and Data Structures Using C++

Structural Design Patterns Used in Data Structures Implementation

Pointers and Linked Lists

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

1 Abstract Data Types Information Hiding

Sorting revisited. Build the binary search tree: O(n^2) Traverse the binary tree: O(n) Total: O(n^2) + O(n) = O(n^2)

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Krishna Institute of Engineering & Technology, Ghaziabad Department of Computer Application MCA-213 : DATA STRUCTURES USING C

El Dorado Union High School District Educational Services

How To Port A Program To Dynamic C (C) (C-Based) (Program) (For A Non Portable Program) (Un Portable) (Permanent) (Non Portable) C-Based (Programs) (Powerpoint)

Binary Heap Algorithms

C++ Programming Language

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Object Oriented Software Design II

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Implementation Aspects of OO-Languages

Stack Allocation. Run-Time Data Structures. Static Structures

Data Structures. Level 6 C Module Descriptor

Habanero Extreme Scale Software Research Project

The C Programming Language course syllabus associate level

3 Representation in C++.

Data Structure with C

Embedded Programming in C/C++: Lesson-1: Programming Elements and Programming in C

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

PES Institute of Technology-BSC QUESTION BANK

10CS35: Data Structures Using C

Algorithms and Data Structures

Object Oriented Software Design II

Cours de C++ Utilisations des conteneurs

ADTs,, Arrays, Linked Lists

Binary storage of graphs and related data

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Data Structures Using C++ 2E. Chapter 5 Linked Lists

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Coding conventions and C++-style

C++ INTERVIEW QUESTIONS

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Cpt S 223. School of EECS, WSU

Analysis of Algorithms I: Binary Search Trees

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Stacks. Stacks (and Queues) Stacks. q Stack: what is it? q ADT. q Applications. q Implementation(s) CSCU9A3 1

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

Sample Questions Csci 1112 A. Bellaachia

5. A full binary tree with n leaves contains [A] n nodes. [B] log n 2 nodes. [C] 2n 1 nodes. [D] n 2 nodes.

DATA STRUCTURE - STACK

6.088 Intro to C/C++ Day 4: Object-oriented programming in C++ Eunsuk Kang and Jean Yang

CSI33 Data Structures

Object-Oriented Programming

Analysis of a Search Algorithm

Queue Implementations

GUJARAT TECHNOLOGICAL UNIVERSITY, AHMEDABAD, GUJARAT. Course Curriculum. DATA STRUCTURES (Code: )

Linked Lists Linked Lists, Queues, and Stacks

AP Computer Science AB Syllabus 1

Chapter 1: Key Concepts of Programming and Software Engineering

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

1/20/2016 INTRODUCTION

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Iterators. Provides a way to access elements of an aggregate object sequentially without exposing its underlying representation.

Why you shouldn't use set (and what you should use instead) Matt Austern

Data Structures and Algorithm Analysis (CSC317) Intro/Review of Data Structures Focus on dynamic sets

: provid.ir

Stacks. Linear data structures

Syllabus for Computer Science. Proposed scheme for B.Sc Programme under Choice Based Credit System

language 1 (source) compiler language 2 (target) Figure 1: Compiling a program

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Abstract Data Types. Chapter 2

C++ Language Tutorial

Logging. Working with the POCO logging framework.

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

Lecture Notes on Binary Search Trees

Copyright 2007 Ramez Elmasri and Shamkant B. Navathe. Slide 13-1

Transcription:

Object-Oriented Desin and Prorammin C++ Container Classes Outline Introduction Container Class Objectives Class Library Architecture Parameterized Types Preprocessor Macros enclass void Pointer Method void Pointer Example 1 Introduction Container classes are an important cateory of ADTs { They are used to maintain collections of elements like stacks, queues, linked lists, tables, trees, etc. Container classes form the basis for various C++ class libraries { Note, makin class libraries is a popular way to learn C++: :: C++ container classes can be implemented usin several methods: (0) Ad hoc, rewrite from scratch each time (1) Preprocessor Macros (2) A enclass Facility (e.., GNU lib++) (3) Parameterized Types (4) void Pointer Method Note, methods 1,3 apply to homoeneous collections; method 4 allows heteroeneous collections 2 Container Class Objectives Application Independence Container Class Objectives (cont'd) { Transparently reuse container class code for various applications Type Safety { Insure that the collections remain type safe Ease of Modication This is easy for parameterized types, harder for void pointers: :: { Relatively easy to extend classes to t smoothly into a new application Run-Time Eciency and Space Utilization Ease of Manipulation { Implementation must hide representation details, e.., iterators { Dierent schemes have dierent tradeos e.., extra indirection vs exibility 3 4

Object-Oriented Class Library Architecture Two eneral approaches are tree vs forest (dier in their use of inheritance): Tree: create a sinle rooted tree of classes derived from a common base class, e.., object { e.., standard Smalltalk libraries or NIHCL Object-Oriented Class Library Architecture (cont'd) Object Forest: a collection of enerally independent classes available for individual selection and use Numeric Container Sorted Unsorted Shape { e.., GNU lib++ library, Borland C++ class library, Booch components, Roue Wave, USL Standard components Vector Stack Circle Square Binary Srch Tree Ba Rectanle Tradeos: 1. Uniformity (Tree) vs exibility (Forest) Tree-based class library 2. Sharin (Tree) vs eciency (Forest) { Forest classes do not inherit unnecessary functions 5 6 Parameterized Types Object-Oriented Class Library Architecture (cont'd) Vector Stack Hash Table Binary Srch Tree Forest-based class library Ba Splay Tree Queue Parameterized list class template <class T> class List f List (void): head (0) f void prepend (T &item) f Node<T> *temp = new Node<T> (item, this->head ); this->head = temp; /* :::*/ template <class T> class Node f T value ; Node<T> *next ; Node (T &v, Node<T> *n) : value (v), next (n) f ; Node<T> *head ; ; 7 List<int> list; list.prepend (20); // ::: 8

Preprocessor Macros Parameterized Types (cont'd) Parameterized Vector class template <class T = int, int SIZE = 100> class Vector f Vector (void): size (SIZE) f T&operator[] (size ti)f return this->buf [i]; T buf [SIZE]; size t size ; ; Vector<double> d; // 100 doubles Vector<int, 1000> d; // 1000 ints d[10] =3.1416; 9 Stack example (usin GNU ++) #ifndef stack h #dene stack h #dene name2(a,b) EnErIc2(a,b) #dene EnErIc2(a,b) a ## b #dene Stack(TYPE) name2(type,stack) #dene StackDeclare(TYPE) \ class Stack(TYPE) f \ \ Stack(TYPE) (size t size): size (size) f \ this->bottom = new TYPE[size]; \ this->top = this->bottom + size; \ \ TYPE pop (void) f \ return *this->top ++; \ \ void push (TYPE item) f \ *--this->top = item; \ \ bool is empty (void) f \ return this->top == this->bottom \ + this->size ; \ \ bool is full (void) f \ return this->bottom == this->top ; \ \ ~Stack(TYPE) (void) f delete this->bottom ; \ \ TYPE *bottom ; \ TYPE *top ; \ size t size ; \ #endif 10 Preprocessor Macros (cont'd) Stack driver #include <stream.h> #include "stack.h" StackDeclare (char); typedef Stack(char) CHARSTACK; const int STACK SIZE = 100; CHARSTACK s (STACK SIZE); char c; cout << "please enter your name..: "; while (!s.is full () && cin.et (c) && c!= '\n') s.push (c); cout << "Your name backwards is..: "; while (!s.is empty ()) cout << s.pop (); cout << "\n"; Main problems: (1) Uly ;-) (2) Code bloat (3) Not interated with compiler 11 enclass Technique used by GNU lib++ { Uses sed to perform text substitution sed -e "s/<t>/$t1/" -e "s/<t&>/$t1$t1acc/" Sinle Linked List class class <T>SLList f <T>SLList (void); <T>SLList (<T>SLList &a); ~<T>SLList (void); <T>SLList &operator = (<T>SLList &a); int empty (void); int lenth (void); void clear (void); Pix prepend (<T&> item); Pix append (<T&> item); /* :::*/ <T>SLListNode* last ; ; 12

void Pointer Method General approach: { void * pointers are the actual container elements { Subclasses are constructed by coercin void * elements into pointers to elements of interest Advantaes: 1. Code sharin, less code redundancy 2. Builds on existin C++ features (e.., inheritance) Disadvantaes: 1. Somewhat awkward to desin correctly 2. Inecient in terms of time and space (requires dynamic allocation) 3. Reclamation of released container storae is dicult (need some form of arbae collection) 13 void Pointer Example One example application is a eneric ADT List container class. It contains four basic operations: 1. Insertion { add item to either front or back 2. Membership { determine if an item is in the list 3. Removal { remove an item from the list 4. Iteration { allow examination of each item in the list (without revealin implementation details) The eneric list stores pointers to elements, alon with pointers to links { This allows it to hold arbitrary objects (but watch out for type-safety!!) 14 void Pointer Example (cont'd) Generic List.h #ifndef Generic List #dene Generic List class List f List (void); ~List (void); void remove current (void); // Used as iterators: :: void reset (void); void next (void); class Node f friend List; Node (void *, Node *n = 0); ~Node (void); void add to front (void *); void add to end (void *); Node *remove (void *); void *element ;//Pointer to actual data Node *next ; ; 15 Generic List.h (cont'd) // used by subclasses for implementation void add to end (void *); void add to front (void *); Node *current value (void); void *current (void); bool includes (void *); void *remove (void *); // important to make match virtual! virtual bool match (void *, void *); Node *head ; Node *iter ; // used to iterate over lists ; 16

Generic List.C Generic List.h (cont'd) // Iterator functions inline List::Node *List::current value (void) f return this->iter ; inline void List::reset (void) f this->iter = this->head ; inline void *List::current (void) f if (this->iter ) return this->iter ->element ; return 0; inline void List::next (void) f this->iter = this->iter->next ; 17 // Node methods inline List::Node::Node (void *v, List::Node *n) : element (v), next (n) f inline List::Node::~Node (void) f if (this->next ) // recursively delete the list! delete this->next ; inline void List::Node::add to front (void *v) f this->next = new List::Node (v, this->next ); void List::Node::add to end (void *v) f if (this->next ) // recursive! this->next ->add to end (v); this->next = new List::Node (v); List::Node *List::Node::remove (void *v)f if (this == v) return this->next ; if (this->next ) // recursive this->next = this->next ->remove (v); return this; 18 Generic List.C (cont'd) Generic List.C // List methods void List::add to front (void *v) f this->head = new List::Node (v, this->head ); void List::add to end (void *v) f if (this->head ) // recursive! this->head ->add to end (v); this->head = new List::Node (v); bool List::includes (void *v) f // Iterate throuh list for (this->reset (); this->current (); this->next ()) // virtual method dispatch! if (this->match (this->current (), v)) return true; return false; bool List::match (void *x, void *y) f return x == y; 19 void List::remove current (void) f if (this->head == this->iter ) this->head = this->iter ->next ; this->head = this->head ->remove (this->iter ); this->iter ->next = 0; delete this->iter ; // Deallocate memory this->iter = 0; void *List::remove (void *v) f for (this->reset (); this->current (); this->next ()) if (this->match (this->current (), v)) f void *fv = this->current (); this->remove current(); return fv; return 0; inline List::List (void): head (0), iter (0) f List::~List (void) f if (this->head ) delete this->head ; // recursive! 20

void Pointer Example (cont'd) Card.h #include "Generic List.h" class Card f friend class Card List; enum Suit f SPADE = 1, HEART = 2, CLUB = 3, DIAMOND = 4 ; enum Color f BLACK = 0, RED = 1 ; Card (int r, int s); int rank (void); Suit suit (void); Color color (void); bool operator == (Card &y); void print (ostream &); int rank ; Suit suit ; ; Card.h inline int Card::rank (int) f return this->rank ; inline Card::Suit Card::suit (void) f return this->suit ; inline bool Card::operator == (Card &y) f return this->rank () == y.rank () && this->suit () == y.suit(); inline void Card::print (ostream &str) f str << "suit " << this->suit () << "rank " << this->rank () << endl; inline Card::Card (int r, Card::Suit s) : rank (r), suit (s) f inline Card::Color Card::color (void) f return Card::Color (int (this->suit ()) % 2); 21 22 Card List.h #include "Card.h" class Card List : public List f void add (Card *a card) f List::add to end (a card); Card *current (void) f return (Card *) List::current (); int includes (Card *a card) f return List::includes (a card); void remove (Card *a card) f List::remove (a card); Card List.C // Virtual method bool Card List::match (void *x, void *y) f Card &xr = *(Card *) x; Card &yr = *(Card *) y; // Calls Card::operator == return xp == yp; void Card List::print (ostream &str) f for (this->reset (); this->current (); this->next ()) this->current ()->print (str); void print (ostream &); // Actual match function used by List! virtual bool match (void *, void *); ; 23 24

main.c #include "Card.h" Card List cl; Card *a = new Card (Card::HEART, 2); Card *b = new Card (Card::DIAMOND, 4); Card *c = new Card (Card::CLUB, 3); cl.add (a); cl.add (b); cl.add (c); cl.add (b); cl.print (cout); if (cl.includes (new Card (Card::DIAMOND, 4))) cout << "list includes 4 of diamonds\n"; cout << "somethin's wron!\n"; cl.remove (new Card (Card::CLUB, 3)); cl.print (cout); return 0; Main problem: { Must dynamically allocate objects to store into eneric list! Handlin memory deallocation is dicult without arbae collection or other tricks: :: 25