New User Special Price Expires in

Let's log you in.

Sign in with Facebook


Don't have a StudySoup account? Create one here!


Create a StudySoup account

Be part of our community, it's free to join!

Sign up with Facebook


Create your account
By creating an account you agree to StudySoup's terms and conditions and privacy policy

Already have a StudySoup account? Login here

ECS 40 Spring 2015 Lecture Notes

by: Annalisa Yoakum

ECS 40 Spring 2015 Lecture Notes ECS 40

Marketplace > University of California - Davis > ComputerScienence > ECS 40 > ECS 40 Spring 2015 Lecture Notes
Annalisa Yoakum
GPA 2.728
Software and Object Orient Programming
Sean Davis

Almost Ready


These notes were just uploaded, and will be ready to view shortly.

Purchase these notes here, or revisit this page.

Either way, we'll remind you when they're ready :)

Preview These Notes for FREE

Get a free preview of these Notes, just enter your email below.

Unlock Preview
Unlock Preview

Preview these materials now for free

Why put in your email? Get access to more of this material and other relevant free materials for your school

View Preview

About this Document

Notes for the whole ECS 40 course. Includes notes as well as example code.
Software and Object Orient Programming
Sean Davis
Study Guide
50 ?




Popular in Software and Object Orient Programming

Popular in ComputerScienence

This 26 page Study Guide was uploaded by Annalisa Yoakum on Saturday September 26, 2015. The Study Guide belongs to ECS 40 at University of California - Davis taught by Sean Davis in Spring 2015. Since its upload, it has received 29 views. For similar materials see Software and Object Orient Programming in ComputerScienence at University of California - Davis.


Reviews for ECS 40 Spring 2015 Lecture Notes


Report this Material


What is Karma?


Karma is the currency of StudySoup.

You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!

Date Created: 09/26/15
ECS 4O LECTURE NOTES handin cs40a p1 p2 p3 sh c or any files authorstxt 13 April 1Declare variables anywhere for int c 0 instead of int i then for i 0 C oat x C oatX Reference variables necessity for C int num 12 foo ampnum void fooint n n 18 changes n to 18 int ampref num another name for num foo ampref ref 19 changed num to 19 foo2num void foo2intampn n 26 changed num to 26 void foo3const int ampn 15 April Umask 022 000 010 010 Take the complement opposite of the umask 643 110 100 011 amp 022 111 101 101 641 110100 001 double X y double ampdref x char b deref y Which is X foo char ampcref cref 13 so if you call foo b b is changed CONST strcpy char dest const char source char ar10 OK src3 j NOT OK foo const intamp num can also be fooint constamp num foo2const char letter is OK NAME MANGLING DEFAULT ARGUMENTS C foo int num 12 char s foo 15 Hello OK foo Hi OK the int Will be 12 as DEFAULT DEFINE define MAX 12 OKish const int MAX 12 Better to use 10 printf d s Donen 12 cs C include ltiostreamgt equiva1ent to stdioh for C stdcout ltlt 12 ltlt ltlt cs ltlt Donenquot ltlt is the insertion operator space Do this instead include ltiosstreamgt using namespace std cout ltlt 12 ltlt ltlt cs ltlt Donen ostreamamp ostream operator ltlt int n returns a reference to an ostream 24 April class Person char name int age public Personconst char n int age Person const Personamprhs rhs right hand side Personamp operator const Person amprhs Person destructor if you ve used new in the constructor Example Person Bill Person ObamaBill foo Bill foo Person p Bill fooZ Person fooZ PERSONCPP Bill Mitt for reference V PersonPersonconst char n int age ageag if n name new char strlenn 1 strcpy name n else name NULL OR you can put nameNULL age ag at the beginning of the function PersonPersonconst Person amprhs agerhsage if rhsname name new char strlenrhsage 1 strcpy name rhsname else name NULL PersonPerson if name delete name Personamp Personzzoperator const Person amprhs if this amprhs return this if name delete name age rhsage if rhsname name new charstrlenrhsname 1 strcpyname rhsname else name NULL return this retuming Bill the left hand side only use this here imxyz15 Xyz Ni Ni 27 April COMPOSITION has a EX Vector has an array of city is a EX A dog is an animal Constructors Construct the enclosing class Vector Construct the enclosed class City Destructors Destruct the enclosed class City Destruct the enclosing class Vector char strtok static char last NULL class Person char name int age static int count public static void printCount Person count counting how many people eXist Person count deconstructing the function above PERSONCPP int Person count O refering to the count in the class HAVE to declare it under includes in a cpp file as a global score void Person print cout ltlt count ltlt age this is ok void Person printCount cout ltlt count this is also ok void main Person Obama Obamaprint PersonprintCount T his requires a static function see class IOSTREAM cingetlineslOOO n n can be left off as it is already default cingetslOOO n this one keeps the n in Only difference of the two cingetc Will get any character c cinget can do this cinignorelOOOeof eof end of file Typically eof is replaced With n use atoi to change any character command lines to ints scanning choice for menu for p3 Hello 53 Hi 126 Line these up printf 5s 5llf salutation GPA salutationhi hello and GPA53 PP N 126 are just names of strings IOMAINIP include iomainip cout ltlt setW5 ltlt left ltlt salutation ltlt setW5 ltlt setprecisionl ltlt fixed ltlt right ltlt GPA see from above Examples of more ags scientific scientific notation instead of fixed dec decimal base 10 read in AND out cin and cout hex decimal base 16 cin and cout oct decimal base 8 cin and cout uppercase letters that involve in numbers are changed to uppercase showbase O octal OX hex showpause puts the plus sign in front of positive numbers bool alpha converts bool to words 0 false can be read false and return 0 FSTREAM ifstream filename ofstream filename fstream openfilename ags 29 April FSTREAM ifstream filename iosbinary p4 ofstream filename mode fstream f open filename mode H open planesdat ios modes ios in out app automatically seeks to the end for read ate more generic seeks end for read and write trunc erase file default for out nocreate open the file only if it exists noreplace open the file only if it does not eXist binary open planesdat iosin iosbinary infread char size for binary files char plane size sizeofPlane infwrite char size seekplong ios ag p put g get seepglong ios ag ios ags ios beg end cur use long Wherever you are currently Reading the third plane seekg2 sizeofPlane iosbeg OVERLOADED OPERATORS CANT Redefine operators less than will always be less than sizeof Change the number of operators it can take binary is binary Change the precedence like multiplication operator class Person public bool opearator const Person amprhs const if name ampamp rhsname one is NULL return strcmp name rhsname 0 ampamp age rhsage if name rhsname both NULL return false return rhsage age 1 May MORE OVERLOADED OPERATORS bool Personoperatorltconst Person amprhs const return age lt rhsage Overload the bracket in Vector C1ass class Vector public const City amp operator int index const to return a const reference City amp operator int index City ampVector operatorint index return cityArraysindex MAINCPP Vector cities city cities3 End Examp1e Personamp Personoperator pre increment int post increment age return this p4 example Goal cout ltlt Obama class Person public friend ostreamamp operatorltltostreamamp os const Person amprhs global function ostreamamp operatorltltostreamamp os const Person amprhs dont write friend os ltlt name ltlt ltlt age WRONG os ltlt rhsname ltlt ltlt rhsage ltlt endl RIGHT return os Link list which only grows as needed class List forward declaration class ListNode friend class List int data ListNode next ListNode int d ListNode n datad nextn constructor class List ListNode head public List headNULL 4 May 2015 ListNodeint dat ListNode n data dat nextn Insert at Front List List head NULL void List insert int data head new ListNodedata head Find bool List find int data ListNode ptr head While ptr ampamp ptr gtdata data ptr ptr gtnext return ptr gt NULL Insert Anywhere void List insert int data ListNode ptr preV NULL for ptr head ptr ampamp data gt ptr gtdata ptr ptr gtnext how to progress through a Link List preV ptr if lpreV if preV NULL head new ListNodedata head from Insert at Front else inserting after first node preV gtnext new ListNodedata ptr Remove memorize void List remove int data ListNode ptr preV NULL for ptr head ptr ampamp data gt ptr gtdata ptr ptr gtnext for a sorted list instead of gt for an unsorted list prev ptr if ptr ampamp ptr gtdata data if lpreV head ptr gtnext else removing after rst node preV gtnext ptr gtnext delete ptr Destructor List List Listnode ptr for ptr head ptr ptr head head ptr gtnext delete ptr 6 May 2015 class List ListNode head tail List headNULL tailNULL void insertint data if ltail if empty list head tail new ListNodedata NULL else tail tail gtnext new ListNodedata NULL removeint data ListNode ptr prev NULL for ptr head ptr ampamp ptr gtdata data prt prt gtnext prev ptr if ptr if lprev first node head ptr gtnext if hea NULL tail NULL else after first node DOUBLE LINK LIST NODES ListNode int d ListNode pre ListNode neX memorize void List insertint data ListNode ptr prev NULL for ptr head ptr ampamp ptr gtdata gt data ptr ptr gtnext must be sorted prev ptr if lprev head neWListNodedata NULL head if ptr ptr gtprev head if first node else if later node prev gtnext new ListNodedata prev ptr if ptr ptr gtprev prev gtnext void List removeint data ListNode ptr prev NULL for ptr head ptr ampamp ptr gtdata gt data ptr ptr gtnext must be sorted prev ptr if ptr ampamp ptr gtdata data if lprev front of list head ptr gtnext if ptr gtnext ptr gtnext gtprev NULL else if after first node prev gtnext ptr gtnext if ptr gtnext ptr gtnext gtprev prev lines With l are identical so prev NULL delete ptr if found 8 May 2015 class Person name age as usual refer to other notes public void print cout ltlt name void printAge cout ltlt age Person const char n int a deleted in lecture Person protected char name int age class Student public Person public applies to global functions and classes derived from Student If public was changed to protected then all of the public in the class Person would become protected double GPA public Student const char n int a double G void print cout ltlt GPA void print2 printAge for protected Person from above this is how to print the age of the student not using SanPerson print since it wouldn t be available to main since it would be protected class CollegeStudentzprivate Student private means you can t call any functions from Student or Person If private was protected then Student and Person become protected char major public CollegeStudent const char n int a double G char maj standard constructor print cout ltlt major STUDENTCPP StudentStudent const char n int a double G Person n a GPA G Person na deleted in lecture name new char strlenn 1 main CollegeStudent Lynn Lynn 18 38 CS Person Obama Obama 62 Student San San 19 35 Obamaprint Will print Obama Sanprint Will print 35 SanprintAge Will print 19 SanPerson print Will print San calling the print method of the Person class Lynnprint Will print CS from the CollegeStudent class 11 May 2015 P5 copy constructor example PersonPerson const Person amprhs nameNULL agerhsage in City set name and state to NULL Copy the lat long and population if rhsage name new charstrlenrhsname 1 strcpyname rhsname would do this for both state and name NO Return this POLYMORPHISM Late binding does not know which function to call until RUN TIME not compile time class Person public Virtual print cout ltlt name Virtual print O This is PURE Virtual Will not supply the code for implementation Makes Person an abstract class You can t have an object of that type If class Student doesn t have a print method then you can t create a Student If there is a print in CollegeStudent then you can make a Student class Student public Person public print cout ltlt GPA printGPA cout ltlt GPA class CollegeStudent public Student printGPA cout ltlt major main Person Mitt Mitt 64 ptr Student Daisy Daisy 20 32 sptr CollegeStudent Liem Liem 19 30 CS sptr ampMitt N OT ALLOWED because you re trying to access something that isn t there sptr gtprintGPA NOT ALLOWED sptr ampLiem sptr gtprintGPA Will print out 30 because its not a Virtual Will call at compile time ptr ampMitt ptr gtprint Will print Mitt ptr ampDaisy ptr gtprint Will print 32 THIS IS POLY ptr ampLiem ptr gtprint Will print 30 So if VIRTUAL Will wait until run time and the Virtual table If not Will call at compile time No Virtual friends as well V this is how to get around it Friends can call Virtual functions class Person public Virtual void writeostream ampos friend ostreamamp operator ltlt ostreamamp os const Person amprhs ostreamamp operator ltlt ostreamamp os const Person amprhs rhswriteos TEMPLATES A template class has parameterized types class ltTypename Tgt List ltTgt insert T ampdata ListNode ltTgt ptr preV NULL new ListNode ltTgt data ptr main List ltintgt list List ltcitygt cityList List ltPersongt peopleList Different types of calls Take the word inside lt gt and replaces T With it It creates a separate class for each call LISTH class ltTypename Tgt class ListNode T data ListNode ltTgt next ListNode T ampd ListNode ltTgt n datad nextn 13 May 2015 template lttypename Tgt class Vector int count int size T array public Vector Tamp operator int index Vector ltcitygt cities example for p6 V in this case T cities VECTORCPP template lttypename Tgt Vector ltTgtVector count 0 size 10 array new T 10 template lttypename Tgt ABOVE EVERY FUNCTION Tamp VectorltTgt operator int index return arrayindeX Tricks to Templates DONT explicitly compile template classes so don t g vectorcpp in this case DO add include Vectorcpp to the bottom of vectorh above the endif DO add vectorcpp to the dependency lines of the Makefile so With vectorh is vectorcpp RECURSION Need a base case and then the recursive call The task MUST get smaller or else you ll be in an infinite loop 18 May 2015 Problems With Code Redefinition Makefile g listcpp Undefined Reference Makefile left out an 0 file from linker line include listcpp before endif header prototype doesn t match cpp signature p6 template class Airport public City List ltFlightgt ights STANDARD TEMPLATE LIBRARY Containers Sequence vector list deque Associative set multiset map multi map Containter Adapter stack queue priority queue Iterators use begin end beyond last element rbegin reverse iterators rend reverse iterators string s Hello for const interator itr sbegin itr send itr cout ltlt itr ltlt endl this Will print H e l l 0 on separate lines for constreverseiterator ritr srbegin ritr srend ritr cout ltlt ritr ltlt endl this Will print 0 l l e H on separate lines Containers ALWAYS include A default constructor A copy constructor A destructor A bool empty which has maXsize and size The can use lt lt gt gt swap or end V one past begin end rbegin rend erase iterator erase begin itr end itr clear 20 May 2015 SEQUENCE Vector ltvectorgt pushback capacity resize Vector ltF1ightgt ights20 list lt1istgt pushfront popfront remove merge reverse sort pushback popback Deque Double ended queue pushfront popfront pushback popback Queue FIFO First In First Out CONTAINERS Associative Balanced Binary Search Tree 13 52 46 73 69 AVL Tree Red Black Tree Associative Containers include Everything that containers have Bidirectional Irerators iterator find lowerboundX returns smallest object greater or equal to X upperboundy return smallest object greater than y int countz set ltintgt s sinsert5 for constiterator itr slowerbound3 itr supperbound27 itr cout ltlt itr will print out numbers in order from 3 to 27 27 May 2015 MAPS Try scrabble for p8 2 include map typedef multimap ltstring intgt ZipMap main ZipMap zipmap zipmapinsert ZipMapValuetype DaVis 95616 zipmapinsert ZipMapValuetype DaVis 95617 for ZipMapzziterator itr zipmaplowerbound DaVis itr zipmapupperbound DaVis itr cout ltlt itr gtsecond ltlt endl 29 May 2015 ALGORITHMS include algorithm fill itrbegin itrend value bool equal itrbegin itrend itr2begin operator removeitrbegin itrend valuetoberemoved replace itrbegin itrend oldvalue newvalue randomshuf eitrbegin itrend int count itrbegin itrend value iter minelementitrbegin itrend iter maXelementitrbegin itrend accumulate itrbegin itrend accumulate function accumulatevalue functionpreviousaccumulation current element foreach itrbegin itrend function finditrbegin itrend value needs operator sort itrbegin itrend comparator function bool binary searchitrbegin itrend value comparator function so ifaltbbutbis notlta thena STACKS AND QUEUES Stack First In Last Out FILO Queues First In First Out FIFO 1 June 2015 Final Example void Stack pushint data if head NULL check for empty list head neWListNodedata head void pop ListNode ptr head head head gtnext delete ptr int top check for empty list so if head NULL return head gtdata push3 push7 topO push4 pom pom push6 3 37 37 347 34 3 6


Buy Material

Are you sure you want to buy this material for

50 Karma

Buy Material

BOOM! Enjoy Your Free Notes!

We've added these Notes to your profile, click here to view them now.


You're already Subscribed!

Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'

Why people love StudySoup

Jim McGreen Ohio University

"Knowing I can count on the Elite Notetaker in my class allows me to focus on what the professor is saying instead of just scribbling notes the whole time and falling behind."

Kyle Maynard Purdue

"When you're taking detailed notes and trying to help everyone else out in the class, it really helps you learn and understand the I made $280 on my first study guide!"

Bentley McCaw University of Florida

"I was shooting for a perfect 4.0 GPA this semester. Having StudySoup as a study aid was critical to helping me achieve my goal...and I nailed it!"


"Their 'Elite Notetakers' are making over $1,200/month in sales by creating high quality content that helps their classmates in a time of need."

Become an Elite Notetaker and start selling your notes online!

Refund Policy


All subscriptions to StudySoup are paid in full at the time of subscribing. To change your credit card information or to cancel your subscription, go to "Edit Settings". All credit card information will be available there. If you should decide to cancel your subscription, it will continue to be valid until the next payment period, as all payments for the current period were made in advance. For special circumstances, please email


StudySoup has more than 1 million course-specific study resources to help students study smarter. If you’re having trouble finding what you’re looking for, our customer support team can help you find what you need! Feel free to contact them here:

Recurring Subscriptions: If you have canceled your recurring subscription on the day of renewal and have not downloaded any documents, you may request a refund by submitting an email to

Satisfaction Guarantee: If you’re not satisfied with your subscription, you can contact us for further help. Contact must be made within 3 business days of your subscription purchase and your refund request will be subject for review.

Please Note: Refunds can never be provided more than 30 days after the initial purchase date regardless of your activity on the site.