7 Pages

lecIV.1-notes

Course: CIS 15, Fall 2009
School: CUNY Baruch
Rating:
 
 
 
 
 

Word Count: 1766

Document Preview

advanced cis15 programming techniques, using c++ fall 2007 lecture # IV.1 topics: inheritance composition of classes resources: Pohl, chapters 8 and 11 an example consider the program robot.cpp (posted on the class web page) this program models a world in which there is a robot and some spots of dirt the robot wanders around looking for spots of dirt and vacuuming them up this is kind of like the...

Register Now

Unformatted Document Excerpt

Coursehero >> New York >> CUNY Baruch >> CIS 15

Course Hero has millions of student submitted documents similar to the one
below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.

Course Hero has millions of student submitted documents similar to the one below including study guides, practice problems, reference materials, practice exams, textbook help and tutor support.
advanced cis15 programming techniques, using c++ fall 2007 lecture # IV.1 topics: inheritance composition of classes resources: Pohl, chapters 8 and 11 an example consider the program robot.cpp (posted on the class web page) this program models a world in which there is a robot and some spots of dirt the robot wanders around looking for spots of dirt and vacuuming them up this is kind of like the assignment from unit II, where you had a robot running around looking for coins; but as we go through this example, you'll see where the code becomes more sophisticated and more object-oriented the class definition for the robot class is as follows cis15-fall2008-azhar-lecIV.1 1 cis15-fall2008-azhar-lecIV.1 2 class robot { private: point location; int num_vacuumed; public: robot() { num_vacuumed = 0; } int getX() const; int getY() const; void set( int x, int y ); void print() const; void move(); void move( direction d ); void eat(); bool hungry(); }; composition the robot class includes a member of the point class, which we have used before (many times!) we say that the robot class is related to the point class by composition composition means that one class contains a data member that is an instance of another class, i.e., a data member that is a variable whose data type is another class another example of composition in robot.cpp is that the class world contains both robot and dirt instances cis15-fall2008-azhar-lecIV.1 3 cis15-fall2008-azhar-lecIV.1 4 privacy note that several of the function members (methods) of robot look like those for point getX() getY() set( int x, int y ) print() these function members provide a way to access the values of the data members of the instance of point, which is a data member of robot since the data members (x and y) are private, we cannot access them directly in robot -- we have to refer to them indirectly by using the public function members of point overloading the rest of the methods in the robot class give us the functionality we want from robot, allowing it to move, to vacuum up spots of dirt and to report whether it is busy (i.e., if there are still spots of dirt in the world for it to vacuum) move() move( direction d ) vacuum() busy() note that we have two versions of the move() function: one that takes no arguments and one that takes one argument creating two versions of the same function, distinguished by their different argument lists, is called overloading we used overloading when talking earlier in the term about different kinds of constructors cis15-fall2008-azhar-lecIV.1 5 cis15-fall2008-azhar-lecIV.1 6 void robot::move(){ direction d; d = static_cast<direction>( rand() % 4 ); move( d ); } // overloaded function void robot::move( direction d ){ int x = location.getX(); int y = location.getY(); switch( d ) { case north: y = (y + 1) % WORLD_SIZE; break; case south: y = (y - 1); if ( y < 0 ) y = WORLD_SIZE; break; case east: x = (x + 1) % WORLD_SIZE; break; case west: x = (x - 1) % WORLD_SIZE; if ( x < 0 ) x = WORLD_SIZE; break; } location.set( x, y ); } cis15-fall2008-azhar-lecIV.1 7 extending classes since we are inherently lazy, we can create a class that is an extension of another class--instead of creating a whole new class (like dirt or robot) that contains all the functionality of one class (like point) and then adds functionality to it the class being extended is called the super (or parent) class, and the resulting classes that are derived from extending the parent are called subclasses or children classes look at robot2.cpp, which is a modified version of robot.cpp in which the dirt and robot classes are redefined as subclasses of the point class cis15-fall2008-azhar-lecIV.1 8 class dirt : public point { private: bool gone; public: dirt() { gone = false; } void disappear(); }; void dirt::disappear(){ cout << "poof!" << endl; gone = true; } class robot : public point { private: int num_vacuumed; public: robot() { num_vacuumed = 0; } void move(); void move( direction d ); void vacuum(); bool busy(); }; void robot::move(){ direction d; d = static_cast<direction>( rand() % 4 ); move( d ); } // overloaded function void robot::move( direction d ){ int x = getX(); int y = getY(); switch( d ) { case north: y = (y + 1) % WORLD_SIZE; break; case south: y = (y - 1); if ( y < 0 ) y = WORLD_SIZE; break; case east: x = (x + 1) % WORLD_SIZE; break; case west: x = (x - 1) % WORLD_SIZE; if ( x < 0 ) x = WORLD_SIZE; break; } set( x, y ); } 9 cis15-fall2008-azhar-lecIV.1 10 cis15-fall2008-azhar-lecIV.1 void robot::vacuum(){ cout << "shrooooop..."; num_vacuumed++; } // the robot is busy until all the dirt has been found and vacuumed bool robot::busy() { if ( num_vacuumed < NUM_SPOTS ){ return true; } else { return false; } } inheritance the relationship between the classes is illustrated by: point robot dirt that is the class robot and the class dirt are both subclasses of the class point put another way, every instance of a robot is an instance of point and every instance of dirt is an instance of point an instance of a subclass inherits all the members of its parent class which means that dirt and robot inherit x, y, getX(), getY(), set() and print() from point note that x and y are private, which means that they cannot even be accessed directly by the subclasses of point (later we'll explain how they could be) cis15-fall2008-azhar-lecIV.1 11 cis15-fall2008-azhar-lecIV.1 12 normally we want to do more than a have subclass just be a copy of the superclass--like we do with dirt and robot what we often want to do is to have the subclass add things to the superclass (In Java this is explicit. When we define a subclass it is by saying it extends the superclass). in our example robot2, the classes dirt and robot are examples of this here dirt is extended with: a private data member gone, which records whether the dirt instance has been vacuumed up yet a public function member disappear that sets the gone flag to true when a dirt instance has been vacuumed up thus dirt has all of the data members of point as well as the additional ones listed here as a result we can do this: dirt spot; spot.set( 2, 3 ); which calls the set method on the dirt object named spot dirt inherits the set method from point cis15-fall2008-azhar-lecIV.1 13 cis15-fall2008-azhar-lecIV.1 14 overriding and inheritance a subclass definition can re-define a function member defined in the superclass this is called overriding (don't confuse it with overloading !) we can, for example, override the definition of move in robot the program robot3.cpp has: class creature : public point { public: void move(); void move(direction d); }; which has two subclasses: class robot : public creature { private: int num_vacuumed; cis15-fall2008-azhar-lecIV.1 15 public: robot() { num_vacuumed = 0; } void vacuum(); bool busy( int num_spots ); }; and class cat : public creature { public: void move(); void move( direction d ); }; the robot class uses the default move() functions, but the cat class overrides the default definitions by providing its own (see the code, but the main difference is that the cat's move() function moves 2 spaces at a time instead of 1) cis15-fall2008-azhar-lecIV.1 16 a picture of this new hierarchy is shown below: protected point creature robot cat dirt earlier we mentioned that we could not directly access a superclass' private data members in a subclass, but instead had to use the superclass' public function members to get access to the private data members however, if we really wanted access to the superclass' data members, we could declare them as protected instead of private protected data members sit somewhere between public members, which are accessible to any object, and private members, which are only accessible within that class roughly speaking, protected members are like private data members but are also accessible by members of derived classes we will talk more about protected later on. cis15-fall2008-azhar-lecIV.1 17 cis15-fall2008-azhar-lecIV.1 18 more on inheritance since robot is a subclass of creature, we can carry out any operation on a robot that we can on a creature and on a point we already know that this is the case where the operations are function members of creature with simple parameters thus we can do: robot rosie; rosie.set( 2, 3 ); rosie.move(); calling methods from point and creature on robot comparing objects we can compare two instances of an object: bool robot::busier( robot r ) { if ( this->num_vacuumed > r.num_vacuumed ) return true; else return false; } we can pass this function an instance of a robot and get the result that compares the current instance (referenced by the pointer this->) to its argument instance cis15-fall2008-azhar...

Find millions of documents on Course Hero - Study Guides, Lecture Notes, Reference Materials, Practice Exams and more. Course Hero has millions of course specific materials providing students with the best way to expand their education.

Below is a small sample set of documents:

CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ fall 2008 lecture # II.2 topics: ctors and dtors polymorphism and overloading friend classes, composition and derivation resources: Pohl, chapter 5 (mostly sections 5.1-5.3, 5.7, 5.10)ctors and dt
CUNY Baruch - CIS - 1
cis1.5-spring2008-azhar, lab II, part 1instructions This is the first part of the lab/homework assignment for unit II. The entire assignment will be worth 9 points. The first part is worth 4 points and will be distributed and worked on in class o
CUNY Baruch - CIS - 1
today our first c+ program output the software development cycle variables data types data storage binary numbers ASCII assignment and mathematical operators &quot;hello world&quot;our first c+ program. typical first program in any language outpu
CUNY Baruch - CIS - 1
cis1.5 introduction to computing using c+ (robotics applications) spring 2008 lecture # I.1 introduction(0) introduction to the course about this course introduction to computer programming using the C+ language uses robotics as a context (i.e.,
CUNY Baruch - CIS - 1
today: loops and le operations loops le operationsloops looping, or iteration, means doing something more than once, perhaps doing something over and over and over and . and over again there are times when you want your program to do something
CUNY Baruch - CIS - 1
today: new topics: two-dimensional arrays switch statement review topics: constants string functions (see lecture IV.2 and textbook chapter 8) cctype functions (see lecture IV.2 and textbook chapter 8) In class, we followed a comprehensive ex
CUNY Baruch - CIS - 1
cis1.5-spring2008-azhar, lab IV, part 1instructions This is the rst part of the lab/homework assignment for unit IV. The entire assignment will be worth 9 points. The rst part is worth 4 points and will be distributed and worked on in class on Th
CUNY Baruch - CIS - 1
today: simple classes FINAL EXAM: will be on MONDAY MAY 21, 1.00pm3.00pm (room to be announced.)simple classes classes are ways of organizing programs to provide structure a class is a special kind of compound data type classes are compound be
CUNY Baruch - CIS - 1
today: strings what are strings and why to use them reading: textbook chapter 8what are strings a string in C+ is one of a special kind of data type called a class we will talk more about classes in detail at the end of the term but note that
CUNY Baruch - CIS - 1
today: math operations and function arguments random numbers math operators data type conversion function argumentsrandom numbers computers can generate random numbers, which is like picking a number by rolling dice there are two steps necess
CUNY Baruch - CIS - 1
today: functions what are functions and why to use them library and programmer-defined functions parameters and return values reading: textbook chapter 5, sections 1-4 modularityadvantages of functions we can divide up a program into small,
CUNY Baruch - CIS - 1
today: arrays what are arrays and why to use them integer arrays reading: textbook chapter 7, sections 3-4arrays arrays are used to hold sets of related types of data the data could be integers or doubles or booleans the data could also be ch
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ fall 2008 lecture # I.2 topics: unix fundamentalswhat is UNIX? Unix is an operating system (like Windows) which means it is a program that runs on a computer that makes it possible for you to use t
CUNY Baruch - CIS - 1
cis1.5-spring2008-azhar, lab IIIinstructions This assignment will be worth 9 points. Both parts together are due on Wednesday March 26 and must be submitted by email (as below). Follow these emailing instructions: 1. Create a mail message address
CUNY Baruch - CIS - 1
today: sorting algorithms blort sort selection sort insertion sort bubble sort FINAL EXAM: Thursday, May 22nd (1pm - 3pm) (place to be announced.)sorting sorting is one of the classic tasks done in computer programming the basic idea with s
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ fall 2008 lecture # V.1 topics: arrays pointers arrays of objects resources: some of this lecture is covered in parts of Pohl, chapter 3arrays and pointers overview arrays and pointers are strong
CUNY Baruch - CIS - 10
Philosophy of Artificial Intelligence Paper 2 Due Tuesday, May 1 In this paper you will work out some ideas from section 6 of Daniel Dennett's &quot;Quining Qualia.&quot; Below I give you some very strong suggestions about how to proceed. (If you want to appro
CUNY Baruch - CIS - 15
cis15/summer2008/ozgelenC+ ReviewWrite a complete C+ program, including at least one comment for the main program and one for each function, as follows. Your program will emulate some aspects of a card game based on the game of Hearts. Its okay if
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment IIinstructions This is assignment for unit II. It is worth 10 points. It must be submitted by email (as below). Follow these emailing instructions: 1. Create a mail message addressed to ozgelen@sci.brooklyn.c
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment IVinstructions This is assignment for unit IV. It is worth 10 points. It must be submitted by email (as below). Follow these emailing instructions: 1. Create a mail message addressed to ozgelen@sci.brooklyn.c
CUNY Baruch - CIS - 15
Quick and Dirty EMACSNote that any ordinary character goes into the buffer (no insert is needed). For a more detailed emacs manual, check out the GNU emacs manual on-line at: http:/www.gnu.org/software/emacs/manual/Some necessary abbreviations:CM
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment III, part 2instructions This is the second part of the assignment for Unit III. It is worth 5 points. Follow these emailing instructions: 1. Create a mail message addressed to ozgelen@sci.brooklyn.cuny.edu with
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment III, part 1instructions This is the rst part of the assignment for Unit III. It is worth 5 points. Follow these emailing instructions: 1. Create a mail message addressed to ozgelen@sci.brooklyn.cuny.edu with th
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment I, part 1instructions This is the rst part of the assignment for unit I. The entire assignment will be worth 5 points. The rst part is worth 2 points. The second part is worth 3 points. Both parts must be su
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # VI.1 topics: recursion searchingrecursion recursion is defining something in terms of itself there are many examples in nature and in mathematics and in computer graphics,
CUNY Baruch - CIS - 15
cis15-summer2008 ozgelen, assignment I, part 2instructions This is the rst part of the assignment for unit I. The entire assignment will be worth 5 points. The rst part is worth 2 points. The second part is worth 3 points. Both parts must be su
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # V.2 topics: functions: parameters and arguments call by value vs call by reference namespaces generic pointers dynamic memory allocation resources: Pohl, chapter 3functions
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # V.1 topics: arrays pointers arrays of objects resources: some of this lecture is covered in parts of Pohl, chapter 3arrays review a data structure consisting of related elem
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # II.2 topics: ctors and dtors polymorphism and overloading friend classes, composition and derivation resources: Pohl, chapter 5 (mostly sections 5.1-5.3, 5.7, 5.10)ctors and
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ spring 2008 lecture # VII.1 topics: generic programming templates STL (standard template library) on-line reference: http:/www.cppreference.com/index.htmlgeneric programming methodology for enhan
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # I.1 introduction topics: (0) introduction to the course (1) to do (2) review of c+ instructor: Arif Tuna Ozgelen, ozgelen@sci.brooklyn.cuny.edu course web page: http:/www.sci.bro
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # IV.1 topics: inheritance composition of classes resources: Pohl, chapters 8 and 11an example consider the program patrol.cpp (posted on the class web page) this program mode
CUNY Baruch - CIS - 15
cis15-summer2008-ozgelen, assignment I, part 3 (optional)instructions This is an optional third part of the assignment for unit I. This part is worth 1 point of extra credit and can be submitted along with parts 1 and 2.1. enumerated data types
CUNY Baruch - CIS - 1
cis1.5-fall2008-ozgelen, lab Vinstructions This lab must be submitted by email (as below): 1. Create a mail message addressed to ozgelen@sci.brooklyn.cuny.edu with the subject line cis1.5. 2. Attach ONLY the .cpp files for each part, as outlined be
CUNY Baruch - CIS - 1
today: math operations and function arguments random numbers math operators data type conversion function arguments file operationsrandom numbers computers can generate &quot;random&quot; numbers, which is like picking a number by rolling dice there a
CUNY Baruch - CIS - 1
today: sorting algorithms blort sort selection sort insertion sort bubble sortsorting sorting is one of the classic tasks done in computer programming the basic idea with sorting is to rearrange the elements in an array so that they are in a
CUNY Baruch - CIS - 1
cis1.5-fall2008-ozgelen, lab II, part 2instructions This part of the lab is worth 5 points. Both parts must be submitted by email (as below): 1. Create a mail message addressed to ozgelen@sci.brooklyn.cuny.edu with the subject line cis1.5. 2. Atta
CUNY Baruch - CIS - 1
cis1.5 introduction to computing using c+ (legal applications) fall 2008 lecture # I.1 introductionintroduction to the course about this course introduction to computer programming using the C+ language uses legal applications as a context (i.e.
CUNY Baruch - CIS - 1
today: new topics: two-dimensional arrays switch statement review topics: constants string functions (see lecture IV.2 and textbook chapter 8) cctype functions (see lecture IV.2 and textbook chapter 8) In class, we followed a comprehensive ex
CUNY Baruch - CIS - 1
today: logical operations and control structures the if statement relational operators logical operators truth tables loops: the while and for statementsthe if branching statement we have already been using the if statement: void decreaseRais
CUNY Baruch - CIS - 1
today: strings what are strings and why to use them reading: textbook chapter 8what are strings a string in C+ is one of a special kind of data type called a class we will talk more about classes in detail at the end of the term but note that
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # I.2 topics: unix fundamentalswhat is UNIX? Unix is an operating system (like Windows). OS? - A program that coordinates and oversees the resources of a computer and provides a
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # III.1 topics: C-style strings input and output resources: Pohl, chapter 9C-style strings (1). storing multiple characters in a single variable data type is still char BUT i
CUNY Baruch - CIS - 15
cis15 advanced programming techniques, using c+ summer 2008 lecture # II.1 topics: objects and class design command line arguments resources: Pohl, chapter 4aggregate data types class and struct struct comes from C class is new in C++ both a
CUNY Baruch - CIS - 1
today: functions what are functions and why to use them library and programmer-dened functions parameters and return values reading: textbook chapter 5, sections 1-4 modularityadvantages of functions we can divide up a program into small, un
CUNY Baruch - CIS - 1
today: arrays what are arrays and why to use them integer arrays reading: textbook chapter 7, sections 3-4arrays arrays are used to hold sets of related types of data the data could be integers or doubles or booleans the data could also be ch
CUNY Baruch - CS - 3157
MAKE(1L)LOCAL USER COMMANDSMAKE(1L)NAME make - GNU make utility to maintain groups of programs SYNOPSIS make [ -f _ a_ e_ i_ e ] [ option ] . target . m_ k_ f_ l_ WARNING This man page is an extract of the documentation of _ N_ _ a_ e G_ U m_ k
CUNY Baruch - CS - 1007
CS1007 lecture #4 notesthu 12 sep 2002 news data types and storage variables and assignment binary numbers and arithmetic ASCII Strings math operators increment and decrement operators reading: ch 2.5,2.7-2.12cs1007-fall2002-sklar-lect04
CUNY Baruch - CS - 1007
CS1007 lecture #21 notesthu 21 nov 2002 news vectors linear searching reading: ch 12.1-12.6cs1007-fall2002-sklar-lect21 1vectors (1).Java has a nice class which handles arrays dynamically: java.util.Vector the elements of a Vector can be
CUNY Baruch - CS - 1007
CS1007 lecture #6 notesthu 19 sep 2002 news homework #2 will be posted by midnight tonight homework #1 should be returned in recitation next week the while and for loops break and continue statements switch statement reading: ch 3.5-3.10 cs10
CUNY Baruch - CS - 3157
fork(2)fork(2)NAMEfork, fork1 - create a new processSYNOPSIS#include &lt;sys/types.h&gt; #include &lt;unistd.h&gt; pid_t fork(void); pid_t fork1(void);MT-LEVELfork( ) is Async-Signal-SafeDESCRIPTIONfork( ) and fork1( ) cause creation of a new proce
CUNY Baruch - CS - 1007
CS1007 lecture #10 notestue 8 oct 2002 NEWS error quiz # 1, question 1d if you got the question WRONG, bring the quiz to class on thu 10 oct for a one-time regrade arrays (one-dimensional) nding array minimum and maximum sorting big-Oh 2-dimension
CUNY Baruch - CS - 1007
CS1007 lecture #15 notestue 29 oct 2002 news networks applets GUIs reading: ch 8.1-8.4cs1007-fall2002-sklar-lect15 1networks (1).two or more computers connected to each other networked computers can share information and resources, e.g.:
CUNY Baruch - CS - 3157
SED(1)User CommandsSED(1)NAME sed - a Stream EDitor SYNOPSIS sed [-n] [-V] [-quiet] [-silent] [-version] [-help] [-e script] [-expression=script] [-f script-file] [-file=script-file] [script-if-no-other-script] [file.] DESCRIPTION _ e_ is a str
CUNY Baruch - CS - 3157
C for Java Programmerslecture notes credits: Advanced Programming (cs3995, Spring 2002, Prof Schulzrinne) Software Construction (J. Shepherd) Operating Systems at Cornell (Indranil Gupta) today: Why learn C after Java? A brief background on C
CUNY Baruch - CS - 1007
CS1007 lecture #7 notestue 24 sep 2002 news homework #2 due tue oct 1 homework #1 should be returned in recitation this week short quiz #1 (5 points) in class on thu sep 26 bring one page of notes no computers, calculators, phones, etc. for, whil
CUNY Baruch - CS - 3157
lecture #16 mon oct 28, 2002news homework #3 due today homework #4 out today replacing quiz #3 see web page for updates. today programming tools overview conguration management sources: some slides from H. Schulzrinne, cs3995, spring 2002
CUNY Baruch - CS - 1007
CS1007 lecture #3 notestue 10 sep 2002 http:/www.columbia.edu/~cs1007 today: news quick and dirty UNIX quick and dirty emacs creating/editing/compiling/running your first Java program homework #1 submitting homework #1 cs1007-fall2002-s
CUNY Baruch - CS - 3157
Perl Regular Expressions: Kleene Star Kleene star and friends * operator ? match zero or more times Precedence is tighter than union ? /very* long string/Perl Regular Expressions: Special Characters Character class shortcuts \d = [0-9] \s = w
CUNY Baruch - CS - 3157
lecture #20 wed nov 13, 2002news homework #5 and #6 will be combined check class home page for updates today CGI HTML forms sources see references link on class web page cs3157-fall2002-sklar-lect201a.cgi#!/bin/sh echo &quot;Content-type:
CUNY Baruch - CS - 3157
read(2)read(2)NAMEread, pread, readv - read from fileSYNOPSIS#include &lt;sys/types.h&gt; #include &lt;sys/uio.h&gt; #include &lt;unistd.h&gt; ssize_t read(int fildes, void *buf , size_t nbyte); ssize_t pread(int fildes, void *buf , size_t nbyte, off_t offset)