weatherreport.C,
weatherreport.txt.
weatherreport1.C,
weatherreport1.txt.
temp_High,
temp_Low,
forecast)
with a single array of structures, named
a.
We saw an array of structures
here.
I made the
output
more legible by printing each weather report in a single line,
with the columns lined up.
weatherreport2.C,
weatherreport2.txt.
main
function of
weatherreport1.C,
and the seven remaining weather reports in the
weather::nextDay
function of
weatherreport1.C,
I output all eight weather reports in one place
(in the
main
function of
weatherreport2.C).
weatherreport3.C,
weatherreport3.txt.
weather.
Instead of creating one
weather
object and then updating it seven times,
the main
function in
weatherreport3.C
creates a series of seven short-lived
weather
objects, each one a
const.
Constants are safer than variables.
Excerpts from
time_example.C:
class Time {
public:
//Declarations for the data members:
int hour; //in the range 0 to 23 inclusive
int minute; //in the range 0 to 59 inclusive
int second; //in the range 0 to 59 inclusive
//Declarations for the member functions:
void print() const; //const member function
void next(int n); //non-const member function
void next();
};
void Time::print() const //This member function can't change the Time object.
{
cout << (hour < 10 ? "0" : "") << hour << ":"
<< (minute < 10 ? "0" : "") << minute << ":"
<< (second < 10 ? "0" : "") << second;
}
Easier to do it with the i/o manipulators
setfill
and
setw.
'Single
quotes'
around a value of type
char.
#include <iomanip> //for the i/o manipulators setw and setfill
void Time::print() const //This member function can't change the Time object.
{
cout << setfill('0')
<< setw(2) << hour << ":"
<< setw(2) << minute << ":"
<< setw(2) << second;
}
exercise.C,
exercise.txt
The above program is a modification of
obj2.C.
//Return a random int in the range small to big, inclusive.
int inrange(int small, int big)
{
const int n {big - small + 1}; //number of values in the range
return small + rand() % n;
}
obj2a.C
We saw the keyword static
inside of a function in
static.C.
In
ear7’s
objRev.C,
the day before June 1, 2025 should be May 31, 2025.
objRev2.C,
objRev2.txt