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<
.
+
,
prefix
++
,
postfix
++
,
==
,
!=
,
-
,
<
,
>
,
>=
,
<=
)
to class grade
.
Also add
-
,
prefix --
,
and
postfix --
.
Add some statements to
to class grade
’s
main.C
to test these new functions.