DOC PREVIEW
Duke CPS 108 - Lecture

This preview shows page 1-2-14-15-30-31 out of 31 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 31 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Software Design8.1A Rose by any other name…C or Java?z Why do we use Java in our courses (royal we?)¾ Object oriented¾ Large collection of libraries¾ Safe for advanced programming and beginners¾ Harder to shoot ourselves in the footz Why don't we use C++ (or C)?¾ Standard libraries weak or non-existant (comparatively)¾ Easy to make mistakes when beginning¾ No GUIs, complicated compilation modelSoftware Design8.2Why do we learn other languages?z Perl, Python, PHP, mySQL, C, C++, Java, Scheme, ML, …¾ Can we do something different in one language?• Depends on what different means.• In theory: no; in practice: yes¾ What languages do you know? All of them. ¾ In what languages are you fluent? None of themz In later courses why do we use C or C++?¾ Closer to the machine, we want to understand the machine at many levels, from the abstract to the ridiculous• Or at all levels of hardware and software¾ Some problems are better suited to one langauge• What about writing an operating system? Linux?Software Design8.3C++ on three slidesz Classes are similar to Java, compilation model is different¾ Classes have public and private sections/areas¾ Typically declaration in .h file and implementation in .cpp• Separate interface from actual implementation• Good in theory, hard to get right in practice¾ One .cpp file compiles to one .o file• To create an executable, we link .o files with libraries• Hopefully someone else takes care of the details (Makefile)z We #include rather than import, this is a preprocessing step¾ Literally sucks in an entire header file, can take a while for standard libraries like iostream, string, etc.¾ No abbreviation similar to java.util.*;Software Design8.4C++ on a second slidez We don't have to call new to create objects, they can be created"on the stack"¾ Using new creates memory "on the heap"¾ In C++ we need to do our own garbage collection, or avoid and run out of memory (is this an issue?)z vector similar to ArrayList, pointers are similar to arrays¾ Unfortunately, C/C++ equate array with memory allocation¾ To access via a pointer, we don't use . we use ->z Streams are used for IO, iterators are used to access begin/end of collection¾ ifstream, cout correspond to Readers and System.outSoftware Design8.5How do we read a file? (SearchDemo)TreeSet<String> unique = new TreeSet<String>();int total = 0;while (s.hasNext()){String str = s.next();total++;unique.add(str.toLowerCase());}myWordsAsList = new ArrayList(set);string word;set<string> unique;int total = 0;while (input >> word){transform(word.begin(), word.end(),word.begin(),makelower); // ml NOT standardunique.insert(word);total++;}myWords = vector<string>(unique.begin(), unique.end());Software Design8.6Shafi Goldwasserz RCS professor of computer science at MIT¾ Co-inventor of zero-knowledge proof protocolsHow do you convince someone that you know something without revealing “something”z ACM Grace Murray Hopper award and Godel prize in Theoretical Computer Science (twice)Work on what you like, what feels right, I now of no other way to end up doing creative workSoftware Design8.7Toward an Understanding of C++z Traditional first program, doesn’t convey power of computing but it illustrates basic components of a simple program#include <iostream>using namespace std;// traditional first programint main(){cout << "Hello world" << endl;return 0;}z This program must be edited/typed, compiled, linked and executed.z Other languages don’t use compile/link phase, examples?Software Design8.8What’s a namespace?z In “standard” C++, objects and types are classified as to what namespace they’re in. Hierarchy is good.#include <iostream>// traditional first programint main(){std::cout << "Hello world" << std::endl;return 0;}z It’s much simpler to “use” a namespace, in small programs there won’t be any conflicts (and small is fairly big)Software Design8.9Compiling and linking, differences// string.cpp// stuff we can’t// understand#include <string>int main(){string s = “hi”;}hello.cpp01010101010101…hello.o111000110101010…string.ohelloLinkhello.exeSoftware Design8.10It’s all relative and it dependsI make the best bread in the cityI make the best bread in the worldI make the best bread in the universeI make the best bread on the blockSoftware Design8.11Quadratic Equation Examplevoid Roots(double a, double b, double c,double& root1, double& root2);// post: root1 and root2 set to roots of// quadratic ax^2 + bx + c// values undefined if no roots exist int main(){double a,b,c,r1,r2;cout << "enter coefficients ";cin >> a >> b >> c;Roots(a,b,c,r1,r2);cout << "roots are " << r1 << " " << r2 <<endl;return 0;}Software Design8.12Who supplies memory, where’s copy?void Roots(double a, double b, double c,double& root1, double& root2);// post: root1 and root2 set to roots of// quadratic ax^2 + bx + c// values undefined if no roots exist z For value parameter, the argument value is copied into memory that “belongs” to parameterz For reference parameter, the argument is the memory, the parameter is an alias for argument memorydouble x, y, w, z;Roots(1.0, 5.0, 6.0, x, y);Roots(1.0, w, z, 2.0, x); // no good, why?Software Design8.13Parameter Passing: const-referencez When parameters pass information into a function, but the object passed doesn’t change, it’s ok to pass a copy¾ Pass by value means pass a copy¾ Memory belongs to parameter, argument is copiedz When parameter is altered, information goes out from the function via a parameter, a reference parameter is used¾ No copy is made when passing by reference¾ Memory belongs to argument, parameter is aliasz Sometimes we want to avoid the overhead of making the copy, but we don’t want to allow the argument to be changed (by a malicious function, for example)¾ const-reference parameters avoid copies, but cannot be changed in the functionSoftware Design8.14Count # occurrences of “e”z Look at every character in the string, avoid copying the stringint letterCount(const string& s, const string& letter)// post: return number of occurrences of letter in s{int k, count = 0, len = s.length();for(k=0; k < len; k++) {if (s.substr(k,1) == letter) {count++;}}return count;}z Calls below are legal (but won’t be if just reference parameters)int ec = letterCount("elephant", "e");string s = "hello"; cout << letterCount(s, "a");Software Design8.15General rules for Parametersz Don’t worry


View Full Document

Duke CPS 108 - Lecture

Download Lecture
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Lecture 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?