CMSC162 Intro to Algorithmic Design II Blaheta. Lab March 2019

Similar documents
Lab 6 Vectors and functions

Lab 12: Lijnenspel revisited

Lab 7 Unit testing and debugging

Lab 12 Lijnenspel revisited

Lab 9: Pointers and arrays

Starting to Program in C++ (Basics & I/O)

Operator overloading

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Lab 2: ADT Design & Implementation

Absolute C++ Walter Savitch

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

Ch. 12: Operator Overloading

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

COSC 2P95. Procedural Abstraction. Week 3. Brock University. Brock University (Week 3) Procedural Abstraction 1 / 26

CSE 303: Concepts and Tools for Software Development

Lecture 14: more class, C++ streams

And Even More and More C++ Fundamentals of Computer Science

Project 1 Balanced binary

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

QUIZ Friends class Y;

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

Ch. 11: References & the Copy-Constructor. - continued -

CS201 Some Important Definitions

Lesson 13 - Vectors Dynamic Data Storage

Fall 2017 CISC/CMPE320 9/27/2017

PIC 10A Objects/Classes

CISC 2200 Data Structure Fall, C++ Review:3/3. 1 From last lecture:

CSE 333 Midterm Exam July 24, Name UW ID#

Lab 10: Sockets 12:00 PM, Apr 4, 2018

QUICK EXCEL TUTORIAL. The Very Basics

1) Log on to the computer using your PU net ID and password.

CS354 gdb Tutorial Written by Chris Feilbach

G52CPP C++ Programming Lecture 17

Today in CS162. External Files. What is an external file? How do we save data in a file? CS162 External Data Files 1

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.

Chapter Two Bonus Lesson: JavaDoc

A student was asked to point out interface elements in this code: Answer: cout. What is wrong?

CSE 333. Lecture 11 - constructor insanity. Hal Perkins Paul G. Allen School of Computer Science & Engineering University of Washington

Intro. Scheme Basics. scm> 5 5. scm>

QUIZ on Ch.5. Why is it sometimes not a good idea to place the private part of the interface in a header file?

Getting Started. Excerpted from Hello World! Computer Programming for Kids and Other Beginners

AIMS Embedded Systems Programming MT 2017

Lecture 13: more class, C++ memory management

Creating a new form with check boxes, drop-down list boxes, and text box fill-ins. Customizing each of the three form fields.

KOM3191 Object Oriented Programming Dr Muharrem Mercimek OPERATOR OVERLOADING. KOM3191 Object-Oriented Programming

Week 8: Operator overloading

Recap: Pointers. int* int& *p &i &*&* ** * * * * IFMP 18, M. Schwerhoff

Software Design and Analysis for Engineers

Introduction to Excel

COP Programming Assignment #7

Google Drive: Access and organize your files

Understanding main() function Input/Output Streams

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

CpSc 111 Lab 5 Conditional Statements, Loops, the Math Library, and Redirecting Input

y

C++ Style Guide. 1.0 General. 2.0 Visual Layout. 3.0 Indentation and Whitespace

Lab 4. Out: Friday, February 25th, 2005

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

Computer Science II CSci 1200 Lecture 18 Operators and Friends

C++ Input/Output: Streams

Hello World! Computer Programming for Kids and Other Beginners. Chapter 1. by Warren Sande and Carter Sande. Copyright 2009 Manning Publications


Better variadic functions in C

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Lab 2 Building on Linux

Lecture 12 CSE July Today we ll cover the things that you still don t know that you need to know in order to do the assignment.

2SKILL. Variables Lesson 6. Remembering numbers (and other stuff)...

UNIVERSITY OF NEBRASKA AT OMAHA Computer Science 4500/8506 Operating Systems Fall Programming Assignment 1 (updated 9/16/2017)

Lambda Correctness and Usability Issues

Practicum 5 Maps and Closures

CS 103 Lab - Party Like A Char Star

And Even More and More C++ Fundamentals of Computer Science

Constructors for classes

Vector and Free Store (Vectors and Arrays)

QUIZ. How could we disable the automatic creation of copyconstructors

The Dynamic Typing Interlude

CpSc 1011 Lab 5 Conditional Statements, Loops, ASCII code, and Redirecting Input Characters and Hurricanes

Dynamic memory in class Ch 9, 11.4, 13.1 & Appendix F

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:

A brief introduction to C++

Physics 6720 I/O Methods October 30, C++ and Unix I/O Streams

CMSC445 Compiler design Blaheta. Project 2: Lexer. Due: 15 February 2012

1 Getting started with Processing

Object-Oriented Programming

Lab: Supplying Inputs to Programs

Pointers in C/C++ 1 Memory Addresses 2

These are notes for the third lecture; if statements and loops.

More About WHILE Loops

Mastering Data Abstraction or Get nit-picky on design advantages

CISC220 Lab 2: Due Wed, Sep 26 at Midnight (110 pts)

Assignment 1c: Compiler organization and backend programming

GDB Tutorial. A Walkthrough with Examples. CMSC Spring Last modified March 22, GDB Tutorial

CS 1110, LAB 3: MODULES AND TESTING First Name: Last Name: NetID:

Problem Solving with C++

How to approach a computational problem

EL2310 Scientific Programming

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

CS24 Week 3 Lecture 1

CPSC 427: Object-Oriented Programming

Transcription:

CMSC162 Intro to Algorithmic Design II Blaheta Lab 10 28 March 2019 This week we ll take a brief break from the Set library and revisit a class we saw way back in Lab 4: Card, representing playing cards. At the time, we were more focused on learning how pointers work, but today we ll use the class to practice operator overloading and some related ideas. To start, copy the files from /home/shared/162/lab10/ into your working directory for this lab. Look at the files; they are similar to the ones from Lab 4 but I ve updated a few things. Start writing a readme file and describe what s in the directory according to our usual readme format. Read each file and write down questions about anything you re not sure about. Vim FOTD: windows Go into your directory (which you ve already copied the lab files into) and type vim -o Card.h Card.cpp Instead of opening all the files sequentially (as would happen if you omitted the -o, it opens them all at once! If you use :q (which, you ll recall, closes a file without writing it), it just closes one of the windows. (:wq also affects only the current window.) To add an extra window, another colon-mode command is handy: :new test_card.u will open a new window showing test Card.u. You can also try :e Makefile Notice that :new opens a file in a new window, while :e opens a file in the current window.) And, of course, when we say window here, it is only with respect to the single vim process. All of these windows are within a single terminal

window. There are two main advantages to using vim windows instead of separate terminal windows to edit multiple files: first, all the switching back and forth can be easily done from the keyboard. And second, they share buffers, so you can hit 10dd in one window, deleting ten lines, and p in another, pasting precisely those ten lines with no funny whitespace or line numbers or tab conversion or anything else. Vim s window management commands start with Ctrl-W (abbreviated W h W j W k W l W w W s W v W r Left one window Down one window Up one window Right one window Cycle cursor through windows one-by-one Split current window into two windows (horizontally) Split current window vertically into two windows Rotate windows (actually changing their positions) To quit all windows at once, use :qa. (If you ve edited some of them, you may need to use :wqa or :qa!.) W): Defining == As you saw when you read chapter CI6, 1 and mentioned in class, in C++ you can write method definitions that let you use builtin operators like == or < with classes that you write. Go into Card and add a method to overload == to test whether two Card objects are equal to each other. Conveniently (and not coincidentally), there is already an isequalto method to actually do the work for you. To declare the overloaded operator, you ll declare a method whose name is operator==, following the form on p436. When the method is defined, the body can just call isequalto to do the work. Don t forget to add a test case to test Card to confirm that this works; note that unlike the name of the method (which will be operator==), the name of the test block has to be an identifier, so you ll start it with something like test opeqeq 1 Which I m not fully recapping here. 2

So what? We already had isequalto, so what benefit is there in overloading ==? The convenience of writing something like if (card1 == card2) is nice, of course, but the big win is that a lot of library functions are designed to use an overloaded == to do their work. Look inside the file show library stuff.cpp now. Uncomment the first commented-out part (now that you ve implemented ==). Save the file, run make, and then run./show library stuff to see what it does it makes use of builtin library functions to do its work, and they make use of the == operator to do their work. In many ways, implementing certain operators lets you unlock a whole section of the C++ library. You ll notice that all the library functions I ve used in there take as their first two arguments cards.begin() and cards.end(). This is one of several uses of the iterators that we started to see this week in this case, a way to tell the library algorithms to process the whole vector, from beginning to end. Defining << The C++ streaming (input/output) operators provide a nifty way for writers of classes to make their objects read-able and write-able since << and are just operators, you can overload them too! When you write something like cout << 5; the thing on the left is an ostream, and the thing on the right is an int, so C++ looks for and finds a definition for operator<< that takes an ostream& and an int. Deep in the libraries, there is a function defined whose header is ostream& operator<< (ostream& out, int n) to do this work. Since that also returns an ostream&, it means that when you write 3

cout << 6 << 7; it performs the first operation (cout << 6), which results in an updated ostream that can serve as the left side of the next << operator. In the past, we ve made print methods that take an ostream& to do their work; when we make an << operator, the body of that method will be much like what we ve written before; it s really just the headers that will be different. 2 To make it possible to output your Card using streams, you need to do two extra things (that are both specific to the stream operators): 1. In Card.h, in the public part of the Card class, declare a function called operator<< that takes an ostream& and a const Card&, and returns an ostream&, and precede this declaration with the word friend so C++ knows that it is a friend function and not a member it s not part of the Card class but it can look at the private contents of a Card and 2. In Card.cpp, write the function. The header will be the same except for the word friend. (Notably, it will not include Card:: anywhere in the line!) Do not print anything directly to cout here use the ostream parameter instead. To test this, you can write a test case that creates an ostringstream and prints some cards to it, and then verifies that the resulting string is what you expected: ostringstream testout; testout << queenh << " " << jackc; check (testout.str()) expect == "QH JC"; This is very similar to how the print method of Maze was tested in the code I gave you for Lab 7. So what? The benefits of this one might be a little clearer once you define a stream operator for a class, you can put objects of that class into the stream just 2 Again, I won t recap the book here; refer to Section C6.2 for more details. 4

as if they were built-in types. Once you ve got it implemented, uncomment the second part of the demo file. Now that we can conveniently print out Card values, you can see the effects of a few other library functions that got unlocked when you implemented ==. Side note: once == and << are both defined on a type, you can use values of that type directly as expect targets in a.u file. Remember wanting to write something like check ( Location{3,4}.east() ) expect == Location{4,4}; in the maze project? If you were to overload == and << for the Location class, that line would work exactly as you d want. Defining = and the Rule of Three For a lot of classes, including Card, you do not need to explicitly overload the = operator; if you don t, the compiler will auto-define one that works well in many cases (a fact that we ve relied on any time we assigned a Location, Card, or Maze to another variable!). What does it mean to overload =? As the assignment operator, it changes the contents of this object to match those of a given other object similar to a constructor that takes a (const reference to) a given other object, known as a copy constructor. You really only need to define either one when your class has to manage extra resources (such as new pointers, or open files, or similar) and the Rule of Three says if you think you need to define either one, you should be defining both of them plus a destructor (which takes care of cleaning up the resources when the object goes away). The Card class doesn t need any of that. reference. But file this away for future Other overloads Overload < (less-than comparison). You should be sure to make aces high, that is, an ace is not less than any other card (but all non-aces are less than aces). You can ignore suit for this comparison. Once you ve got it done, 5

you can uncomment the last part of the library demo file you ve unlocked the builtin sort function! Use the same technique you used on << to overload as well (this time to read from an istream&). Handin Hand in by 4pm Wednesday, as lab10. Rubric (tentative) RUBRIC 1 Present in lab with preview stuff done Defining == 1 Header in class definition 1 Body defined correctly 1 Tested in.u file (fail ok) Defining << 1 / 2 Header correct, friend 1 Body defined correctly 1 / 2 Tested in.u file (fail ok) Other definitions 1 / 2 < header 1 < well tested including ace logic (fail ok) 1 < defined with correct ace logic 1 1 / 2 tested (fail ok) indicates point is only available if the code compiles, with at least a stub for the relevant method(s). Extras Go back and revise your Maze and Location classes to make use of == (for Location) and the stream operators (for Maze and Location), and clean up your test files. 6