data_t
objectsdata_t
object, read a file, and add
data_t
objects to a list (std::vector
).
data_t
class
(object) that will hold the following two data members:
std::string name
int id
data.h
that holds the object interface
and data.cpp
that holds the object implemantation.
main.cpp
test driver program (see below) reads a data
file (redirected from std::cin
), creates a
data_t
object from each pair of string and numbers,
and adds them to a list. It then prints them out and deletes
the list. Once your program successfully compiles (using
the Makefile
provided below), you should be
able to test the program by executing this command on the
command line:
./main < listdata.txt
data_t
object should provide the following:
data_t(std::string iname="",int iid=-1);
data_t(const data_t& rhs);
)
~data_t();
), default ok (can comment this out)
const data_t& operator=(const data_t& rhs);
)
friend istream& operator>>(istream& s, data_t& rhs);
friend ostream& operator<<(ostream& s, const data_t& rhs);
main()
routine works unaltered:
#include <iostream>
#include <string>
#include <vector>
#include "data.h"
int main(int argc, char *argv[]);
int main(int argc, char *argv[])
{
data_t data;
std::vector<data_t> records;
std::vector<data_t>::iterator itr;
std::string name;
int num;
// read input file consisting of names and id codes adding entities to list
while(!std::cin.eof()) {
std::cin >> name >> num;
if(std::cin.good()) {
data.setName(name);
data.setId(num);
// std::cout << data;
records.push_back(data);
}
}
// now play it back
std::cout << "*** printing records" << std::endl;
for(int i=0; i<records.size(); i++) {
// std::cout << records[i] << std::endl;
std::cout << records[i].getName() << " " << records[i].getId() << std::endl;
}
// try it again...
std::cout << "*** printing records again" << std::endl;
for(itr = records.begin(); itr != records.end(); ++itr) {
// data = *itr;
// std::cout << data.getName() << " " << data.getId() << std::endl;
std::cout << itr->getName() << " " << itr->getId() << std::endl;
}
// clear list via STL clear() function (memory leak if data_t used new!!)
std::cout << "*** deleting records" << std::endl;
records.clear();
// nope ... see if we can delete an empty list
std::cout << "*** deleting records again" << std::endl;
records.clear();
// prove we survived
std::cout << "*** done" << std::endl;
}
listdata.txt
file used to test the above program is:
Mike 1234
Bill 3212
Sarah 1321
John 1021
Carol 3223
Debbie 4231
Gary 9321
Ann 1231
Dale 7231
Lynn 8133
Patty 9999
Mike 1234
Bill 3212
Sarah 1321
John 1021
Carol 3223
Debbie 4231
Gary 9321
Ann 1231
Dale 7231
Lynn 8133
Patty 9999
*** printing records
Mike 1234
Bill 3212
Sarah 1321
John 1021
Carol 3223
Debbie 4231
Gary 9321
Ann 1231
Dale 7231
Lynn 8133
Patty 9999
*** printing records again
Mike 1234
Bill 3212
Sarah 1321
John 1021
Carol 3223
Debbie 4231
Gary 9321
Ann 1231
Dale 7231
Lynn 8133
Patty 9999
*** deleting records
*** initializing new records
*** deleting records again
*** done
Makefile
that you can use to compile the project:
.SUFFIXES: .c .o .cpp .cc .cxx .C
UNAME = $(shell uname)
PLATFORM = $(shell uname -p)
CC = g++
COPTS = -g -Wall
INCDIR =
LIBDIR =
ifeq ("$(shell uname)", "Linux")
LIBS = -lm
else
ifeq ("$(shell uname)", "Darwin")
LIBS = -framework Foundation -lstdc++
endif
endif
.c.o:
$(CC) -c $(INCDIR) $(COPTS) -o $@ $<
.cpp.o:
$(CC) -c $(INCDIR) $(COPTS) -o $@ $<
all : main
OBJECTS = \
data.o
main : $(OBJECTS) main.o
$(CC) -o $@ $(INCDIR) $(COPTS) $(OBJECTS) $@.o $(LIBDIR) $(LIBS)
data.o: data.h data.cpp
clean :
rm -f *.o
rm -rf main main.app
tar.gz
archive of your asg##/ directory, including:
README
file containing
Makefile
.h
headers and .cpp
source)
make clean
before tar
)
handin
notes