We will need to know how to do this when we make our first C++ class,
class
date.
theTime.C,
theTime.txt
tm_sec
might be a leap second.)
to_time_t
function is the number of seconds that have elapsed
since the ever-memorable night they invented Unix
(the original version of Linux),
at midnight on January 1, 1970.
60 * 60 * 24 * 365.25
to convert seconds to years:
bc -l (minus lowercase L for the math library of the "binary calculator") 1769720672 / (60 * 60 * 24 * 365.25) 56.07906406063832484092 control-dThe function
localtime
fills up a structure with information,
and returns a pointer to this structure.
tm_sec
might be a leap second.)
Pass some information
(the trio of integers
year,
month,
day)
down to the functions
(print,
next,
etc.)
that do all the work on the dates.
3ints.C,
3ints.txt
ints.
date_print
could simply have been of type
int,
int,
int.
date_
functions.
struct.C,
struct.txt
struct
instead of in three separate
ints.
++p->day
in the one-argument function
next,
the
p
is an operand of the
->,
not an operand of the
++.
->
(at level 2)
has higher precedence than the
prefix
++
operator
(at level 3).
obj1.C,
obj1.txt.
obj2.C,
obj2.txt.
date::next
and the four-argument
date::next).
const date&
is called the
copy constructor.
main
function.
date
has a constructor that checks for errors,
what would happen if the
main
function tried to create a
date d {2, 30, 2026}; //Bad date: there is no February 30
year,
month,
day
inside of the member functions of class
date.
But now that these data members
have become private,
what would happen if the
main
function tried to say
cout << "America became independent in " << independenceDay.year << ".\n";
date
in
obj2.C.
Even if we don’t write this constructor,
the computer will still behave as if we had.
date.
This constructor will have no explicit arguments (i.e., arguments you can see),
and will initialize the newborn
date
object to today’s date.
A constructor with no explicit arguments is called a
default
constructor.
The default constructor for class
date
will have to call the three functions
chrono::system_clock::now,
chrono::system_clock::to_time_t,
and
localtime;
the
main
function will no longer have to call them.
Test the default constructor by changing the declaration for
today
in the
main
function of
obj2.C
to
const date today; //Call the default constructor. today.print(); //Should output today's date. cout << "is today.\n";
const
public member functions for the class
date
in
obj2.C
named
prev
and
prev.
They will be just like the existing member functions
next and
next,
except that they will move the
date
object into the past instead of the future.
Test the new member functions by calling them in
main.
int:
//Bad. int i; //Initialize i to unpredictable garbage. i = 10; //Assign a value to i, replacing the garbage.
//Better.
int i {10}; //Initialize i to 10.
Now with data members in a constructor:
//Bad.
//This constructor for class date initializes the three data members of the
//newborn object to unpredictable garbage. Then it assigns values to the
//data members, replacing the garbage.
date::date(int init_month, int init_day, int init_year)
{
year = init_year;
if (init_month < 1 || init_month > 12) {
cerr << "Bad month " << init_month << "\n";
exit(EXIT_FAILURE);
}
month = init_month;
if (init_day < 1 || init_day > length[month]) {
cerr << "Bad month " << init_month << " and day " << init_day
<< "\n";
exit(EXIT_FAILURE);
}
day = init_day;
}
//Better.
//This constructor for class date initializes the three data members of the
//newborn object to the values supplied by the user, init_year, init_month,
//init_day.
date::date(int init_month, int init_day, int init_year)
: year {init_year}, month {init_month}, day {init_day}
{
if (init_month < 1 || init_month > 12) {
cerr << "Bad month " << init_month << "\n";
exit(EXIT_FAILURE);
}
if (init_day < 1 || init_day > length[month]) {
cerr << "Bad month " << init_month << " and day " << init_day
<< "\n";
exit(EXIT_FAILURE);
}
}
obj3.C,
obj3.txt.
date
initializes the data members of the newborn
date
object,
instead of filling them with unpredictable garbage and then assigning to them.
monthsInYear
does not need to receive the address of any particular object of class
date.
void print() const; //We already have this declaration. static int monthsInYear(); //Declaration for a new member function. //etc.
//Definition of a static member function of class date.
int date::monthsInYear()
{
return size(length) - 1; //number of months in a year
}
//Test the static member function in the main fuction. cout << "A year has " << date::monthsInYear() << " months.\n";
Use the new static member function
date::monthsInYear
in the following two existing member functions:
//In the three-argument constructor of class date
if (month < 1 || month > monthsInYear()) {
cerr << "Bad month " << init_month << "\n";
//etc.
//In the no-argument next member function of class date
if (month < monthsInYear()) {
++month;
//etc.
date
in many different C++ programs,
divide the program into the following three files,
and put them in the same directory.
Use the
mkdir
command to create the directory.
date.h
is the
header file
for class
date.
#
lines at the start and end of this file.
date.C
is the
implementation file
for class
date.
date.h.
main.C,
main.txt
contains the rest of the C++ program that uses objects of class
date.
date.h.
cd cd public_html pwd /home/students/jsmith/public_html mkdir date ls -ld date drwxr-xr-x 2 jsmith students 6 Jan 29 20:54 date cd date pwd /home/students/jsmith/public_html/date wget https://markmeretzky.com/fordham/2000/src/class/date/date.h wget https://markmeretzky.com/fordham/2000/src/class/date/date.C wget https://markmeretzky.com/fordham/2000/src/class/date/main.C ls -l (minus lowercase L) -rw-r--r-- 1 jsmith students 2067 Jan 29 19:18 date.C -rw-r--r-- 1 jsmith students 708 Jan 29 20:26 date.h -rw-r--r-- 1 jsmith students 816 Jan 29 19:17 main.C c++ -o ~/bin/mydate date.C main.C (minus lowercase O) ls -l ~/bin/mydate (minus lowercase L) -rwxr-xr-x 1 jsmith students 14440 Jan 28 21:12 /home/students/jsmith/bin/mydate mydate A year has 12 months. 7/4/1776 is Independence Day. etc. echo $? (See the (normally invisible) exit status producted by the program.) 0
#include
with
<angle brackets>
looks in the directory
/usr/include/c++/15
(on our machine
storm.cis.fordham.edu)
for the header file.
#include
with
"double quotes"
looks in your current directory for the header file.
date.h,
the same header file as abovedate.C,
the same implementation file as abovemain2.C,
main2.txt,
a different main filemain
function in this program has an array of objects.
Compile this program with
c++ -o ~/bin/main2 main2.C date.C ls -l ~/bin/main2 main2 1/15/2026
The data members are private,
so no one other than the member functions of class
date
needs to know that the data members have changed.
Do not change the three arguments of the constructor,
or the output of the
print
member function,
even though the class no longer has three data members.
date
with two data members,
year
and
day.
date
with one data member,
day.
distance
function
to the one-data-member class
date.
date.h:
declare
distance
as member function vs. a friend function.
date.C:
define
distance
as member function vs. a friend function.
main.C,
main.txt:
exactly the same output for member function vs. friend function.
My recommendations: if a function needs to mention the private members of a class,
date::print
in
date.h
and
date.C,
point::print
in
point.h
and
point.C.
operator<<
and
operator>>
must be friends,
even though each once concentrates on one object of the class.
friend
function of the class if it deals with two or more objects of the class.
date::distance
in
date.h
and
date.C,
and (coming up)
point::equals
in
point.h
and
point.C.
date::monthsInYear
and
announcer::howMany
in
announcer.h
and
announcer.C.
Like a
date
object,
we can think of a
point
object primarily as a structure that holds data members.
This program consists of three files:
cd cd public_html pwd /home/students/jsmith/public_html mkdir point ls -ld point drwxr-xr-x 2 jsmith students 6 Jan 29 20:54 point cd point pwd /home/students/jsmith/public_html/point wget https://markmeretzky.com/fordham/2000/src/class/point/point.h wget https://markmeretzky.com/fordham/2000/src/class/point/point.C wget https://markmeretzky.com/fordham/2000/src/class/point/main.C ls -l -rw-r--r-- 1 jsmith students 447 Jun 10 2025 main.C -rw-r--r-- 1 jsmith students 1051 Jun 10 2025 point.C -rw-r--r-- 1 jsmith students 477 Jun 10 2025 point.h c++ -o ~/bin/point main.C point.C ls -l ~/bin/point point Length of line AB is 5 etc.
point
a
friend
function named
midpoint,
that will construct and return a new
point
that is exactly halfway between two existing points.
point.h;
see the declaration of the
friend
function
distance
in this file
date.h.
point.C;
you have to fill in the two (underlined) blanks.
//in the file point.C
point midpoint(const point& A, const point& B) //a friend function
{
const point m { , ); //Construct the midpoint ...
return m; //...and return it.
}
You’ll also have to test
midpoint
in the file
main.C.
This file already has two
point
objects,
A
and
B.
//in the main function in the file main.C
const point M {midpoint(A, B)}; //Call the copy constructor for class point.
cout << "The midpoint of A and B is ";
M.print();
cout << "\n";
point
a static data member named
origin of type
const point.
(See
date.h
and
date.C
for the declaration and definition of a static data member.)
Then change the body of the
point::r
member function to
//return sqrt(x * x + y * y); return distance(*this, origin); //distance from this point to the origin
date
and
point,
an object of class
announcer
holds very little data:
just an identifying number for itself.
announcer
object is not to hold data.
Class
announcer
has a static data member
announcer::count,
like the static data member
date::length
of class
date.
Class
announcer
has a static member function
announcer::howmany,
like the static member function
date::monthsInYear.
A static member function does not receive an implicit argument,
since a static member function does not use the data members
inside of any object.
(An “implicit” argument is an invisible argument.)
Class
announcer
as a copy constructor whose explicit argument is a
const announcer& another,
just like the
obj2.C
version of class
date
had a copy constructor whose explicit argument was a
const date& another.
(An “explicit” argument is an argument you can see,
as opposed to the invisible argument that is a pointer to an object.)
We had to write a copy constructor for class
announcer
because the copy constructor we’d get by default would do no more
than initialize the non-static
data member:
announcer::announcer(const announcer& another) //"copy" constructor
: name {another.name.id}
{
}
Download, compile, and run this three-file program:
cd cd public_html pwd /home/students/jsmith/public_html mkdir announcer ls -ld announcer drwxr-xr-x 2 jsmith students 6 Feb 5 20:54 announcer cd announcer pwd /home/students/jsmith/public_html/announcer wget https://markmeretzky.com/fordham/2000/src/class/announcer/announcer.h wget https://markmeretzky.com/fordham/2000/src/class/announcer/announcer.C wget https://markmeretzky.com/fordham/2000/src/class/announcer/announcer.C ls -l -rw-r--r-- 1 jsmith students 657 Feb 5 2025 announcer.C -rw-r--r-- 1 jsmith students 521 Feb 5 2025 announcer.h -rw-r--r-- 1 jsmith students 1155 Feb 5 2025 main.C c++ -o ~/bin/announcer main.C announcer.C ls -l ~/bin/announcer announcer announcer 10 is born. etc.
main2.C
to demonstrate where C++ objects are born and die.
cd ~/public_html/announcer pwd wget https://markmeretzky.com/fordham/2000/src/class/announcer/main2.C ls -l main2.C c++ -o ~/bin/announcer main2.C announcer.C ls -l ~/bin/announcer announcer At start of main, we have 0 objects.
new
and
delete[]
a block of memory in C++
(a typical resource)constructor.c.
fopen
and
fclose.
cc
instead of
c++.
cc constructor.c (Should create a file named a.out) ls -l a.out -rwxr-xr-x 1 jsmith students 25048 Jun 1 09:18 a.out ./a.out (Should create a file named outfile.txt) echo $? (See the exit status producted by a.out) 0 ls -l outfile.txt -rw-r--r-- 1 jsmith students 16 Jun 1 09:26 outfile.txt cat outfile.txt (See what's in the file outfile.txt) Morning. Noon. Night. rm outfile.txt
constructor.C.
outfile.txt
file as the above C program.
#include <iostream>
#include <cstdlib>
#include "window.h" //for classes window and icon
using namespace std;
int main()
{
window w; //Construct window w.
icon i0 {w}; //Construct icon i0 and put it in the window.
icon i1 {w}; //Construct icon i1 and put it in the window.
icon i2 {w}; //Construct icon i2 and put it in the window.
doWork();
return EXIT_SUCCESS; //Destruct i2, i1, i0, w, in that order.
}
element.
{curly
brace}
discipline,
we can construct each object in its own block of dynamically allocated memory.
ints
in
new1.C.)
new,
the computer automatically calls the constructor
for any object(s) in the block.
new1.C,
because the block in
new1.C
held pathetic little integers, not actual objects.)
delete,
the computer automatically calls the destructor for any object(s)
in the block.
m3
is constructed after
m1,
but is destructed
after
m1.
c++ main4.C announcer.C ls -l a.out ./a.out m1 (announcer number 1) born. etc.
interval)
containing two smaller objects
(dates)
as its data members.
interval
begins by automatically making two detours to a constructor for class
date.
{body}
of the constructor for class
interval
is executed.
Similarly, the destructor for class
interval
ends
by automatically making two detours to the
destructor for class
date.
Before these detours,
the
{body}
of the constructor for class
interval
is executed.
date.h,
date.C,
interval.h,
interval.C,
and
main.C)
with
c++ -o ~/bin/interval main.C interval.C date.C ls -l ~/bin/interval interval constructing date 9/1/2025 etc.
stack
object is hardwired to hold a stack of
ints.
A stack
is what an accountant would call a LIFO list:
“last in, first out”.
Compile and run this three-file program (consisting of
stack.h,
stack.C,
and
main.C)
with
c++ -o ~/bin/stack main.C stack.C ls -l ~/bin/stack stack Type a positive integer to push it, etc.
stack.C,
stack.txt.
stack,
like the template class
vector
we saw in
vectorint1.C.
<angle
brackets>.
On our machine
storm.cis.fordham.edu,
an
int
occupies 4 bytes = 32 bits.
Therefore an
int
can hold any one of
232 = 4,294,967,296
different values.
We let these values represent integers in the range from
–2,147,483,648
to
2,147,483,647 inclusive.
The leftmost bit of an
int
is called the
sign bit.
It is
1
for negative numbers,
0
for non-negative numbers.
| bits | int |
01111111 11111111 11111111 11111111 |
2,147,483,647 |
01111111 11111111 11111111 11111110 |
2,147,483,646 |
01111111 11111111 11111111 11111101 |
2,147,483,645 |
| ⋮ | ⋮ |
00000000 00000000 00000000 00000010 |
2 |
00000000 00000000 00000000 00000001 |
1 |
00000000 00000000 00000000 00000000 |
0 |
11111111 11111111 11111111 11111111 |
–1 |
11111111 11111111 11111111 11111110 |
–2 |
11111111 11111111 11111111 11111101 |
–3 |
| ⋮ | ⋮ |
10000000 00000000 00000000 00000010 |
–2,147,483,646 |
10000000 00000000 00000000 00000001 |
–2,147,483,647 |
10000000 00000000 00000000 00000000 |
–2,147,483,648 |
On our machine
storm.cis.fordham.edu,
an unsigned int
occupies 4 bytes = 32 bits.
Therefore an
unsigned int
can hold any one of
232 = 4,294,967,296
different values.
We let these values represent integers in the range from
0
to
4,294,967,295 inclusive.
| bits | unsigned int |
11111111 11111111 11111111 11111111 |
4,294,967,295 |
11111111 11111111 11111111 11111110 |
4,294,967,294 |
11111111 11111111 11111111 11111101 |
4,294,967,293 |
| ⋮ | ⋮ |
00000000 00000000 00000000 00000010 |
2 |
00000000 00000000 00000000 00000001 |
1 |
00000000 00000000 00000000 00000000 |
0 |
The member function
myrandom::rand
in the file
myrandom.C
scrambles the value of the data member
myrandom::next
with a multiplication and an addition.
For example, the first time we call
myrandom::random,
it changes the value of
next
from 1 to
1,103,527,590.
Mathematicians have determined that the most random part
of the resulting value
consists of the 15 bits in positions 16 through 30 inclusive.
Here is 1,103,527,590 written in binary with these bits
in yellow:
01000001110001100111111010100110 (this is 1,103,527,590)
00000000000000000100000111000110
(this is 1,103,527,590 shifted 16 places to the right)
& 00000000000000000111111111111111
(this mask is 0x7FFF)
00000000000000000100000111000110
(this is 16,838)
The result of the “bitwise and” is
100000111000110
which is 16,838 in decimal.
myrandom
object does only one thing for us.
In other words,
it has only one member function,
not counting the constructor.
date
jt88)
Constructor takes degrees, minutes, seconds.
// An array of 3 Angle objects
const Angle triangle[] = {
Angle {90, 0, 0},
Angle {45, 0, 0},
Angle {45, 0, 0}
};
const size_t n {size(triangle)}; //the number of objects in the array
jc208)
coffee
kwesner1)
zclass.
element2
mrd22)
MilitaryRank
(jmm56)