Purpose of Review. Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures

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

Chapter 17: Linked Lists

Abstract Data Types 1

Dynamic Data Structures

Abstract Data Types 1

Abstract Data Types. CptS 223 Advanced Data Structures. Larry Holder School of Electrical Engineering and Computer Science Washington State University

Chapter 17: Linked Lists

(8 1) Container Classes & Class Templates D & D Chapter 18. Instructor - Andrew S. O Fallon CptS 122 (October 8, 2018) Washington State University

CS197c: Programming in C++

DATA STRUCTURES AND ALGORITHMS LECTURE 08 QUEUES IMRAN IHSAN ASSISTANT PROFESSOR AIR UNIVERSITY, ISLAMABAD

CS 103 Unit 15. Doubly-Linked Lists and Deques. Mark Redekopp

CS 103 Unit 12 Slides

Chapter 18: Stacks And Queues

EE 355 Unit 11b. Doubly-Linked Lists and Deques. Mark Redekopp

Template based set of collection classes STL collection types (container types)

STL: C++ Standard Library

Short Notes of CS201

Standard Template Library

CS201 - Introduction to Programming Glossary By

SSE2034: System Software Experiment 3

1. The term STL stands for?

Unit 1: Preliminaries Part 4: Introduction to the Standard Template Library

Sequential Containers Cont'd

EE 355 Unit 10. C++ STL - Vectors and Deques. Mark Redekopp

1 Short Answer (7 Points Each)

Absolute C++ Walter Savitch

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 2/10/2013

COEN244: Class & function templates

List, Stack, and Queues

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

Unit 4 Basic Collections

a data type is Types

CS302. Today s Topics: More on constructor/destructor functions More on STL <list>

LECTURE 03 LINKED LIST

Course Review for. Cpt S 223 Fall Cpt S 223. School of EECS, WSU

Linear Structures. Linear Structure. Implementations. Array details. List details. Operations 4/18/2013

CS183 Software Design. Textbooks. Grading Criteria. CS183-Su02-Lecture 1 20 May by Eric A. Durant, Ph.D. 1

Data Structures and Algorithm Analysis

Programming with Haiku

Chapter 18: Stacks And Queues

Chapter 5. The Standard Template Library.

Queue Implementations

Lecture 12. Monday, February 7 CS 215 Fundamentals of Programming II - Lecture 12 1

Chapter 18: Stacks And Queues. Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS 32. Lecture 5: Templates

THINK LIKE CREATIVE PROBLEM SOLVING V. ANTON SPRAUL

vector<int> second (4,100); // four ints with value 100 vector<int> third (second.begin(),second.end()); // iterating through second

Exam 3 Chapters 7 & 9

The Standard Template Library Classes

Discussion 2C Notes (Week 3, January 21) TA: Brian Choi Section Webpage:

Objects and Classes. Basic OO Principles. Classes in Java. Mark Allen Weiss Copyright 2000

Introducing C++ to Java Programmers

CMSC 202 Final May 19, Name: UserID: (Circle your section) Section: 101 Tuesday 11: Thursday 11:30

CSCI-1200 Data Structures Fall 2010 Lecture 8 Iterators

ECE 250 Data Structures and Algorithms MID-TERM EXAMINATION B /13:30-14:50 MC-4021/RCH-211

CSCI-1200 Data Structures Fall 2014 Lecture 8 Iterators

Exercise 6.2 A generic container class

CS11 Advanced C++ Fall Lecture 1

About this exam review

Polymorphism. Programming in C++ A problem of reuse. Swapping arguments. Session 4 - Genericity, Containers. Code that works for many types.

Quiz Start Time: 09:34 PM Time Left 82 sec(s)

CS201 Latest Solved MCQs

pointers & references

Templates and Vectors

Lecture 7. Log into Linux New documents posted to course webpage

7 TEMPLATES AND STL. 7.1 Function Templates

Lesson 13 - Vectors Dynamic Data Storage

Classes and Objects. Class scope: - private members are only accessible by the class methods.

STL components. STL: C++ Standard Library Standard Template Library (STL) Main Ideas. Components. Encapsulates complex data structures and algorithms

Object Oriented Paradigm

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

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

l Operators such as =, +, <, can be defined to l The function names are operator followed by the l Otherwise they are like normal member functions:

Linked Lists. Linked list: a collection of items (nodes) containing two components: Data Address (link) of the next node in the list

C++ and OO. l C++ classes and OO. l More Examples. l HW2. C++ for C Programmers by Ira Pohl

CMSC 341 Lecture 6 STL, Stacks, & Queues. Based on slides by Lupoli, Dixon & Gibson at UMBC

Vector. Vector Class. class Vector { public: typedef unsigned int size_type;

Sequential Containers Cont'd

Lectures 19, 20, 21. two valid iterators in [first, last) such that i precedes j, then *j is not less than *i.

The Class. Classes and Objects. Example class: Time class declaration with functions defined inline. Using Time class in a driver.

C++ 프로그래밍실습. Visual Studio Smart Computing Laboratory

CPSC 427a: Object-Oriented Programming

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

EECS168 Exam 3 Review

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

CS250 Final Review Questions

Arrays and Linked Lists

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Problem Solving with C++

class Polynomial { public: Polynomial(const string& N = "no name", const vector<int>& C = vector<int>());... };

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

Lectures 11,12. Online documentation & links

STL Containers, Part I

STL Standard Template Library

COP4530 Data Structures, Algorithms and Generic Programming Recitation 4 Date: September 14/18-, 2008

Array Elements as Function Parameters

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

CSCI-1200 Data Structures Spring 2018 Lecture 8 Templated Classes & Vector Implementation

Transcription:

C++ Review 1

Purpose of Review Review some basic C++ Familiarize us with Weiss s style Introduce specific constructs useful for implementing data structures 2

Class The Class defines the data structure and the operations that access and manipulate it Member data Member functions or methods Encapsulation = data + methods Information hiding Public vs. private vs. protected 3

Comment Constructor Constructor 4

Encapsulation 5 Information Hiding

Separation of Interface and Implementation Interface (.h) file Defines class and its member data and functions Implementation (.cc or.cpp) file Provides implementations of member functions 6

Extra Syntax Default parameters Initializer list Explicit constructor Constant member function Accessor methods Mutator methods 7

Explicit Constructor Default Parameters Accessor Initializer List Mutator IntCell obj; obj = 37; 8

Preprocessor Directives IntCell class interface in file IntCell.h. 9

Preprocessor Directive Scoping Operator ClassName::member IntCell class implementation in file IntCell.cpp. 10

Preprocessor Directives Default class Program using IntCell class in file TestIntCell.cpp. 11

C++ Details Pointers Parameter passing Return passing Reference variables Destructor, copy constructor, operator= 12

Pointers Address-of operator & IntCell icobj; IntCell *m = & icobj; No garbage collection in C++ 13

Parameter Passing Call by value Small objects not altered by function Call by constant reference Large objects not altered by function Call by reference Objects altered by function double avg (const vector<int> & arr, int n, bool & errorflag); 14

What s wrong with this code? Return passing 15

Reference Variables As seen, can be used for parameter passing Also used as synonyms for the objects they reference Avoid cost of copying string x = findmax (a); cout << x << endl; const string & x = findmax (a); cout << x << endl; 16

Destructor Default definitions for all classes Destructor Called when object goes out of scope or subject to a delete By default, calls destructor on all data members Might want to delete objects created using new Might want to close any opened files. 17

Copy Constructor Copy constructor Declaration during initialization IntCell B = C; IntCell B (C); Object passed using call by value (instead of by & or const & ) Object returned by value (instead of by & or const & ) Simple assignment for all members with primitive data types (e.g., int, double, ) Calls copy constructors on all member objects 18

operator= Copy assignment operator: operator= Called when objects on both sides of assignment already constructed E.g., IntCell B(0); IntCell C(1); B = C; By default, operator= called on each data member of objects 19

Default destructor, copy constructor and operator= for IntCell 20

Problems with Defaults 21

Problems with Defaults Output? 22

Fixing the Defaults 23

Templates Designing type-independent data structures and algorithms Function templates Class templates 24

Function Templates 25

Function Templates 26

Class Templates Zero may not be a valid Object 27

Class Templates 28

C++ Standard Template Library: Basics SGI s web reference: http://www.sgi.com/tech/stl/ 29

Key concepts Containers Iterators 30

Example Problem Find the maximum in an (i) linked list of integers (ii) array of integers 31

Coding using STL Linked List Array *curr; *curr; Do you see the advantage of using iterators in the above example? 32

List of containers STL container: vector list slist queue stack deque set Data structure that it implements: Array Doubly-linked list Singly-linked list FIFO structure LIFO structure Array-like structure with efficient insertion & removal at both ends Set of unique elements 33

API for the containers Function: push_front pop_front push_back pop_back empty size insert erase clear resize front Back [] at Purpose: Inserts elements before the first (not available for vector) Removes the first element (not available for vector) Appends element at the end Removes element from the end Boolean indicating if the container is empty Returns number of elements Insert an element in a position Removes an element at a position Removes all elements Resizes the container Returns a reference to the first element Returns a reference to the last element Subscripting access without bounds checking Subscripting access with bounds checking 34

Summary Basic C++ Templates Tools for easing the design of typeindependent data structures and algorithms 35