C++
group_lec_vector/catch_and_release.cpp
group_lec_vector/catch_and_release.cpp
#include
<
iostream
>
#include
<
fstream
>
#include
<
vector
>
#include
"Date.h"
int
main
()
{
std
::
vector
<
Date
>
many_dates
;
std
::
ifstream infile
(
"when.txt"
);
if
(
!
infile
.
good
())
{
std
::
cerr
<<
"Can't open file!"
<<
std
::
endl
;
return
1
;
}
int
month
;
int
day
;
int
year
;
std
::
cout
<<
"The vector is currently storing "
<<
many_dates
.
size
()
<<
" dates."
<<
std
::
endl
;
// Read triples of numbers from a file, create Date objects,
// and push them onto the vector
while
(
!
(
infile
>>
month
).
eof
()
)
{
infile
>>
day
;
infile
>>
year
;
//dynamically allocate a Date object
Date
somedate
(
month
,
day
,
year
);
many_dates
.
push_back
(
somedate
);
}
std
::
cout
<<
"The vector now has "
<<
many_dates
.
size
()
<<
" dates."
<<
std
::
endl
;
// Do something with all of those dates. In this case, display each one
for
(
int
i
=
0
;
i
<
many_dates
.
size
();
++
i
)
{
Date
dp
=
many_dates
[
i
];
dp
.
display
();
}
return
0
;
}
group_lec_vector/Date.cpp
group_lec_vector/Date.cpp
#include
"Date.h"
#include
<
iostream
>
using
namespace
std
;
//DATE CLASS DEFINITION
Date
::
Date
(
int
m
,
int
d
,
int
y
)
//constructor
:
month
(
m
),
day
(
d
),
year
(
y
)
//initialization list
{
}
void
Date
::
display
()
{
cout
<<
month
<<
'/'
<<
day
<<
'/'
<<
year
<<
endl
;
}
group_lec_vector/Date.h
#ifndef DATE_H #define DATE_H //DATE CLASS DECLARATION class Date { public: Date(int m, int d, int y); //constructor void display(); private: int month; int day; int year; }; #endif
group_lec_vector/Makefile
c_and_r: Date.o catch_and_release.o g++ Date.o catch_and_release.o -o c_and_r Date.o: Date.cpp Date.h g++ -c Date.cpp catch_and_release.o: catch_and_release.cpp g++ -c catch_and_release.cpp clean: rm -f *.o c_and_r
group_lec_vector/when.txt
6 15 1215 7 4 1776 7 28 1914 9 1 1939 9 2 1945 6 26 1948 1 28 1986