In-class Examples List
CS 131
Intro to Computer Science I
Fall 2003
Last modified: Mon Dec 8 16:08:42 PST 2003
Important Notes:
- This is merely a selection of files and examples, most of which were
mentioned or used in the class. It is not a comprehensive
list, nor are all lectures and labs included. Formatting is generally
minimal.
- Week 15, Lecture 1, December 8, 2003: using #ifndef to help
prevent redefinition errors, discussion (but no executable
examples) of the distinction between redefined and overloaded
function in a derived class, the definition of a function
signature, how a derived call instance can call the base class'
version of a redefined function, and brief discussion of
protected class members (as compared to private and public
class members)
- Week 14, Lecture 2 version of
Point3.h, except now with #ifndef added
- Week 14, Lecture 2, December 3, 2003: examples involving
destructor, copy constructor, overloaded assignment, and
intro to inheritance
- First, the problem: these help show how, without explicitly-
(and appropriately-)defined destructor, copy constructor,
and overloaded assignment operator, pointer and/or
dynamically allocated fields within a class can be
problematic:
Student1.h,
Student1.cpp,
uglyFunct.h,
uglyFunct1.cpp,
showDiff1.cpp
- Then, the solution: these help show how explicitly-
and appropriately-defined destructor, copy constructor,
and overloaded assignment operator can help when a
class includes pointers and/or dynamically allocated
fields:
Student2.h,
Student2.cpp,
uglyFunct.h,
uglyFunct2.cpp,
showDiff2.cpp
- Simple example of inheritance: ColoredPoint3, a derived
class of base class Point3 (and a simple testing function):
Point3.h,
Point3.cpp,
ColoredPoint3.h,
ColoredPoint3.cpp,
test_ColoredPoint3.cpp
- Week 13, Lecture 2, November 19, 2003: intro to overloaded
operators, friends, and other class-related topics
- Week 13, Lecture 1, November 17, 2003: intro to recursion
- Week 12, Lecture 2, November 12, 2003: examples of using pointers,
dynamic arrays, and a MyNode class used to build a simple linked
list
- Week 11, Lecture 1, November 3, 2003: examples of
C++'s sizeof operator (to tell how many bytes of storage
are required by a variable or type), "modern"
C++ type casting, output-formatting using setw,
and the switch statement.
-
sizeof1.cpp
- example using C++ sizeof operator
-
cast1.cpp
- example using "modern" C++ type casting
-
setw1.cpp
- example of output formatting including use of
setw
-
switch1.cpp
- example using a switch statement
- Week 10, Lecture 2, October 29, 2003: examples of pass-by-reference,
file i/o
-
wtsIntoArray.cpp,
test_wtsIntoArray.cpp,
- example using pass-by-reference and file input
-
io1.cpp
- baseline example using interactive input and file output
(new output file each time called)
-
io2.cpp
- example using interactive input and file output
(appending to output file each time called)
-
io3.cpp
- example using interactive input and file output
(appending to output file each time called)
-
io4.cpp
- example using file input and interactive output
-
io5.cpp
- example using file input and file output
(new output file each time called)
-
io6.cpp
- example using interactive input and file output,
but also using a system call and another little
helper file to append the time and date before
each new rat weights result appended to the output
file.
-
io7.cpp
- POST-LECTURE example using file input and interactive output,
BUT it asks the user for the name of the file
where the rats' weights can be found.
- Week 10, Lecture 1, October 27, 2003: pass-by-reference, intro
to file i/o (but no examples of file i/o yet)
-
printBeerVerse.cpp,
printBeerVerse.h,
testPrintBeerVerse.cpp
- baseline example of a pass-by-value parameter
-
testBeer2.cpp
- showing the argument's value before and after the
function call
-
uglyBeerVerse.cpp,
uglyBeerVerse.h,
testBeer3.cpp
- still pass-by-value, but demonstrates how changing a
pass-by-value parameter, while poor style, does NOT change
the corresponding argument
-
print2.cpp,
print2.h,
testBeer4.cpp
- now pass-by-reference, and demonstrates how changing
a pass-by-reference parameter DOES change the corresponding
argument
-
swap.cpp,
swap.h,
testSwap.cpp
- a second (and classic) pass-by-reference example:
a function that swaps the values of its two arguments
- Week 9, Lecture 2, October 20, 2003: arrays
- sumArray.cpp
- sumArray.h
- (note the use of const before sumArray's array
parameter --- as mentioned in the 131 HW #8 handout,
this should be used if the function is NOT intended
to CHANGE the array passed as an argument)
- tryMe1.cpp -
simple array examples, and calling function sumArray
- tryMe2.cpp -
more array examples, including an array of objects (Dice)
and a 2-dimensional array
- (note: for Dice and randgen code, used in tryMe2.cpp,
see Week 8, Lecture 2 links below)
- Week 8, Lecture 2, October 13, 2003: implementation of
C++ class files, continued, and for loops
- Dice example is adapted from example in Owen Astrachan's
"A Computer Science Tapestry", 2nd Edition, MacGraw Hill
- Dice.h
- Dice.cpp
- tryDice.cpp
-- informal tests done in-lecture
- testDice.cpp
-- a more-thorough set of tests, but still using a
for-loop
- utility code from Astrachan:
- Week 8, Lecture 1, October 13, 2003: implementation of
C++ class files
Back to Course Home Page
Please send questions to
st10@humboldt.edu .