The Header File for a Simple Data Class
#include <iostream>
using namespace std;
#ifndef H_DATACLASS
#define H_DATACLASS
class dataClass
{
private:
int age;
string name;
public:
//-----------------------------------------------
// Default Constructor
//
// The name is set to the empty string and the
// age is set to zero.
//-----------------------------------------------
dataClass();
//-----------------------------------------------
// Initialization Constructor
//
// The name and age are set to the specifed values.
//
// In Parameters: newName, newAge
//-----------------------------------------------
dataClass(string newName, int age);
//-----------------------------------------------
// Copy Constructor
//
// The new object has the same name and age as
// obj.
//
// In Parameter: obj
//-----------------------------------------------
dataClass(const dataClass& obj);
//-----------------------------------------------
// getName
//
// Return the name of this object.
//-----------------------------------------------
string getName() const;
//-----------------------------------------------
// getAge
//
// Return the age of this object.
//-----------------------------------------------
int getAge() const;
//-----------------------------------------------
// setName
//
// Set the name of this object to the specified
// value.
//
// In Parameter: newName
//-----------------------------------------------
void setName(string newName);
//-----------------------------------------------
// setAge
//
// Set the age of this object to the specified
// value.
//
// In Parameter: newAge
//-----------------------------------------------
void setAge(int newAge);
//-----------------------------------------------
// Equality operator
//
// Return true if the name field of this object
// is equal to the name field of r2 and false
// otherwise.
//
// In Parameter: r2
//-----------------------------------------------
bool operator == (const dataClass& r2) const;
//-----------------------------------------------
// Less than operator
//
// Return true if the name field of this object
// is less than the name field of r2 and false
// otherwise.
//
// In Parameter: r2
//-----------------------------------------------
bool operator < (const dataClass& r2) const;
//-----------------------------------------------
// Greater than operator
//
// Return true if the name field of this object
// is greater than the name field of r2 and
// false otherwise.
//
// In Parameter: r2
//-----------------------------------------------
bool operator > (const dataClass& r2) const;
//-----------------------------------------------
// displayData
//
// Display the data in this object on the screen
// as one line of text followed by a newline
// character.
//-----------------------------------------------
void displayData() const;
};
#endif
The Implementation File for a Simple Data Class
#include "dataClass.h"
//-----------------------------------------------
// Default Constructor
//
// The name is set to the empty string and the
// age is set to zero.
//-----------------------------------------------
dataClass::dataClass()
{
name = "";
age = 0;
}
//-----------------------------------------------
// Initialization Constructor
//
// The name and age are set to the specifed values.
//
// In Parameters: newName, newAge
//-----------------------------------------------
dataClass::dataClass(string newName, int newAge)
{
name = newName;
age = newAge;
}
//-----------------------------------------------
// Copy Constructor
//
// The new object has the same name and age as
// obj.
//
// In Parameter: obj
//-----------------------------------------------
dataClass::dataClass(const dataClass& obj)
{
name = obj.name;
age = obj.age;
}
//-----------------------------------------------
// getName
//
// Return the name of this object.
//-----------------------------------------------
string dataClass::getName() const
{
return name;
}
//-----------------------------------------------
// getAge
//
// Return the age of this object.
//-----------------------------------------------
int dataClass::getAge() const
{
return age;
}
//-----------------------------------------------
// setName
//
// Set the name of this object to the specified
// value.
//
// In Parameter: newName
//-----------------------------------------------
void dataClass::setName(string newName)
{
name = newName;
}
//-----------------------------------------------
// setAge
//
// Set the age of this object to the specified
// value.
//
// In Parameter: newAge
//-----------------------------------------------
void dataClass::setAge(int newAge)
{
age = newAge;
}
//-----------------------------------------------
// Equality operator
//
// Return true if the name field of this object
// is equal to the name field of r2 and false
// otherwise.
//
// In Parameter: r2
//-----------------------------------------------
bool dataClass::operator == (const dataClass& r2) const
{
return name == r2.name;
}
//-----------------------------------------------
// Less than operator
//
// Return true if the name field of this object
// is less than the name field of r2 and false
// otherwise.
//
// In Parameter: r2
//-----------------------------------------------
bool dataClass::operator < (const dataClass& r2) const
{
return name < r2.name;
}
//-----------------------------------------------
// Greater than operator
//
// Return true if the name field of this object
// is greater than the name field of r2 and
// false otherwise.
//
// In Parameter: r2
//-----------------------------------------------
bool dataClass::operator > (const dataClass& r2) const
{
return name > r2.name;
}
//-----------------------------------------------
// displayData
//
// Display the data in this object on the screen
// as one line of text followed by a newline
// character.
//-----------------------------------------------
void dataClass::displayData() const
{
cout << ' ' << name << ' ' << age << endl;
}
A List Class Implemented Using A Statically Allocated Array
//---------------------------------------------------------
// arrList.cpp
//
// File for a list class based on arrays.
//
// D. Searls
// Jan 2008
//---------------------------------------------------------
#ifndef H_LISTCLASS
#define H_LISTCLASS
#include<cstdlib>
#include<iostream>
using namespace std;
template<class itemType>
class list
{
private:
static const int MAXSIZE = 100;
itemType data[MAXSIZE];
int current;
int n;
public:
//-----------------------------------------------------
// Default Constructor
//
// Constructs an empty list.
//-----------------------------------------------------
list()
{
n = 0;
}
//-----------------------------------------------------
// Copy Constructor
//
// Constructs a list populated with the values in the
// specified list in the same order as they appear in
// the list.
//
// In Parameter: theList
//-----------------------------------------------------
list(const list& theList)
{
for(int i = 0; i < theList.n; i++)
{
data[i] = theList.data[i];
}
n = theList.n;
}
//-----------------------------------------------------
// push_front
//
// Insert the specified item at the front of the list.
//
// In Parameter: item
//-----------------------------------------------------
void push_front(const itemType& item)
{
if (n == MAXSIZE)
{
cout << "The List is Full!" << endl;
exit(0);
}
for (int i = n; i > 0; i--)
{
data[i] = data[i-1];
}
data[0] = item;
n++;
}
//-----------------------------------------------------
// remove
//
// Remove the first element equal to the specified item.
// Equality is based on the result returned by the ==
// operator applied to items in the list.
//
// In Parameter: item
//-----------------------------------------------------
void remove(const itemType& item)
{
int i = 0;
while (i < n && !(item == data[i]))
{
i++;
}
if (i < n) // item was found so remove it
{
while (i < n-1)
{
data[i] = data[i+1];
i++;
}
n--;
}
}
//-----------------------------------------------------
// clear
//
// Remove all of the items from the list.
//-----------------------------------------------------
void clear()
{
n = 0;
}
//-----------------------------------------------------
// size
//
// Return the number of items in the list.
//-----------------------------------------------------
int size()
{
return n;
}
//-----------------------------------------------------
// sort
//
// Sort the values in ascending order based on the
// result returned by the < operator applied to the
// items in the list.
//-----------------------------------------------------
void sort()
{
int start; // index of 1st node in unsorted portion of list
int min; // index of node with minimum value
int curr; // index of current node
itemType temp; // Temporary record used in swapping
start = 0;
while (start != n-1) // More than one item in unsorted portion
{
min = start;
curr = start + 1;
while (curr != n)
{
if (data[curr] < data[min])
{
min = curr;
}
curr = curr + 1;
}
if (min != start)
{
temp = data[start];
data[start] = data[min];
data[min] = temp;
}
start = start + 1;
}
}
//-----------------------------------------------------
// find
//
// Return a pointer to the first element in the list
// that is equal to the specified item based on the ==
// operator applied to items in the list. If there is
// no element equal to the item,
// return NULL.
//
// In Parameter: item
//-----------------------------------------------------
itemType* find(const itemType& item)
{
int curr; // Pointer to current node
curr = 0;
while (curr != n && !(data[curr] == item))
{
curr = curr + 1;
}
return itemAt(curr);
}
//-----------------------------------------------------
// begin
//
// Return a pointer to the first item in the list or
// end() if the list is empty.
//
// The functions begin(), next(), and end() are meant
// to be used to perform a linear traversal of the list.
//-----------------------------------------------------
itemType* begin()
{
current = 0;
return itemAt(current);
}
//-----------------------------------------------------
// next
//
// Return a pointer to the next item in the list or
// end() if there is no next item.
//
// The functions begin(), next(), and end() are meant
// to be used to perform a linear traversal of the list.
//-----------------------------------------------------
itemType* next()
{
if (current != n)
{
current = current + 1;
}
return itemAt(current);
}
//-----------------------------------------------------
// end
//
// Return a pointer just past the last element in the
// list.
//
// The functions begin(), next(), and end() are meant
// to be used to perform a linear traversal of the list.
//-----------------------------------------------------
itemType* end()
{
return NULL;
}
private:
//-----------------------------------------------------
// itemAt
//
// Return a pointer to the information at the specified
// location. If the location is NULL then this method
// returns a NULL pointer.
//
// Pre-condition: location points to an existing node
// in the list or is NULL.
//
// In Parameter: location
//-----------------------------------------------------
itemType* itemAt(int location)
{
itemType* ptr;
if (location != n)
{
ptr = &(data[location]);
}
else
{
ptr = end();
}
return ptr;
}
};
#endif
An Application to Test the List Class
//---------------------------------------------------------
// File: arrListTest.cpp
//
// Purpose: test the list class implementation.
//
// D. Searls
// Asbury College
// Jan 2008
//---------------------------------------------------------
#include <iostream>
#include <fstream>
#include <string>
#include "arrList.cpp"
#include "dataClass.h"
using namespace std;
//-----------------------------------------------
// Function: readData
//
// Purpose: read data from a text file placing
// faculty records in the fac list and student
// records in the student list. The file name
// is "people.txt".
//
// Pre-conditions: fac and stu are empty lists.
// There are no incomplete records in the
// data file.
//
// Post-conditions: fac is a list of faculty
// data and stu is a list of student data.
//
// Out parameters: fac, stu
//-----------------------------------------------
void readData(list<dataClass>& fac, list<dataClass>& stu)
{
ifstream infile; // input file
dataClass onePerson; // one person's data
char statusCode;
string name;
int age;
infile.open("people.txt");
if (!infile.fail())
{
infile >> statusCode;
while (!infile.fail())
{
infile >> age;
onePerson.setAge(age);
getline(infile, name);
onePerson.setName(name);
if (statusCode == 'F')
{
fac.push_front(onePerson);
}
else
{
stu.push_front(onePerson);
}
infile >> statusCode;
}
}
else
{
cout << "Unable to open the input file. Program aborted!" << endl;
exit(0);
}
}
//-----------------------------------------------
// display
//
// Display the contents of myList on the screen.
//
// In Parameter: myList
//-----------------------------------------------
void display(list<dataClass>& myList)
{
dataClass* ptr;
ptr = myList.begin();
while (ptr != myList.end())
{
cout << " " << ptr->getName() << " " << ptr->getAge() << endl;
ptr = myList.next();
}
}
//-----------------------------------------------
// update
//
// Update myList by adding one to the age field
// of each record.
//
// In/Out Parameter: myList
//-----------------------------------------------
void update(list<dataClass>& myList)
{
dataClass* ptr;
ptr = myList.begin();
while (ptr != myList.end())
{
ptr->setAge(ptr->getAge() + 1);
ptr = myList.next();
}
}
//---------------------------------------------------------
// M a i n D r i v e r
//---------------------------------------------------------
int main()
{
list<dataClass> faculty; // List of faculty
list<dataClass> students; // List of students
dataClass facRecord; // Faculty record
dataClass* ptr;
string name;
readData(faculty, students);
// Display the two lists.
cout << faculty.size() << " Faculty:" << endl;
display(faculty);
cout << endl;
cout << students.size() << " Students:" << endl;
display(students);
cout << endl;
// Sort lists and redisplay them.
students.sort();
cout << "Students sorted:" << endl;
display(students);
cout << endl;
faculty.sort();
cout << "Faculty sorted:" << endl;
display(faculty);
cout << endl;
// Update faculty ages and display updated list.
update(faculty);
cout << "Updated faculty list:" << endl;
display(faculty);
cout << endl;
// Search faculty list
cout << "Search faculty list by name. ";
cout << "Searching terminates when search fails.\n\n";
cout << "Enter name of faculty person: ";
getline(cin, name);
facRecord.setName(name);
ptr = faculty.find(facRecord);
while (ptr != faculty.end())
{
cout << ptr->getName() << " is " << ptr->getAge() << endl;
cout << "Enter name of faculty person: ";
getline(cin, name);
facRecord.setName(name);
ptr = faculty.find(facRecord);
}
cout << '\'' << name << "' not found." << endl << endl;
cout << endl << endl;
// Clear both lists
faculty.clear();
students.clear();
cout << "After clearing both lists:" << endl;
cout << " Size of faculty list: " << faculty.size() << endl;
cout << " Size of student list: " << students.size() << endl << endl;
// Now we'll repopulate a list and test the remove function
// by removing the first element, the last element, and an
// element in between the first and last.
for (char ch = 'f'; ch >= 'a'; ch--)
{
facRecord.setName(string(5, ch));
facRecord.setAge(static_cast<int>(ch));
faculty.push_front(facRecord);
}
// Display list
cout << "New faculty list:" << endl;
display(faculty);
cout << "Size (length) of faculty list: " << faculty.size() << endl;
cout << endl;
// Delete first item and display resulting list
facRecord.setName(string(5, 'a'));
faculty.remove(facRecord); // Remove first element in list
cout << "List after removing \"" + facRecord.getName() + "\":" << endl;
display(faculty);
cout << "Size (length) of faculty list: " << faculty.size() << endl;
cout << endl;
// Delete last item and display resulting list
facRecord.setName(string(5, 'f'));
faculty.remove(facRecord); // Remove last element in list
cout << "List after removing \"" + facRecord.getName() + "\":" << endl;
display(faculty);
cout << "Size (length) of faculty list: " << faculty.size() << endl;
cout << endl;
// Remove an element other than first or last and display resulting list
facRecord.setName(string(5, 'c'));
faculty.remove(facRecord); // Remove element between first and last
cout << "List after removing \"" + facRecord.getName() + "\":" << endl;
display(faculty);
cout << "Size (length) of faculty list: " << faculty.size() << endl;
cout << endl;
// Try to remove an item not in the list and display resulting list
facRecord.setName(string(5, 't'));
faculty.remove(facRecord); // Try to remove item not in list
cout << "List after removing \"" + facRecord.getName() + "\":" << endl;
display(faculty);
cout << "Size (length) of faculty list: " << faculty.size() << endl;
cout << endl;
//*******************************************
// Test list class with integers
//*******************************************
list<int> intList; // List of integers;
int* intPtr;
// Populate the list
for (int i = 1; i <= 10; i++)
{
intList.push_front(100*i);
}
// Display the list
for (intPtr = intList.begin(); intPtr != intList.end(); intPtr = intList.next())
{
cout << *intPtr << " ";
}
cout << endl;
cout << "Size of intList is " << intList.size() << endl << endl;
// Sort the list and display the resulting list
intList.sort();
for (intPtr = intList.begin(); intPtr != intList.end(); intPtr = intList.next())
{
cout << *intPtr << " ";
}
cout << endl;
cout << "Size of intList is " << intList.size() << endl << endl;
// Remove some elements and display resulting list
intList.remove(1000);
intList.remove(100);
intList.remove(300);
intList.remove (800);
for (intPtr = intList.begin(); intPtr != intList.end(); intPtr = intList.next())
{
cout << *intPtr << " ";
}
cout << endl;
cout << "Size of intList is " << intList.size() << endl << endl;
return 0;
}