vi
editor.
Use
wget
to download one or two of the C++ programs we looked at today,
and then compile and run them.
(For example, how about a program such as
12hour.C
or
2d.C,
whose output depends on the current date and/or time?)
See you in room 602 Leon Lowenstein tomorrow (May 28).
passstruct.C.
Move
your program to the
public_html
subdirectory of your home directory on
storm.cis.fordham.edu,
then go
here
and make sure your program is visible.
While you’re at it, you can
remove
any
junk
files left over from the first night of the course.
Read and compare
bubble1.C
and
bubble2.C.
Study the three versions of
structure.C,
structure1.C,
structure2.C.
$PATH?
Create a
bin
subdirectory of your home directory on
storm.cis.fordham.edu,
if you have not already done so.
Create and
chmod
the
compile
shellscript if you have not already done so.
See
Linux.
Required reading:
compare the three versions of
3ints.C,
struct.C,
obj1.C.
The ideal way to do this would be to print them out
and study them side by side.
Then write a C++ program with a class of objects containing data members.
Imitate
obj1.C.
For example,
it could be
print
member function.
As usual, put this C++ program in the
public_html
subdirectory of your home directory on
storm.cis.fordham.edu.
weatherreport.C
(with output
weatherreport.txt),
turning it into the easier-to-read
weatherreport2.C
(with output
weatherreport2.txt).
Continue to study the evolving series of programs
3ints.C:
a lot of disconnected intsstruct.C:
create a type of structureobj1.C:
create a class of objectsobj2.C:
a class with a constructorobj2.3:
a class whose constructor initializes the data members of the
newborn object
Hand in (i.e., place in the
public_html
subdirectory
of your home directory on
storm.cis.fordham.edu)
a version of
obj2.C
incorporating the answers to exercises 4d, 4e, 4f in
this page.
If you don’t have time to finish this by Wednesday, June 4,
and/or if you think we’re going to fast,
we can spend some or all of our lab hour on Wednesday working on 4d, 4e, 4f.
Write an interesting class
(in a .h file and a .C file)
that has one or more friend functions, as well as member functions.
date
(date.h,
date.C)
with all the
operator
functions we did in class today.
Also add
operator-=,
operator-
(only the one taking a date and an int),
prefix
operator--,
and
postfix
operator--.
Do not bother with the pedagogical function definitions.
The program should also have a
main.C
file containing a main
function that will test all these
operator
functions.
Special extra homework only for
ear7.
Add the following code to the
main
function in your
objRev.C.
Does it produce the correct output?
If not, can you debug it?
date d {6, 1, 2025}; //June 1, 2025
d.prev();
d.print(); //Should output 5/31/2025
cout << "\n";
date e {1, 1, 2025}; //January 1, 2025
d.prev();
d.print(); //Should output 12/31/2024
cout << "\n";
grade
the usual overloaded operators.
(It doesn’t need
a copy constructor,
operator=,
operator[]
or
operator().)
Observe that the
operator
functions for class
grade
are very similar to the ones we wrote for
the one-data-member class
date.
rabbit
(in
rabbit.h and
rabbit.C)
and class
wolf
(in
wolf.h and
wolf.C)
in the video game, and also the
main
function.
terminal.h
is a good summary of the services offered by the
terminal
object.
Download the game and run it.
You can make little changes like changing the speed
(change the
250
in the
main
function)
or changing the background color (i.e., the background character)
of the
terminal
object
(change the argument of the constructor of the
terminal
object in the
main
function).
Do the destructor exercise.
storm.cis.fordham.edu
if you have not already done so.
If you can’t download and compile it,
you can run the two versions there in
~mmeretzky/game1/a.out
and
~mmeretzky/game2/a.out.
A criticism of the game in ¶ 24:
we have incorrectly bundled together carnivorous appetite and
manual motion in class
wolf;
this makes it impossible to inherit one without the other.
Similarly,
we have incorrectly bundled together herbivorous appetite and
random motion in class
rabbit;
this makes it impossible to inherit one without the other.
We will correct these problems with a dose of multiple inheritance and templates
during the last week of the course
in paragraph 32,
and then with
Templates.
wolf
and
rabbit
(and
boulder
and
landmine)
have become
identical,
except for the names of the mother and father of each grandchild,
and the
char
with which each grandchild appears on the screen.
It is therefore appropriate and easy
to replace these identical grandchild classes with the single
template class
grandchild
in paragraph 22 of
Templates.
The
grandchild
class takes three template arguments:
the mother, the father, and the
char.
Do the
map
homework in paragraph 3 of
Gateways.
If you have time,
play with the form and the C++ program in paragraphs 4 and 5.
storm.cis.fordham.edu.
Write your secret password on a piece of paper.
If you forgot your secret password,
submit
this form
to reset your secret password.
Nothing to hand in this week.
Compare
binary.C,
which outputs an integer as 32 bits,
with
hexadecimal.C,
which outputs an integer as 8 hex digits.
Each hex digit is an abbreviation for a series of four bits.
(A series of four bits is called a nibble.)
Can you compile and run these two C++ programs in your home directory on
storm.cis.fordham.edu?
Do you understand why the first for loop in
loop.C
has to perform an invisible multiplication and addition
each time you evaluate the expression
a[i]?
Do you understand how the second
for
loop avoids this hidden arithmetic?
Play with the Linux commands.
If you put a file in the public_html subdirectory of your
home directory on storm.cis.fordham.edu,
see if it shows up
here.
(You may have to refresh
this page
in your browser.)
bintodec1.C
(in
bintodec2.C,
bintodec3.C,
bintodec4.C)
to catch the
invalid_argument
and
out_of_range
exceptions (and any other exceptions) that might be thrown by the function
stoi.
Admire how sdg5 generated the hexadecimal digits from
right to left,
and then loaded them into the array result
starting at the end of the array and working forward,
in his program
hexadecimal.C.
Compare to this
hexadecimal.C.
Note the similarities between the following programs:
rocket.C
looped through an array,
and used a pointer (p) to access four consecutive elements
of the array
(p[0],
p[1],
p[2],
p[3])
during each iteration of the loop.
(Does
rocket.C
run smoothly on your Apple Macintosh or Windows PC?
Are you really serious about animating the recursive
maze.C
program we ran last semester?)
movingaverage.C
looped through an array,
and used a pointer (p) to access five consecutive elements
of the array
(p[-2],
p[-1],
p[0],
p[1],
p[2])
during each iteration of the loop.
bubblesortintptr.C
looped through an array,
and used a pointer (q) to access two consecutive elements
of the array
(q[0],
q[1])
during each iteration of the loop.
The program
passstruct.C
began with the blueprint for a new type of structure
(called a month).
It created a variable named j
of this type, and passed the address of this variable to a function named
f.
The function received this address as a pointer named p,
and used the pointer to access the two fields inside of j.
The blueprint for month
had to be written up above the
main
function, to make it possible to mention the word month
in both of the functions main
and f.
Imitating
passstruct.C,
write a C++ program that has a blueprint for an interesting type of structure
containing a few fields.
Then create a variable of this type in the main function,
and pass the address of the variable down to another function.
The other function will receive the address as a pointer named p
and will use the pointer to access the fields inside of the structure.
Have the function do something interesting with the structire,
not merely outputting the fields of the structure.
Name your program
structure.C,
and put it in the
public_html
subdirectory of your home directory
on
storm.cis.fordham.edu
by 6:00 p.m. EST on Wednesday, January 29, 2025.
For more inspiration, look at
struct.C.
It creates a structure named tomorrow,
and passes the address of tomorrow
down to the one-argument next function,
which does an elaborate computation to change the three fields inside
of
tomorrow
to the following day’s date.
Good luck.
date in
obj1.C
already has the following two non-const
member functions:
void next(int n); //Go n days into the future. void next(); //Go 1 day into the future.Add two more non-
const
member functions
void prev(int n); //Go n days into the past. void prev(); //Go 1 day into the past.that will make the object to which they belong move into the past.
Test your new functions by making a date
object that contains March 1, 2025.
Make the object go 1 day into the past and verify that it changes into
February 28, 2025.
Put this program into a file named
prev.C
in the
public_html
subdirectory of your home directory on
storm.cis.fordham.edu
by Wednesday, February 5, 2025 at 6:00 pm EST.
Also by next Wednesday,
make a C++ program named interesting.C
in your public_html
that creates a class, and creates one or more objects of that class,
and calls one or more member functions of these objects to do something
interesting.
A simple but spiritless possibility would be to create a class
time
containing three data members named
hour,
minute,
second,
very similar to the class date in
obj1.C.
An experiment you could try:
what is the biggest number of integers you can say you want to store on
storm.cis.fordham.edu
in the program
new3.C
without throwing an exception?
Is it always the same number?
Some code you could study:
do the two versions of the function categor in
jr224
always return the same value for any windspeed greater than or equal to 74?
The Rose Hill tutoring room, John Mulcahy Hall room 310, is open 11:30–5:15 M–F (only until 4:00 pm on Monday until they find coverage).
obj2.C.
date
in this program already has a constructor with three explicit arguments.
Keep this constructor,
but add another constructor with no explicit arguments.
This no-arg constructor will initialize the newborn date
object to today’s date.
To get this information,
the constructor will call the functions
time
and
localtime
that we saw in
time.C.
Test your new constructor with the following code in the
main
function:
const date t; //Call the new constructor with no explicit arguments. cout << "Today is "; t.print(); //Make sure that t contains today's date. cout << ".\n";
date
in this program already has several member functions.
Keep them, but add another member function declared as
public: //Declarations for member functions of class date int dayOfYear() const;that will return an
int,
in the range 1 to 365 inclusive,
that will indicate what day of the year is the object
that the member function belongs to.
For example, the
dayOfYear
member function of a date object containing
January 1 of any year will return 1, and the
dayOfYear
member function of a date object containing
December 31 of any year will return 365.
(Christmas will return 359.)
Your member function will have to use the numbers in the
date::length
array.
Test your new member function with the following code in the
main
function:
const date xmas {12, 25, 2025}; //Call constructor with 3 explicit args.
cout << "Christmas is day number " << xmas.dayOfYear()
<< " of the year.\n"; //Make sure it's 359.
friend
function declared as
public: //Declarations for member functions and friend functions of class date int dayOfYear() const; //a member function friend int distance(const date& d1, const date& d2); //a friend functionand defined as
int distance(const date& d1, const date& d2)
{
//etc.
}
Its two arguments will be references to two date
objects,
and it will return the distance measured in days between the two objects.
Think of this function as if it were subtracting
d2
from
d1.
That means the return value will be positive if
d1 is later than d2,
zero if
d1 and d2 are holding the same date,
and
negative if d2 is later than d1.
Hint:
distance
should call
dayOfYear
to do some of its work.
Test your new friend function with the following code in the
main
function:
const date summer {6, 20, 2025}; //first day of summer
const date t; //today
const int dist {distance(summer, t)};
cout << "Only " << dist << " days till summer.\n";
three.C
in the
public_html
subdirectory of your home directory on
storm.cis.fordham.edu
by Wednesday, February 12, 2025 at 6:00 pm EST.
Make a subdirectory named height
in the public_html
subdirectory of your home directory on
storm.cis.fordham.edu:
cd cd public_html pwd mkdir height ls -ld height cd height pwdIn this new directory, place a new version of the C++ program consisting of the three files
height.h,
height.C,
and
main.C.
In this new version, change the feet and inches
data members of class height
to the following data member.
double centimeters; //the height in centimetersKeep the arguments of the constructors, and the output of the
print
member function, the same;
the rest of the program (in this case, the main function),
should have no idea that the data members have been changed.
Extra credit, for people who know Trigonometry.
Make a subdirectory named point
in the public_html subdirectory of your home directory
on
storm.cis.fordham.edu.
In this new directory,
place a new version of the C++ program consisting of the three files
point.h,
point.C,
and
main.C.
In this new version, change the x and y
data members of class height
to the following two data members
double r; //the polar coördinates of the point double theta; //in radiansKeep the arguments of the constructors, and the output of the
print
member function the same;
the rest of the program (in this case, the main function),
should have no idea that the data members have changed.
Also add two new public member functions,
double x() const; double y() const;that will return the Cartesian coördinates of the
point.
More extra credit.
Do the indentation exercise at the end of class
announcer.
See Glenmore Marshall’s instructions for installing C++ on Macintosh (not required for this course). Thanks.
I corrected my embarrassing inefficiences in the
dayOfYear
member function of class date.
Sorry.
date
(date.h,
date.C,
main.C)
to
two data members
and then to one data member.
operator<<
to the one-data member class
date
in
date.h,
date.C,
main.C.
Review how we added the (inline) member function
operator+=
to the one-data member class
date
in
date.h,
date.C,
main.C.
grade
in your home directory on storm.cis.fordham.edu.
cd (Go to your home directory.) pwd (Make sure you arrived there.) ls -l (See what you already have in your home directory.) mkdir grade (Create a subdirectory named grade.) ls -ld grade (Make sure you created the subdirectory.) cd grade (Go down into the new subdirectory.) pwd (Make sure you arrived there.) ls -l (See what's there. It should be empty, of course.)In this subdirectory, create a three-file C++ program (
grade.h,
grade.C,
main.C)
that creates a class named
grade.
An object of this class will hold one of 14 possible values,
indicating a grade in the range
F, F+, D–, D, D+, …, A–, A, A+.
What data member(s) would have to be inside of each grade
object to empower the grade object to hold one of these 14
possible values?
Give the class a constructor that will accept one
string argument such as
"A"
or
"A-"
or
"B+".
Give class grade a friend function
operator<<
and a member function
operator+=,
imitating what we did for the one-data member class
date.
Write a main
function in the file
main.C
that will construct one or more grade
objects and demonstrate that
operator<<
and
operator+=
work.
If you just can’t get class
grade
to work,
you can peek at my
grade.h,
grade.C,
main.C,
but I want to encourage you to do it on your own.
Can you figure out how to make an
operator-=
member function
(almost identical to
operator+=)?
Next week, we will make lots of other operator functions,
starting with
operator+
and
operator++.
Thank you.
operator+=
and prefix
operator++
change the value of an existing object.operator+
and postfix
operator++
create a new object.
(operator+
can create this new object as a pass-by-value argument.)
operator+=
is simpler than
operator+.
And prefix
operator++
is simpler than postfix
operator++.
operator+
and prefix
operator++
can do some of their work by calling
operator+=.operator++
can do some of its work by calling prefix
operator++.
operator!=,
operator>,
operator>=,
operator<=
can do some of their work by calling the two friend functions
operator==
and
operator<.
operator
function calls another
operator
function that you have previously written.
There is no speed penalty in doing this,
if the
operator
function that you are calling is inline.
+,
prefix
++,
postfix
++,
==,
!=,
-,
<,
>,
>=,
<=,
>>)
to class grade
(grade.h
and
grade.C).
Also add
-,
prefix --,
and
postfix --.
Note that you are writing two different functions named
operator-,
one to subtract an int from a grade,
yielding a new grade object,
and
one to subtract a grade from a grade,
yielding an int that gives the distance between
the two objects.
And you are writing two functions named
operator+,
one with an object on the left and an int on the right,
and
one with an object on the right and an int on the left.
Add some statements to
to class grade’s
main.C
to test each of your new functions.
Put a web page containing a form into your
public_html
directory.
The form will launch a gateway program named
display.cgi
in the cgi-bin
subdirectory of your
public_html
directory.
cd cd public_html pwd ls -l wget https://markmeretzky.com/fordham/2000/src/gateway/form.html ls -l form.html mkdir cgi-bin (Make a new directory.) ls -ld cgi-bin cd cgi-bin pwd wget https://markmeretzky.com/fordham/2000/src/gateway/display.C ls -l display.C c++ display.C ls -l a.out mv a.out display.cgi ls -l display.cgi
Then edit your
form.html
file,
and in the
ACTION
attribue of the
FORM
tag, change my login name
mmeretzky
to your login name.
Then point your web browser at
https://storm.cis.fordham.edu/~jsmith/form.html
where jsmith
is your login name,
and press some buttons.
Please let me know if this doesn’t work.
Study
display.C,
especially how the
main
function loops through the map
m, spitting out pairs of
LI
tags.
Special homework only for kheslin2:
make a BMI (Body Mass Index) form (English or Metric)
that launches a C++ program to display your BMI.
For example, suppose you have a widget (of type NUMBER)
named height
representing your height in inches.
Then assuming you have created the map named m in
display.C,
you can get the numerical value of height
by calling the function
stoi
(“string to integer”):
const int height {stoi(m["height"])}; //#include <string> for stoi
Please have this ready to demonstrate for the class on Thursday, April 3, 2025.
Thanks,
kheslin2!
interesting2.C
the way we reorganized the program
interesting.C
into the files
vehicle.h,
car.h,
motorcycle.h,
and
main.C
in class on April 3.
Keep the output of the program exactly the same,
income 30.00 capital gains 40.00 regressive 1000100.00 progressive 10000.00 property 200.00but use inheritance to get rid of all the
if/else
statements.
(You will create five classes derived from class
tax;
write them, and class
tax
itself, in six
.h
files.
In the main
function,
loop through an array of pointers.)
Put an HTML file containing a form into your
public_html
directory on storm.cis.fordham.edu.
Put a gateway program into the cgi-bin subdirectory pf your
public_html
directory.
It’s okay if you want to get started by making a copy of my form
and my gateway, as in the
first box.
But then please change the form and gateway to something original
and surprising.
h,
j,
k,
l
to move the
wolf.
(This is code that last saw the light of day during the Obama Administration.)
Then in the main
function in
main1.C,
replace the
rabbit
object with an array
(or a
vector<rabbit>)
of three
rabbit
objects (or as many as you want).
For example,
rabbit a[] {
{term, 10, 20}, //Please use different numbers, not mine.
{term, 30, 40},
{term, 50, 60}
};
const size_t n {size(a)}; //need <iostream> for size
During each iteration of the loop in the main
function, call the move member function of each
rabbit in the array
(or in the
vector<rabbit>).
You can let the game end as soon as any
rabbit
is eaten by the
wolf.
Now that you have more than one
rabbit,
change the victory message in
main1.C
to “You killed a rabbit!”.
For the Linear Algebra of Quantum Mechanics, I recommend volume 2 of Leonard Susskind’s Theoretical Minimum series, Quantum Mechanics: The Theoretical Minimum (2014). A little book (small format paperback, 280 pp.) on neural networks I recently read was Deep Learning (2019) by John D. Kelleher, part of the MIT Press Essential Knowledge series.
wolf
and
rabbit.
easter.C
we ran in class today.
It was a present for my Monday night CISC-1100-C01 students.
Please run the version of the game with
multiple inheritance.
You can add a few
boulders and
landmines to
main.C
if you wish.
Then follow the directions to mass produce all the grandchild classes
(wolf,
rabbit,
boulder,
landmine)
by means of one
grandchild.h
template.