Programming Abstractions Cynthia Lee CS106B. Today’s Topics Introducing C++ from the Java Programmer’s Perspective  C++ strings and streams, continued.

Slides:



Advertisements
Similar presentations
Hope Is the Thing with Feathers
Advertisements

Dialogue 2 “The Penguin and the Flamingo”. “Hope”-Emily Dickenson Narrator 4: Hope is the thing with feathers that perches in the soul, and sings the.
“Hope” is the thing with feathers -
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Access to Names Namespaces, Scopes, Access privileges.
1 Introduction to Computers and Programming Quick Review What is a Function? A module of code that performs a specific job.
CS-341 Dick Steflik Introduction. C++ General purpose programming language A superset of C (except for minor details) provides new flexible ways for defining.
28-Jun-15 Access to Names Namespaces, Scopes, Access privileges.
Rhyme Scheme and Stanzas Ms. Macemore Unit Two: American Romanticism.
Emily Dickinson By: Kadie Mullinax. Hope is the Thing with Feathers “Hope” is the thing with feathers - That perches in the soul - And sings the tune.
Unit 2: Java Introduction to Programming 2.1 Initial Example.
Word Choice Spice up your poetry!. WORD CHOICE Recap: USE LANGUAGE THAT IS NATURAL AND NOT OVERDONE AVOID REPETITION USE WORDS CORRECTLY USE POWERFUL.
Intro to Generic Programming Templates and Vectors.
Hello AP Computer Science!. What are some of the things that you have used computers for?
Please complete your paper with the information from the following slides!
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 13: An Introduction to C++
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
Variable Scope Storage Class Recursion
Templates An introduction. Simple Template Functions template T max(T x, T y) { if (x > y) { return x; } else { return y; } } int main(void) { int x =
 A figure of speech in which an expression is used to refer to something that it does not literally denote in order to suggest a similarity without using.
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Functions.
CPS120: Introduction to Computer Science Lecture 14 Functions.
Gets a Makeover. Time to Envision your Name in Lights…
Programming Abstractions Cynthia Lee CS106X. Today’s Topics Introducing C++ from the Java Programmer’s Perspective  Absolute value example, continued.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
1 CS161 Introduction to Computer Science Topic #9.
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
Hello Computer Science!. Below is an example of a Hello World program in JAVA. While it is only three lines of code, there are many things that are happening.
CS 106X – Programming Abstractions in C++ Cynthia Bailey Lee CS2 in C++ Peer Instruction Materials by Cynthia Bailey Lee is licensed under a Creative Commons.
EMILY DICKINSON Enlightened and modern; yet a recluse.
CSE 1341 Honors Note Set 2 1. Overview  Java vs. C++  Functions in C++  First Programming Packet  Development Environment 2.
“Hope” is the thing with feathers
CS 31 Discussion, Week 5 Faisal Alquaddoomi, Office Hours: BH 2432, MW 4:30-6:30pm, F 12:00-1:00pm (today)
“Hope is the thing with feathers”
How to write a literary essay
Poetry Project Elicia Bilyeu.
Extended Metaphor Definition: A metaphor that continues over multiple sentences, and that is sometimes extended throughout an entire work. Why Writers.
Figurative Language Authors use figurative language in their writing to create a picture in the reader’s mind.
December 10, May 15, 1886 EMILY DICKINSON. About Emily  1874 her father died  Being an already reclusive person, she became even more withdrawn.
Chapter 16 Templates Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
C++ Functions A bit of review (things we’ve covered so far)
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
Programming Abstractions
Not your ordinary figurative language
Programming Abstractions
Semester 1 2nd 9 Weeks Week 1 Daily Warm Ups.
By: Sydney Carton and Charles Darnay
Programming Abstractions
Programming Abstractions
Programming Abstractions
By: Sydney Carton and Charles Darnay
Programming Abstractions
Wednesdays words September 20, 2017.
Programming Abstractions
Definitions gale = (n.) strong wind storm
Programming Abstractions
Edit-Me Monday No warm up.
A First Program.
Hope Is the Thing with Feathers
Emily Dickinson: The Belle of Amherst
“Hope is the thing with feathers”
Please Pick Up Your Journal
Templates An introduction.
Wednesdays words September 20, 2017.
Presentation transcript:

Programming Abstractions Cynthia Lee CS106B

Today’s Topics Introducing C++ from the Java Programmer’s Perspective  C++ strings and streams, continued  Reference parameters ADTs: Abstract Data Types  Introduction: What are ADTs?  Vector data structure  Grid data structure › Passing objects by reference const reference parameters › Loop over “neighbors” in a grid

Warm-Up (CS106A review): Parameters int main(){ int n = -5; absoluteValue(n); cout << "|-5| = " << n << endl; return 0; } void absoluteValue(int n) { if (n<0){ n = -n; } 3 What is printed? A. |-5| = 5 B. |-5| = -5 C. Other/none/more than one of the above

"Pass by reference" int main(){ int n = -5; absoluteValue(n); cout << "|-5| = " << n << endl; return 0; } void absoluteValue(int& n) { if (n<0){ n = -n; } 4 What is printed? A. |-5| = 5 B. |-5| = -5 C. Other/none/more than one of the above

"Pass by value" (default behavior of parameters) int main(){ int n = -5; absoluteValue(n); cout << "|-5| = " << n << endl; return 0; } void absoluteValue(int n) { if (n<0){ n = -n; } 5 What is printed? A. |-5| = 5 B. |-5| = -5 C. Other/none/more than one of the above

Often used when you would want to “return” several values from a function (but there is only one return value allowed) #include "random.h" void pickLotto(int& first, int& second, int& third) { first = randomInteger(0,10); second = randomInteger(0,10); third = randomInteger(0,10); } int main(){ int lotto1 = 0; int lotto2 = 0; int lotto3 = 0; pickLotto(lotto1, lotto2, lotto3); cout << lotto1 << " " << lotto2 << " " << lotto3 << endl; return 0; } 6

Pass by reference benefits of reference parameters:  a useful way to be able to in effect 'return' more than one value  often used with large structures and objects, to avoid making bulky copies when passing (more on this in a minute) downsides of reference parameters:  hard to tell from call whether it is ref; can't tell if it will be changed › foo(a, b, c); // will foo change a, b, or c? don’t know.   can't pass a literal value to a ref parameter › grow(39); // error

ADTs Vector, Grid

ADTs  Programming language independent models of common containers  They encompass not only the nature of the data, but ways of accessing it  They form a rich vocabulary of nouns and verbs, often drawing on analogies to make their use intuitive, and to give code written in them a certain literary quality

"Hope" is the thing with feathers BY EMILY DICKENSON “Hope” is the thing with feathers - That perches in the soul - And sings the tune without the words - And never stops - at all - And sweetest - in the Gale - is heard - And sore must be the storm - That could abash the little Bird That kept so many warm - I’ve heard it in the chillest land - And on the strangest Sea - Yet - never - in Extremity, It asked a crumb - of me. Once we say HOPE = BIRD… …it now makes sense to say HOPE PERCHES …or HOPE SINGS We could have described those aspects of HOPE without the analogy, but casting our abstract idea of HOPE as a well-understood thing gave us access to similarly well- understood and evocative verbs, helping the reader more readily grasp our meaning. The same thing happens in code with ADTs.

Vector  ADT abstraction similar to an array  Many languages have a version of this › Java ArrayList › (remember, ADTs are conceptual abstractions that are language- independent)  In C++ we declare one like this:  This syntax is called template syntax › Vectors can hold many things, but they all have to be the same type › The type goes in the after the class name Vector Vector assignment3Scores; Vector measurementsData; Vector > allAssignmentScores; Vector lines;

Grid ADT Code example

Handy loop idiom: iterating over “neighbors” in a Grid static void neighbors(Grid & board, int row, int col) { for (int drow = -1; drow <= 1; drow++) { for (int dcol = -1; dcol <= 1; dcol++) { // do something with board[row + drow][col + dcol] } These nested for loops generate all the pairs in the cross product {-1,0,1} x {-1,0,1}, and we can add these as offsets to a (row,col) coordinate to generate all the neighbors (note: often want to test for and exclude the (0,0) offset, which is “self” not a neighbor) R -1 C -1 R -1 C +0 R -1 C +1 R +0 C -1 R +0 C +0 R +0 C +1 R +1 C -1 R +1 C +0 R +1 C +1

CS106 Honor Code Overview (Please read full document in detail.)

QT Creator A few warnings & tips

If your code doesn’t compile and gives an error about unknown function Main() (note capital M):  There is more than one reason this could arise, but the most common by far is that QT lost its ability to find the path to our Stanford library includes (which define a special Main()), because you put your code in a directory/folder that is deeply nested on your drive.  For example, C:\Documents and Settings\Documents\classes\Stanf ord\2015\Summer\CS106B\assign ments\C++\Assignment1\...  You need to move the assignment directory closer to C:\ (or on mac, the root directory)

If your code doesn’t compile and gives you “multiple definition” errors for all the functions you have:  This can arise if you add new files to your code inside QT creator using its handy “Add New…” feature.  NOT HANDY! Do not use this feature. It breaks the.pro file.

If it’s too late and you already used the “Add New” feature 1.QUIT: Close QT Creator 2.CLEAN UP BROKEN FILES: Delete the.pro and.pro.user files in your code directory; delete the entire build directory (this is an automatically created directory that appears alongside the directory where your code is, it’s name is something like “build-simple-project- Desktop_Qt_5_2_0_MinGW_32bit-Debug”) 3.GRAB REPLACEMENT: Download/unzip the assignment bundle again, and get a fresh copy of the project file (the one that ends in.pro) and put it in place of the one you deleted (you don’t need anything else in the new bundle). 4.ALL BETTER! Re-open QT Creator (it will make a new.pro.user)