Finally, we learn the stuff that I've really longed for. I had tried to learn templates on my own awhile back, but I couldn't understand from the book.
We've touched on templates and data structures enough that I can understand and learn more on my own now. Here's a sample program using a stack template:
Using vectors (and good form; since vectors move the memory locations of its contents whenever the vector is modified, I employ new and delete in order to be able to retain pointers so that other things may refer to objects in my vector.)
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <vector> //<---add
using namespace std;
/* ... */
vector<Student*>s_stack; //Vector of pointers
//<----> remove t_stack
int Menu();
void AddStudent();
void DeleteStudent();
void ListStudents();
void dealloc(); //<---add
int main() {
/* ... */
dealloc(); //<---add
return 0;
}
int Menu() {
/* ... */
}
void AddStudent() {
/* ... */
Student* student = NULL;
if (!(student = new Student)) {
cerr << "Not enough memory for new student." << endl;
//handle error
}
student->set_all(name, avg, annoyance);
s_stack.push(student);
//the vector now contains a pointer to this new obj.
//dynamically allocated objects aren't removed until you delete them.
}
//This function now faster; don't have to delete and re-write.
void DeleteStudent() {
char name[50];
Student* current; //<--------
vector<Student*>::iterator i; //<---add
printf("Please enter the name of the student that you wish to remove.\n");
getchar();
gets(name);
//<---->remove two lines relating to 'current'.
//replace while loop and if(strcmp()) check w/ the following for loop
for (i = s_stack.begin();i != s_stack.end();++i) {
current = *i;
if (strcmp(current->get_name(), name) == 0) {
delete current;
current = NULL;
s_stack.erase(i);
printf("Student found and destroyed\n");
//that's all we needed; get out.
return;
}
}
printf("Student not found\n");
//<--->remove while loop, no longer have temp stack
}
//This function is now faster, don't have to delete and re-write.
void ListStudents() {
Student* current; //<---modify
vector<Student*>::iterator i;
//replace while loop
for(i = s_stack.begin();i != s_stack.end();++i) {
current = *i;
printf("Student Name - %s\nAverage - %f\nAnnoyance Factor - %d\n",
current->get_name(), current->get_avg(),
current->get_annoyance()); //<--- "." becomes "->"
//<--->remove t_stack.push()
}
//<---->remove re-push while loop.
}
void dealloc() {
vector<Student*>::iterator rmvme;
//rmvme is a pointer to an item in s_stack
//since items in s_stack are pointers, it's a pointer to a pointer.
//in all cases, dereferencing a vector iterator gives you the item
//of the type in brackets, in this case, Student*.
//in this case, rmvme is of type Student**.
for (rmvme = s_stack.begin();rmvme != s_stack.end();++rmvme) {
delete *rmvme;
//deletes what Student* points to, which is correct.
*rmvme = NULL;
}
s_stack.clear();
}
*Edit: forgot /code :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.