CSI33 Data Structures



Similar documents
Comp151. Definitions & Declarations

Cpt S 223. School of EECS, WSU

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

Basics of C++ and object orientation in OpenFOAM

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

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

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

C++ INTERVIEW QUESTIONS

Answers to Review Questions Chapter 7

Chapter 5 Functions. Introducing Functions

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

N3458: Simple Database Integration in C++11

C++ Crash Kurs. C++ Object-Oriented Programming

Visual Studio 2008 Express Editions

An Incomplete C++ Primer. University of Wyoming MA 5310

Queue Implementations

Copyright 2001, Bill Trudell. Permission is granted to copy for the PLoP 2001 conference. All other rights reserved.

COMPUTER SCIENCE 1999 (Delhi Board)

Short Notes on Dynamic Memory Allocation, Pointer and Data Structure

Object Oriented Software Design II

! " # $ %& %' ( ) ) *%%+, -..*/ *%%+ - 0 ) 1 2 1

Basics of I/O Streams and File I/O

CS107L Handout 04 Autumn 2007 October 19, 2007 Custom STL-Like Containers and Iterators

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

EP241 Computer Programming

sqlpp11 - An SQL Library Worthy of Modern C++

Bevezetés a C++ Standard Template Library-be

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

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

7.7 Case Study: Calculating Depreciation

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Pointers and Linked Lists

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Functional Programming in C++11

Appendix K Introduction to Microsoft Visual C++ 6.0

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

C++FA 5.1 PRACTICE MID-TERM EXAM

Programming Database lectures for mathema

Iterator Pattern. CSIE Department, NTUT Chien-Hung Liu. Provide a way to access the elements of an aggregate object sequentially

Ch 7-1. Object-Oriented Programming and Classes

Binary storage of graphs and related data

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

Moving from C++ to VBA

Computer Programming C++ Classes and Objects 15 th Lecture

Sequential Program Execution

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

C++FA 3.1 OPTIMIZING C++

Data Structures, Practice Homework 2, with Solutions (not to be handed in)

Function Overloading I

Chapter 5. Selection 5-1

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

Getting Started with the Internet Communications Engine

CORBA Programming with TAOX11. The C++11 CORBA Implementation

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

Cours de C++ Utilisations des conteneurs

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

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Polymorphism. Problems with switch statement. Solution - use virtual functions (polymorphism) Polymorphism

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

While Loop. 6. Iteration

Analysis of a Search Algorithm

Chapter One Introduction to Programming

Sample Questions Csci 1112 A. Bellaachia

C++ Language Tutorial

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Coding conventions and C++-style

Passing 1D arrays to functions.

Lecture 2 Notes: Flow of Control

Object-Oriented Programming

C++ Overloading, Constructors, Assignment operator

Syllabus OBJECT ORIENTED PROGRAMMING C++

2.2: Bitwise Logical Operations

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

5 CLASSES CHAPTER. 5.1 Object-Oriented and Procedural Programming. 5.2 Classes and Objects 5.3 Sample Application: A Clock Class

Conditions & Boolean Expressions

1.1.3 Syntax The syntax for creating a derived class is very simple. (You will wish everything else about it were so simple though.

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

Informatica e Sistemi in Tempo Reale

How to: Use Basic C++, Syntax and Operators

Reading and Writing PCD Files The PCD File Format The Grabber Interface Writing a Custom Grabber PCL :: I/O. Suat Gedikli, Nico Blodow

El Dorado Union High School District Educational Services

Appendix M: Introduction to Microsoft Visual C Express Edition

Syntax Check of Embedded SQL in C++ with Proto

Help on the Embedded Software Block

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

Data Structures using OOP C++ Lecture 1

C++ Programming Language

It has a parameter list Account(String n, double b) in the creation of an instance of this class.

Chapter Objectives. Chapter 7. Stacks. Various Types of Stacks LIFO. Empty Stack. Stacks

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

CHAPTER 4 ESSENTIAL DATA STRUCTRURES

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Chapter 8 Selection 8-1

Object Oriented Software Design

Creating a Simple Visual C++ Program

Transcription:

Outline Department of Mathematics and Computer Science Bronx Community College November 25, 2015

Outline Outline 1 Chapter 12: C++ Templates

Outline Chapter 12: C++ Templates 1 Chapter 12: C++ Templates

Templates Allow Code For Different Types Python doesn t associate types with variable names, so the same code might work for different types. In this example, the function Maximum will find the larger of two numbers having the same type (as long as the operator > is defined for that type). For example, the types int, float, and even Rational will work here: def Maximum(a, b): if a > b: return a else: return b

C++: Different Versions For Different Types int maximum int(int a, int b) { if (a > b){ return a; else { return b;

C++: Different Versions For Different Types int maximum double(double a, double b) { if (a > b){ return a; else { return b;

Template Function Example: C++ template <typename Item> Item maximum(item a, Item b) { if (a > b) { return a; else { return b;

Template Function Example: C++ int main() { int a=3, b=4; double x=5.5, y=2.0; cout << maximum(a, b) << endl; cout << maximum(x, y) << endl; return 0;

C++ : Container Classes Container classes which provide certain access to each item (stack, Queue,...) all behave the same for different data types of the items contained. Iterators should be provided to allow abstract traversal (without needing to know how the container is implemented). C++ template classes are able to provide this.

C++ : Container Classes As each container class is used for some datatype, the compiled template class for that type is instantiated. No code for a template class instance is compiled until it is needed.

The Standard Template Library The STL implements most of the common container classes as C++ template classes. It is now a standard part of the C++ library. It defines a wide variety of containers for classes which implement a few basic operations. (For example, < for binary search trees or priority queues.) It provides iterators for these classes.

The vector Template Class int main() { vector<int> iv; vector<double> dv; int i; for (i=0; i<10; ++i) { iv.push back(i); dv.push back(i + 0.5); for (i=0; i< 10; ++i) { cout << iv[i] << " " << dv[i] << endl; return 0;

The vector Template Class #include <iostream> #include <vector> using namespace std; int main() { vector<int> iv(5, 3); vector<double> dv(5); int i; for (i=0; i<5; ++i) { cout << iv[i] << " " << dv[i] << endl;

The vector Template Class #include <iostream> #include <vector> using namespace std; int main() { vector<int> iv; vector<int>::iterator iter; int i; for (i=0; i<10; ++i) { iv.push back(i); for (iter=iv.begin(); iter!= iv.end(); ++iter) { cout << *iter << endl; return 0;

User-Defined The header file, <classname>.h, is the same for ordinary classes, but class definition has a template data type, a wild card typename instead of a normal type like int or double. The class definition is preceded by template <typename T> where T can be any identifier not in use. (for example, Item in the stack class.) Whenever the template data type is needed in a function declaration, it is used like an ordinary type name: bool pop(item &item); The last line of the header file includes the implementation file: #include "<classname>.template" (which does not include the header file).

User-Defined template <typename Item> class Stack { public: Stack(); ~Stack(); int size() const { return size ; bool top(item &item) const; bool push(const Item &item); bool pop(item &item);

User-Defined private: Stack(const Stack &s); void operator=(const Stack &s); void resize(); Item *s ; int size ; int capacity ; ; #include "Stack.template"

User-Defined // Stack.template template <typename Item> Stack<Item>::Stack() { s = NULL; size = 0; capacity = 0; template <typename Item> Stack<Item>::~Stack() { delete [] s ;

User-Defined // test Stack.cpp #include "Stack.h" int main() { Stack<int> int stack; Stack<double> double stack; int stack.push(3); double stack.push(4.5); return 0;