The Composite State Visitor Pattern

Size: px
Start display at page:

Download "The Composite State Visitor Pattern"

Transcription

1 Design Patterns for Data Structures Chapter 8 he Composite State Visitor Pattern

2 Design Patterns for Data Structures Chapter 8 he Composite State Binary ree with the Visitor Pattern BireeCSV

3 Design Patterns for Data Structures Chapter 8 he host system provides he solution a minimal complete set of operations that any plugin developer can use to build the desired plugin an abstract visitor class with methods for the plugin developer to implement a public method named accept() for the end user to execute

4 Design Patterns for Data Structures Chapter 8 he plugin developer writes a plugin by declaring a visitor to be a subclass of the abstract visitor class implements the visitor methods that the abstract visitor class provides

5 Design Patterns for Data Structures Chapter 8 he plugin user instantiates a specific visitor object calls accept(), passing the visitor object as a parameter he host executes accept(), which, in turn, executes the methods implemented by the plugin developer.

6 BireeCSV _root: AcsvNode<> * AcsvNode Host system BireeCSV(rhs: BireeCSV<> const &) BireeCSV(node: AcsvNode<> *) + BireeCSV( ) + ~BireeCSV( ) + accept(visitor: ABireeCSVVis<> &) + accept(visitor: ABireeCSVVis<> &) const copyroot(rhs: BireeCSV<> const &): AcsvNode<> * + insertroot(data: const &) + left( ): BireeCSV<> & + left( ) const: BireeCSV<> const & + operator=(rhs: BireeCSV const &): BireeCSV & + remroot( ): + right( ): BireeCSV<> & + right( ) const: BireeCSV<> const & + root( ): & + root( ) const: const & + setree(tree: BireeCSV<> &) + ~AcsvNode() # accept(owner: BireeCSV<> &, visitor: ABireeCSVVis<> &) # accept(owner: BireeCSV<> const &, visitor: ABireeCSVVis<> &) copyroot(): AcsvNode * # insertroot(owner: BireeCSV<> &, data: const &) # left( ): BireeCSV<> & # left( ) const: BireeCSV<> const & # remroot(owner: BireeCSV<> &): # right( ): BireeCSV<> & # right( ) const: BireeCSV<> const & # root( ): & # root( ) const: const & # setree(owner: BireeCSV<> &, tree: BireeCSV<> &) NEcsNode McsNode _left: BireeCS<> _data: _right: BireeCS<> McsNode( ) NEcsNode(data: const &) NEcsNode(leftNode: AcsNode<> *, data:, rightnode: AcsNode<> *) NEcsNode(rhs: const NEcsNode<> *) <<use>> ABireeCSVVis + ~ABireeCSVVis() Plugin + emptycase(host: BireeCSV<> &) + nonemptycase(host: BireeCSV<> &) + emptycase(host: BireeCSV<> const &) + nonemptycase(host: BireeCSV<> const &) BiCSVisEmptyVis BiCSVclearVis BiCSVmaxVis _result: bool + BiCSVclearVis( ) _result: const * + BiCSVisEmptyVis( ) + result( ) const: bool + BiCSVmaxVis( ) + result( ) const: const &

7 Design Patterns for Data Structures Figure 8.41 BireeCSV _root: AcsvNode<> * AcsvNode BireeCSV(rhs: BireeCSV<> const &) BireeCSV(node: AcsvNode<> *) + BireeCSV( ) + ~BireeCSV( ) + accept(visitor: ABireeCSVVis<> &) + accept(visitor: ABireeCSVVis<> &) const copyroot(rhs: BireeCSV<> const &): AcsvNode<> * + insertroot(data: const &) + left( ): BireeCSV<> & + left( ) const: BireeCSV<> const & + operator=(rhs: BireeCSV const &): BireeCSV & + remroot( ): + right( ): BireeCSV<> & + right( ) const: BireeCSV<> const & + root( ): & + root( ) const: const & + setree(tree: BireeCSV<> &) + ~AcsvNode() # accept(owner: BireeCSV<> &, visitor: ABireeCSVVis<> &) # accept(owner: BireeCSV<> const &, visitor: ABireeCSVVis<> &) copyroot(): AcsvNode * # insertroot(owner: BireeCSV<> &, data: const &) # left( ): BireeCSV<> & # left( ) const: BireeCSV<> const & # remroot(owner: BireeCSV<> &): # right( ): BireeCSV<> & # right( ) const: BireeCSV<> const & # root( ): & # root( ) const: const & # setree(owner: BireeCSV<> &, tree: BireeCSV<> &) NEcsNode McsNode _left: BireeCS<> _data: _right: BireeCS<> McsNode( ) NEcsNode(data: const &) NEcsNode(leftNode: AcsNode<> *, data:, rightnode: AcsNode<> *) NEcsNode(rhs: const NEcsNode<> *)

8 Design Patterns for Data Structures Figure 8.41 <<use>> + ~ABireeCSVVis() ABireeCSVVis + emptycase(host: BireeCSV<> &) + nonemptycase(host: BireeCSV<> &) + emptycase(host: BireeCSV<> const &) + nonemptycase(host: BireeCSV<> const &) BiCSVisEmptyVis BiCSVclearVis BiCSVmaxVis _result: bool + BiCSVclearVis( ) _result: const * + BiCSVisEmptyVis( ) + result( ) const: bool + BiCSVmaxVis( ) + result( ) const: const &

9 Design Patterns for Data Structures BireeCSV Primitive methods he primitive methods for BireeCSV are implemented exactly as they are for BireeCS.

10 Design Patterns for Data Structures Figure 8.42 // ========= accept ========= template<class > void BireeCSV<>::accept(ABireeCSVVis<> &visitor) { _root->accept(*this, visitor); template<class > void McsvNode<>::accept(BireeCSV<> &owner, ABireeCSVVis<> &visitor) { visitor.emptycase(owner); template<class > void NEcsvNode<>::accept(BireeCSV<> &owner, ABireeCSVVis<> &visitor) { visitor.nonemptycase(owner);

11 Design Patterns for Data Structures Figure 8.42 // ========= accept ========= template<class > void BireeCSV<>::accept(ABireeCSVVis<> &visitor) { _root->accept(*this, visitor); template<class > void McsvNode<>::accept(BireeCSV<> &owner, ABireeCSVVis<> &visitor) { visitor.emptycase(owner); template<class > void NEcsvNode<>::accept(BireeCSV<> &owner, ABireeCSVVis<> &visitor) { visitor.nonemptycase(owner);

12 Design Patterns for Data Structures BireeCSV he minimal complete set of methods o access the parts of a binary tree root() left() right() o build a binary tree insertroot() setree() o trim a binary tree remroot()

13 Design Patterns for Data Structures Chapter 8 he signature mechanism A plugin is a subclass of an abstract visitor. It is free to add any number of attributes of any type that are not present in its superclass. he attributes in a visitor correspond to the parameters in a non-plugin method.

14 Design Patterns for Data Structures BireeCSV Visitor translation techniques Input parameters in the native method are attributes in the visitor initialized in the constructor for the visitor. Output parameters in the native method are reference variable attributes in the visitor. Nonvoid methods in the native class require an additional method named result() that returns the same type.

15 Design Patterns for Data Structures BireeCSV BiCSVisEmptyVis he BireeCS version of isempty() is non-void. herefore, the visitor version provides a result() method.

16 Design Patterns for Data Structures BireeCSV o use a visitor for a nonvoid method: Declare a local visitor. Pass it as a parameter in the data structure s accept() method. Call the visitor s result() method to get the result of the computation.

17 Design Patterns for Data Structures Figure 8.43 // ========= BiCSVisEmptyVis ========= template<class > class BiCSVisEmptyVis : public ABireeCSVVis<> { private: bool _result; // Output result. public: // ========= Constructor ========= BiCSVisEmptyVis() { // ========= visit ========= void emptycase(bireecsv<> &host) override { _result = true; void nonemptycase(bireecsv<> &host) override { _result = false;

18 Design Patterns for Data Structures Figure 8.43 ; // ========= result ========= // Pre: his visitor has been accepted by a host tree. // Post: true is returned if the host tree is empty; // otherwise, false is returned. bool result() const { return _result; // Global function for convenience template<class > bool isempty(bireecsv<> const &tree) { BiCSVisEmptyVis<> isemptyvis; tree.accept(isemptyvis); return isemptyvis.result();

19 Design Patterns for Data Structures BireeCSV General translation rules Use host.left() in a BireeCSV visitor to correspond to _left in a BireeCS nonempty node. Use host.root() in a BireeCSV visitor to correspond to _data in a BireeCS nonempty node. Use host.right() in a BireeCSV visitor to correspond to _right in a BireeCS nonempty node.

20 Design Patterns for Data Structures BireeCSV BiCSVpreOrderVis he BireeCS version of preorder() is void. herefore, the visitor version does not provide a result() method. he BireeCS version has output parameter os, so the visitor version has reference variable attribute _os.

21 Design Patterns for Data Structures Figure 8.44 // ========= BiCSVpreOrderVis ========= template<class > class BiCSVpreOrderVis : public ABireeCSVVis<> { private: ostream &_os; // Input paramter. public: // ========= Constructor ========= BiCSVpreOrderVis(ostream &os): _os(os) { // ========= visit ========= // Pre: his visitor has been accepted by a host tree. // Post: A preorder representation of this tree is sent to os. void emptycase(bireecsv<> &host) override { ; void nonemptycase(bireecsv<> &host) override { _os << host.root() << ; host.left().accept(*this); host.right().accept(*this);

22 Design Patterns for Data Structures Figure 8.44 // Global function for convenience template<class > void preorder(ostream &os, BireeCSV<> const &tree) { BiCSVpreOrderVis<> preordervis(os); tree.accept(preordervis);

23 Design Patterns for Data Structures Figure 8.45 // ========= BiCSVnumNodesVis ========= template<class > class BiCSVnumNodesVis : public ABireeCSVVis<> { private: int _result; // Output result. public: // ========= Constructor ========= BiCSVnumNodesVis(): _result(0) { // ========= visit ========= void emptycase(bireecsv<> &host) override { void nonemptycase(bireecsv<> &host) override { host.left().accept(*this); host.right().accept(*this); _result++;

24 Design Patterns for Data Structures Figure 8.45 ; // ========= result ========= // Pre: his visitor has been accepted by a host tree. // Post: he number of nodes of the host tree is returned. int result() const { return _result; // Global function for convenience template<class > int numnodes(bireecsv<> const &tree) { BiCSVnumNodesVis<> numnodesvis; tree.accept(numnodesvis); return numnodesvis.result();

25 Design Patterns for Data Structures Figure 8.46 // ========= BiCSVequalsVis ========= template<class > class BiCSVequalsVis : public ABireeCSVVis<> { private: BireeCSV<> const &_rhs; // Input parameter. bool _result; // Output result. public: // ========= Constructor ========= BiCSVequalsVis(BireeCSV<> const &rhs): _rhs(rhs) { // ========= visit ========= void emptycase(bireecsv<> &host) override { cerr << BiCSVequalsVis: Exercise for the student. << endl; throw -1; void nonemptycase(bireecsv<> &host) override { cerr << BiCSVequalsVis: Exercise for the student. << endl; throw -1;

26 Design Patterns for Data Structures Figure 8.46 ; // ========= result ========= // Pre: his visitor has been accepted by a host tree. // Post: true is returned if the host tree is equal to rhs; // otherwise, false is returned. // wo trees are equal if they contain the same number of // equal elements with the same shape. bool result() const { cerr << BiCSVequalsVis: Exercise for the student. << endl; throw -1; // Global function for convenience. // ========= operator== ========= template<class > bool operator==(bireecsv<> const &lhs, BireeCSV<> const &rhs) { cerr << operator==: Exercise for the student. << endl; throw -1;

27 Design Patterns for Data Structures Figure 8.47 rhs _left _data _right rhs (a) he environment of a BireeCS empty node. (b) he environment of a BireeCS nonempty node. host _rhs _result (c) he environment of a BireeCSV helper visitor.

28 Design Patterns for Data Structures Figure 8.48 // ========= BiCSVequalsHelperVis ========= template<class > class BiCSVequalsHelperVis : public ABireeCSVVis<> { private: const &_data; // Input parameter. BireeCSV<> const &_left; // Input parameter. BireeCSV<> const &_right; // Input parameter. bool _result; // Output result. public: // ========= Constructor ========= BiCSVequalsHelperVis ( const &data, BireeCSV<> const &left, BireeCSV<> const &right): _data(data), _left(left), _right(right) { // ========= visit ========= void emptycase(bireecsv<> &host) override { cerr << BiCSVequalsHelperVis: Exercise for the student. << endl; throw -1; void nonemptycase(bireecsv<> &host) override { cerr << BiCSVequalsHelperVis: Exercise for the student. << endl; throw -1;

29 Design Patterns for Data Structures Figure 8.48 ; // ========= result ========= // Pre: his visitor has been accepted by a host tree. // Post: true is returned if root equals host.root(), // left equals host.left(), and right equals host.right(); // otherwise, false is returned. bool result() const { cerr << BiCSVequalsHelperVis: Exercise for the student. << endl; throw -1;

30 Design Patterns for Data Structures Figure 8.49 _left _data _right data left right (a) he environment of a BireeCS helper empty node. (b) he environment of a BireeCS helper nonempty node. host _left _data _right _result (c) he environment of a BireeCSV helper visitor.

31 Design Patterns for Data Structures Figure 8.50 he clear() operation for BireeCSV host host host host Empty node Empty node Empty node Empty node (a) Original. (b) Clear left. (c) Clear right. (d) Remove root.

The Composite State Visitor Pattern

The Composite State Visitor Pattern Chapter 6 The Composite State Visitor Pattern Chapter 6 The visitor pattern Represent an operation to be performed on elements of an object structure. Visitor lets you define a new operation without changing

More information

Design Patterns for Data Structures. Chapter 8. The Composite State Binary Tree BiTreeCS

Design Patterns for Data Structures. Chapter 8. The Composite State Binary Tree BiTreeCS Design Patterns for Data Structures Chapter 8 The Composite State Binary Tree BiTreeCS Design Patterns for Data Structures Chapter 8 The Composite State Binary Tree BiTreeCS In a nonempty node _left is

More information

Design Patterns for Data Structures. Chapter Nguyen-Wong B-Trees

Design Patterns for Data Structures. Chapter Nguyen-Wong B-Trees Chapter 10.3 Nguyen-Wong B-Trees Chapter 10.3 A B-tree is an n-way tree with the following characteristics It is balanced. It is ordered. It has a node size restriction. Chapter 10.3 A balanced n-way tree

More information

Design Patterns for Data Structures. Chapter 10. Balanced Trees

Design Patterns for Data Structures. Chapter 10. Balanced Trees Capter 10 Balanced Trees Capter 10 Four eigt-balanced trees: Red-Black binary tree Faster tan AVL for insertion and removal Adelsen-Velskii Landis (AVL) binary tree Faster tan red-black for lookup B-tree

More information

An empty list is a list. A nonempty list has two parts: a data value of some type T, and the rest of the list, which is a list.

An empty list is a list. A nonempty list has two parts: a data value of some type T, and the rest of the list, which is a list. Chapter 6 The composite pattern for a list The definition of a list: An empty list is a list. A nonempty list has two parts: a data value of some type T, and the rest of the list, which is a list. Chapter

More information

Design Patterns for Data Structures. Chapter 10. Balanced Trees

Design Patterns for Data Structures. Chapter 10. Balanced Trees Capter 10 Balanced Trees Capter 10 Four eigt-balanced trees: Red-Black binary tree Faster tan AVL for insertion and removal Adelsen-Velskii Landis (AVL) binary tree Faster tan red-black for lookup B-tree

More information

Design Patterns for Data Structures. Chapter 9. Dictionaries

Design Patterns for Data Structures. Chapter 9. Dictionaries Design Patterns for Data Structures Chapter 9 Dictionaries Design Patterns for Data Structures Chapter 9 The key-value pair A dictionary provides storage based on the key retrieval based on the key access

More information

Design Patterns for Data Structures. Chapter 9. Dictionaries

Design Patterns for Data Structures. Chapter 9. Dictionaries Design Patterns for Data Structures Chapter 9 Dictionaries Design Patterns for Data Structures Chapter 9 The key-value pair A dictionary provides storage based on the key retrieval based on the key access

More information

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST)

CSCI Lab 9 Implementing and Using a Binary Search Tree (BST) CSCI Lab 9 Implementing and Using a Binary Search Tree (BST) Preliminaries In this lab you will implement a binary search tree and use it in the WorkerManager program from Lab 3. Start by copying this

More information

Declarations and Access Control SCJP tips

Declarations and Access Control  SCJP tips Declarations and Access Control www.techfaq360.com SCJP tips Write code that declares, constructs, and initializes arrays of any base type using any of the permitted forms both for declaration and for

More information

Object-Oriented Design (OOD) and C++

Object-Oriented Design (OOD) and C++ Chapter 2 Object-Oriented Design (OOD) and C++ At a Glance Instructor s Manual Table of Contents Chapter Overview Chapter Objectives Instructor Notes Quick Quizzes Discussion Questions Projects to Assign

More information

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

More information

Design Patterns for Data Structures. Chapter 9.5. Hash Tables

Design Patterns for Data Structures. Chapter 9.5. Hash Tables Hash Tables Binary information representation 0 0000 8 1000 1 0001 9 1001 2 0010 A 1010 3 0011 B 1011 4 0100 C 1100 5 0101 D 1101 6 0110 E 1110 7 0111 F 1111 (a) Hexadecimal abbreviation for binary values.

More information

Topic 7: Algebraic Data Types

Topic 7: Algebraic Data Types Topic 7: Algebraic Data Types 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 5.5, 5.7, 5.8, 5.10, 5.11, 5.12, 5.14 14.4, 14.5, 14.6 14.9, 14.11,

More information

template<class T> class LNode { private: T _data; LNode *_next; template<class T> class ListL { private: LNode<T> *_head;

template<class T> class LNode { private: T _data; LNode *_next; template<class T> class ListL { private: LNode<T> *_head; Chapter 6 Mutable Lists Chapter 5 describes immutable lists, that is, lists that cannot change. If you want a new list that is some alteration of a given list you must make a new list from the old list

More information

Inheritance and Interfaces

Inheritance and Interfaces Inheritance and Interfaces Object Orientated Programming in Java Benjamin Kenwright Outline Review What is Inheritance? Why we need Inheritance? Syntax, Formatting,.. What is an Interface? Today s Practical

More information

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles Abstract Data Types (ADTs) CS 247: Software Engineering Principles ADT Design An abstract data type (ADT) is a user-defined type that bundles together: the range of values that variables of that type can

More information

Name CPTR246 Spring '17 (100 total points) Exam 3

Name CPTR246 Spring '17 (100 total points) Exam 3 Name CPTR246 Spring '17 (100 total points) Exam 3 1. Linked Lists Consider the following linked list of integers (sorted from lowest to highest) and the changes described. Make the necessary changes in

More information

CS 247: Software Engineering Principles. ADT Design

CS 247: Software Engineering Principles. ADT Design CS 247: Software Engineering Principles ADT Design Readings: Eckel, Vol. 1 Ch. 7 Function Overloading & Default Arguments Ch. 12 Operator Overloading U Waterloo CS247 (Spring 2017) p.1/17 Abstract Data

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Implementation Kostas Alexis s Definition: A (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction

More information

CS11 Introduction to C++ Fall Lecture 7

CS11 Introduction to C++ Fall Lecture 7 CS11 Introduction to C++ Fall 2012-2013 Lecture 7 Computer Strategy Game n Want to write a turn-based strategy game for the computer n Need different kinds of units for the game Different capabilities,

More information

Example Final Questions Instructions

Example Final Questions Instructions Example Final Questions Instructions This exam paper contains a set of sample final exam questions. It is for practice purposes only. You ll most likely need longer than three hours to answer all the questions.

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms First Semester 2017/2018 Linked Lists Eng. Anis Nazer Linked List ADT Is a list of nodes Each node has: data (can be any thing, int, char, Person, Point, day,...) link to

More information

This examination has 11 pages. Check that you have a complete paper.

This examination has 11 pages. Check that you have a complete paper. MARKING KEY The University of British Columbia MARKING KEY Computer Science 252 2nd Midterm Exam 6:30 PM, Monday, November 8, 2004 Instructors: K. Booth & N. Hutchinson Time: 90 minutes Total marks: 90

More information

Lab 2: ADT Design & Implementation

Lab 2: ADT Design & Implementation Lab 2: ADT Design & Implementation By Dr. Yingwu Zhu, Seattle University 1. Goals In this lab, you are required to use a dynamic array to design and implement an ADT SortedList that maintains a sorted

More information

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED

UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED UNIT 3 ARRAYS, RECURSION, AND COMPLEXITY CHAPTER 11 CLASSES CONTINUED EXERCISE 11.1 1. static public final int DEFAULT_NUM_SCORES = 3; 2. Java allocates a separate set of memory cells in each instance

More information

Java Object Oriented Design. CSC207 Fall 2014

Java Object Oriented Design. CSC207 Fall 2014 Java Object Oriented Design CSC207 Fall 2014 Design Problem Design an application where the user can draw different shapes Lines Circles Rectangles Just high level design, don t write any detailed code

More information

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals

Binary Tree Node Relationships. Binary Trees. Quick Application: Expression Trees. Traversals Binary Trees 1 Binary Tree Node Relationships 2 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the

More information

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management

Binary Trees. For example: Jargon: General Binary Trees. root node. level: internal node. edge. leaf node. Data Structures & File Management Binary Trees 1 A binary tree is either empty, or it consists of a node called the root together with two binary trees called the left subtree and the right subtree of the root, which are disjoint from

More information

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors

POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors CSC 330 OO Software Design 1 Abstract Base Classes class B { // base class virtual void m( ) =0; // pure virtual

More information

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE

POLYMORPHISM 2 PART. Shared Interface. Discussions. Abstract Base Classes. Abstract Base Classes and Pure Virtual Methods EXAMPLE Abstract Base Classes POLYMORPHISM 2 PART Abstract Classes Static and Dynamic Casting Common Programming Errors class B { // base class virtual void m( ) =0; // pure virtual function class D1 : public

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Fall 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract Classes

More information

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

C++ Review. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University C++ Review CptS 223 Advanced Data Structures Larry Holder School of Electrical Engineering and Computer Science Washington State University 1 Purpose of Review Review some basic C++ Familiarize us with

More information

C++ Templates. David Camp

C++ Templates. David Camp C++ Templates David Camp C Marcos #define () #define min(i, j) (((i) < (j))? (i) : (j)) #define max(i, j) (((i) > (j))? (i) : (j)) #define RADTODEG(x)

More information

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Inheritance, and Polymorphism.

Inheritance, and Polymorphism. Inheritance and Polymorphism by Yukong Zhang Object-oriented programming languages are the most widely used modern programming languages. They model programming based on objects which are very close to

More information

Questions: ECE551 PRACTICE Final

Questions: ECE551 PRACTICE Final ECE551 PRACTICE Final This is a full length practice midterm exam. If you want to take it at exam pace, give yourself 175 minutes to take the entire test. Just like the real exam, each question has a point

More information

Chapter 14 Abstract Classes and Interfaces

Chapter 14 Abstract Classes and Interfaces Chapter 14 Abstract Classes and Interfaces 1 What is abstract class? Abstract class is just like other class, but it marks with abstract keyword. In abstract class, methods that we want to be overridden

More information

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe

OBJECT ORIENTED PROGRAMMING USING C++ CSCI Object Oriented Analysis and Design By Manali Torpe OBJECT ORIENTED PROGRAMMING USING C++ CSCI 5448- Object Oriented Analysis and Design By Manali Torpe Fundamentals of OOP Class Object Encapsulation Abstraction Inheritance Polymorphism Reusability C++

More information

CMSC 341 Lecture 10 Binary Search Trees

CMSC 341 Lecture 10 Binary Search Trees CMSC 341 Lecture 10 Binary Search Trees John Park Based on slides from previous iterations of this course Review: Tree Traversals 2 Traversal Preorder, Inorder, Postorder H X M A K B E N Y L G W UMBC CMSC

More information

Review: C++ Basic Concepts. Dr. Yingwu Zhu

Review: C++ Basic Concepts. Dr. Yingwu Zhu Review: C++ Basic Concepts Dr. Yingwu Zhu Outline C++ class declaration Constructor Overloading functions Overloading operators Destructor Redundant declaration A Real-World Example Question #1: How to

More information

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions:

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Summer I Instructions: VIRG INIA POLYTECHNIC INSTITUTE AND STATE U T PROSI M UNI VERSI TY Instructions: Print your name in the space provided below. This examination is closed book and closed notes, aside from the permitted

More information

Practice for Chapter 11

Practice for Chapter 11 Practice for Chapter 11 MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) Object-oriented programming allows you to derive new classes from existing

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Midterm #2 Examination 12:30 noon, Thursday, March 15, 2012 Instructor: K. S. Booth Time: 70 minutes (one hour ten minutes)

More information

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011

CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 Print Your Name: Page 1 of 8 Signature: Aludra Loginname: CSCI 102L - Data Structures Midterm Exam #1 Fall 2011 (10:00am - 11:12am, Wednesday, October 5) Instructor: Bill Cheng Problems Problem #1 (24

More information

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017

UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering. Program 8 EECE.3220 Data Structures Fall 2017 UNIVERSITY OF MASSACHUSETTS LOWELL Department of Electrical and Computer Engineering Program 8 EECE.3220 Data Structures Fall 2017 Binary Search Trees and Class Templates Word Counter Application The object

More information

Data Structures (INE2011)

Data Structures (INE2011) Data Structures (INE2011) Electronics and Communication Engineering Hanyang University Haewoon Nam Lecture 4 1 Stacks Insertion and deletion are made at one end () Last input first output (LIFO) Inserting

More information

CSCE 110 PROGRAMMING FUNDAMENTALS

CSCE 110 PROGRAMMING FUNDAMENTALS CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class Prof. amr Goneid, AUC 1 Dictionaries(1): A Key Table Class Prof. Amr Goneid, AUC 2 A Key Table

More information

OBJECT ORIENTED PROGRAMMING USING C++

OBJECT ORIENTED PROGRAMMING USING C++ OBJECT ORIENTED PROGRAMMING USING C++ Overview of C++ Overloading Overloading occurs when the same operator or function name is used with different signatures Both operators and functions can be overloaded

More information

More On inheritance. What you can do in subclass regarding methods:

More On inheritance. What you can do in subclass regarding methods: More On inheritance What you can do in subclass regarding methods: The inherited methods can be used directly as they are. You can write a new static method in the subclass that has the same signature

More information

Overloaded Operators, Functions, and Students

Overloaded Operators, Functions, and Students , Functions, and Students Division of Mathematics and Computer Science Maryville College Outline Overloading Symbols 1 Overloading Symbols 2 3 Symbol Overloading Overloading Symbols A symbol is overloaded

More information

Programming II (CS300)

Programming II (CS300) 1 Programming II (CS300) Chapter 05: Inheritance and Interfaces MOUNA KACEM mouna@cs.wisc.edu Spring 2018 Inheritance and Interfaces 2 Introduction Inheritance and Class Hierarchy Polymorphism Abstract

More information

Example Final Questions Instructions

Example Final Questions Instructions Example Final Questions Instructions This exam paper contains a set of sample final exam questions. It is for practice purposes only. You ll most likely need longer than three hours to answer all the questions.

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2305/lab06.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 2 Extend the IntegerSet class from Lab 04 to provide the following

More information

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008

CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008 CSE143 Summer 2008 Final Exam Part B KEY August 22, 2008 Name : Section (eg. AA) : TA : This is an open-book/open-note exam. Space is provided for your answers. Use the backs of pages if necessary. The

More information

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive) Final Exam Exercises Chapters 1-7 + 11 Write C++ code to: l Determine if a number is odd or even CS 2308 Fall 2016 Jill Seaman l Determine if a number/character is in a range - 1 to 10 (inclusive) - between

More information

MS OE. Quizzes. Name:

MS OE. Quizzes. Name: The following algorithm accepts a vector of coefficients and an x value and returns the corresponding polynomial evaluated at x. double PolynomialEval ( const ve cto r& c o e f f, double x ) 2

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

Chapter 10 Classes Continued. Fundamentals of Java

Chapter 10 Classes Continued. Fundamentals of Java Chapter 10 Classes Continued Objectives Know when it is appropriate to include class (static) variables and methods in a class. Understand the role of Java interfaces in a software system and define an

More information

2003 AP COMPUTER SCIENCE AB FREE-RESPONSE QUESTIONS

2003 AP COMPUTER SCIENCE AB FREE-RESPONSE QUESTIONS COMPUTER SCIENCE AB SECTION II Time 1 hour and 45 minutes Number of questions 4 Percent of total grade 50 Directions: SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN C++. Note:

More information

CS 61B, Summer 2001 Midterm Exam II Professor Michael Olan

CS 61B, Summer 2001 Midterm Exam II Professor Michael Olan CS 61B, Summer 2001 Midterm Exam II Professor Michael Olan Problem 1 (8 points - 1 pt each parts a.-e., 2 pts part f.) a. Consider the following proof: Suppose f(n) is O(g(n)). From this we know that g(n)

More information

Chapter 12 - Templates

Chapter 12 - Templates Chapter 12 - Templates O utline 12.1 Introd uction 12.2 Function Te m plate s 12.3 Ove rload ing Te m plate Functions 12.4 Class Te m p late s 12.5 Class Te m plate s and Non-type Param e te rs 12.6 Te

More information

ECE 2400 Computer Systems Programming Fall 2017 Topic 14: C++ Inheritance

ECE 2400 Computer Systems Programming Fall 2017 Topic 14: C++ Inheritance ECE 200 Computer Systems Programming Fall 2017 Topic 1: C++ Inheritance School of Electrical and Computer Engineering Cornell University revision: 2017-11-06-09-36 1 Introduction to C++ Inheritance 2 1.1.

More information

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012

MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012 MARKING KEY The University of British Columbia MARKING KEY Computer Science 260 Final Examination 8:30 AM, Wednesday, April 18, 2012 Instructor: K. S. Booth Time: 150 minutes (two hours thirty minutes)

More information

ECE Fall 20l2, Second Exam

ECE Fall 20l2, Second Exam ECE 30862 Fall 20l2, Second Exam DO NOT START WORKING ON THIS UNTIL TOLD TO DO SO. LEAVE IT ON THE DESK. You have until 12:20 to take this exam. Your exam should have 16 pages total (including this cover

More information

Data Structures and Algorithms

Data Structures and Algorithms Data Structures and Algorithms Alice E. Fischer Lecture 3 217 Alice E. Fischer Data Structures L3... 1/31 1 Arrays Growing Arrays vectors 2 3 Empty Lists Inserting Data - Unsorted Inserting Data - Sorted

More information

CpSc212 Goddard Notes Chapter 10. Linked Lists

CpSc212 Goddard Notes Chapter 10. Linked Lists CpSc212 Goddard Notes Chapter 10 Linked Lists 10.1 Links and Pointers The linked list is not an ADT in its own right; rather it is a way of implementing many data structures. It is designed to replace

More information

Test 1: CPS 100. Owen Astrachan. October 11, 2000

Test 1: CPS 100. Owen Astrachan. October 11, 2000 Test 1: CPS 100 Owen Astrachan October 11, 2000 Name: Login: Honor code acknowledgment (signature) Problem 1 Problem 2 Problem 3 Problem 4 TOTAL: value 30 pts. 16 pts. 12 pts. 20 pts. 78 pts. grade This

More information

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad

SCJ2013 Data Structure & Algorithms. Binary Search Tree. Nor Bahiah Hj Ahmad SCJ2013 Data Structure & Algorithms Binary Search Tree Nor Bahiah Hj Ahmad Binary Search Tree A binary search tree has the following properties: For every node n in the tree Value of n is greater than

More information

Topic Binary Trees (Non-Linear Data Structures)

Topic Binary Trees (Non-Linear Data Structures) Topic Binary Trees (Non-Linear Data Structures) CIS210 1 Linear Data Structures Arrays Linked lists Skip lists Self-organizing lists CIS210 2 Non-Linear Data Structures Hierarchical representation? Trees

More information

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity.

OOPS Viva Questions. Object is termed as an instance of a class, and it has its own state, behavior and identity. OOPS Viva Questions 1. What is OOPS? OOPS is abbreviated as Object Oriented Programming system in which programs are considered as a collection of objects. Each object is nothing but an instance of a class.

More information

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L

Inheritance. Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L Inheritance Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 9.4 1 Inheritance Inheritance allows a software developer to derive

More information

2 ADT Programming User-defined abstract data types

2 ADT Programming User-defined abstract data types Preview 2 ADT Programming User-defined abstract data types user-defined data types in C++: classes constructors and destructors const accessor functions, and inline functions special initialization construct

More information

Intermediate Programming & Design (C++) Classes in C++

Intermediate Programming & Design (C++) Classes in C++ Classes in C++ A class is a data type similar to a C structure. It includes various local data (called data members) together with constructors, destructors and member functions. All of them are called

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Tree Implementations Kostas Alexis Nodes in a Binary Tree Representing tree nodes Must contain both data and pointers to node s children Each node will be an object

More information

CS11 Advanced C++ Fall Lecture 3

CS11 Advanced C++ Fall Lecture 3 CS11 Advanced C++ Fall 2006-2007 Lecture 3 Today s Topics C++ Standard Exceptions Exception Cleanup Fun with Exceptions Exception Specifications C++ Exceptions Exceptions are nice for reporting many errors

More information

Topics. bool and string types input/output library functions comments memory allocation templates classes

Topics. bool and string types input/output library functions comments memory allocation templates classes C++ Primer C++ is a major extension of c. It is similar to Java. The lectures in this course use pseudo-code (not C++). The textbook contains C++. The labs involve C++ programming. This lecture covers

More information

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes.

Abstract Classes. Abstract Classes a and Interfaces. Class Shape Hierarchy. Problem AND Requirements. Abstract Classes. a and Interfaces Class Shape Hierarchy Consider the following class hierarchy Shape Circle Square Problem AND Requirements Suppose that in order to exploit polymorphism, we specify that 2-D objects must

More information

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine

Homework 6. Yuji Shimojo CMSC 330. Instructor: Prof. Reginald Y. Haseltine Homework 6 Yuji Shimojo CMSC 330 Instructor: Prof. Reginald Y. Haseltine July 21, 2013 Question 1 What is the output of the following C++ program? #include #include using namespace

More information

Binary Search Trees Part Two

Binary Search Trees Part Two Binary Search Trees Part Two Recap from Last Time Binary Search Trees A binary search tree (or BST) is a data structure often used to implement maps and sets. The tree consists of a number of nodes, each

More information

CS302 - Data Structures using C++

CS302 - Data Structures using C++ CS302 - Data Structures using C++ Topic: Queues and Priority Queues Kostas Alexis Implementations of the ADT Queue Like stacks, queues can have Array-based or Link-based implementation Can also use implementation

More information

CSC 210, Exam Two Section February 1999

CSC 210, Exam Two Section February 1999 Problem Possible Score 1 12 2 16 3 18 4 14 5 20 6 20 Total 100 CSC 210, Exam Two Section 004 7 February 1999 Name Unity/Eos ID (a) The exam contains 5 pages and 6 problems. Make sure your exam is complete.

More information

Due Date: See Blackboard

Due Date: See Blackboard Source File: ~/2315/04/lab04.(C CPP cpp c++ cc cxx cp) Input: Under control of main function Output: Under control of main function Value: 3 In this assignment create an IntegerSet class that will provide

More information

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University

Cpt S 122 Data Structures. Course Review FINAL. Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 Data Structures Course Review FINAL Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Final When: Wednesday (12/12) 1:00 pm -3:00 pm Where: In Class

More information

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination

University of Illinois at Urbana-Champaign Department of Computer Science. First Examination University of Illinois at Urbana-Champaign Department of Computer Science First Examination CS 225 Data Structures and Software Principles Spring 2007 7p-9p, Thursday, March 1 Name: NetID: Lab Section

More information

Overloading & Polymorphism

Overloading & Polymorphism Overloading & Polymorphism Overloading is considered ad-hoc polymorphism. 1 Can define new meanings (functions) of operators for specific types. Compiler recognizes which implementation to use by signature

More information

Lab 11. A sample of the class is:

Lab 11. A sample of the class is: Lab 11 Lesson 11-2: Exercise 1 Exercise 2 A sample of the class is: public class List // Methods public void store(int item) values[length] = item; length++; public void printlist() // Post: If the list

More information

Object-Oriented Principles and Practice / C++

Object-Oriented Principles and Practice / C++ Object-Oriented Principles and Practice / C++ Alice E. Fischer May 13, 2013 OOPP / C++ Lecture 7... 1/27 Construction and Destruction Allocation and Deallocation Move Semantics Template Classes Example:

More information

Polymorphism. return a.doublevalue() + b.doublevalue();

Polymorphism. return a.doublevalue() + b.doublevalue(); Outline Class hierarchy and inheritance Method overriding or overloading, polymorphism Abstract classes Casting and instanceof/getclass Class Object Exception class hierarchy Some Reminders Interfaces

More information

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1

Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Introduction to C++ Introduction to C++ Dr Alex Martin 2013 Slide 1 Inheritance Consider a new type Square. Following how we declarations for the Rectangle and Circle classes we could declare it as follows:

More information

Chapter 6 Introduction to Defining Classes

Chapter 6 Introduction to Defining Classes Introduction to Defining Classes Fundamentals of Java: AP Computer Science Essentials, 4th Edition 1 Objectives Design and implement a simple class from user requirements. Organize a program in terms of

More information

What does it mean by information hiding? What are the advantages of it? {5 Marks}

What does it mean by information hiding? What are the advantages of it? {5 Marks} SECTION ONE (COMPULSORY) Question #1 [30 Marks] a) Describe the main characteristics of object-oriented programming. {5 Marks Encapsulation the ability to define a new type and a set of operations on that

More information

C# Programming for Developers Course Labs Contents

C# Programming for Developers Course Labs Contents C# Programming for Developers Course Labs Contents C# Programming for Developers...1 Course Labs Contents...1 Introduction to C#...3 Aims...3 Your First C# Program...3 C# The Basics...5 The Aims...5 Declaring

More information

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms. Final Examination University of Waterloo Department of Electrical and Computer Engineering ECE 250 Data Structures and Algorithms Instructor: Douglas Wilhelm Harder Time: 2.5 hours Aides: none 14 pages Final Examination

More information

CMSC 341. Binary Search Trees CMSC 341 BST

CMSC 341. Binary Search Trees CMSC 341 BST CMSC 341 Binary Search Trees CMSC 341 BST Announcements Homework #3 dues Thursday (10/5/2017) Exam #1 next Thursday (10/12/2017) CMSC 341 BST A Generic Tree CMSC 341 BST Binary Tree CMSC 341 BST The Binary

More information

1.00 Lecture 14. Exercise: Plants

1.00 Lecture 14. Exercise: Plants 1.00 Lecture 14 Inheritance, part 2 Reading for next time: Big Java: sections 9.1-9.4 Exercise: Plants Create a base class Plant Plant: Private data genus, species, isannual Write the constructor Create

More information

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012

University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012 University of Swaziland Department Of Computer Science Supplementary Examination JULY 2012 Title ofpaper: Course number: Time Allowed: Cunder Unix CS344 Three (3) hours Instructions: Answer question 1.

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

8. Object-oriented Programming. June 15, 2010

8. Object-oriented Programming. June 15, 2010 June 15, 2010 Introduction to C/C++, Tobias Weinzierl page 1 of 41 Outline Recapitulation Copy Constructor & Operators Object-oriented Programming Dynamic and Static Polymorphism The Keyword Static The

More information