********* Message from Amity Warren ************** Looking at my program another time I realized that the compiler didn't seem to need my operator overloading =. I did it for the Book class, but didn't do it for the magazine class - and yet both yet. It seems that this is because the compiler automatically does a binary copy of an object, and so it works. Wahoo! ************************************************** Yes, you don't need the assignment operator in this case, because the default version created by the compiler works fine. -hiroya ------------------- DBLIB.CPP follows -------------------- /* Library Book and Magazine Database main file * by Amity Warren */ #include #include #include #include #include "dblib.h" void sorta(Book* X) { int status = 1; while (status != 0) { status = Switcha(X); } } int Switcha(Book* X) { Book temp; int switched = 0, i = 0; while (i < NUMITEMS - 1) { if (X[i].author > X[i + 1].author) { temp = X[i]; // swap two values X[i] = X[i + 1]; X[i + 1] = temp; switched++; } i++; // advance to next book in array } return switched; } int main(int argc, char** argv) { if (argc != 2) { cout << "\n Usage: " << argv[0] << " infile\n"; exit(1); } ifstream f_in(argv[1]); // error checking if (!f_in) { cerr << "Cannot open file: " << argv[1] << endl; exit(1); } cout << "Book and Magazine Library Database program\n"; cout << "By Amity Warren \n"; Book blib[NUMITEMS]; Magazine mlib[NUMITEMS]; int i; char temp[100]; cout << "Unordered data set follows: " << endl; // read in books for (i = 0; i < NUMITEMS; i++) { f_in.getline(temp, 100); // throw away, marker line for my benefit f_in.getline(temp, 100); blib[i].author = temp; f_in.getline(temp,100); blib[i].title = temp; f_in.getline(temp,100); blib[i].subject = temp; f_in.getline(temp,100); blib[i].ISBN = temp; blib[i].print(); } // read in magazines for (i = 0; i < NUMITEMS; i++) { f_in.getline(temp, 100); // throw away, marker line for my benefit f_in.getline(temp, 100); mlib[i].title = temp; f_in.getline(temp,100); mlib[i].date = temp; f_in.getline(temp,100); mlib[i].subject = temp; f_in.getline(temp,100); mlib[i].ref = temp; mlib[i].print(); } cout << "Books sorted by Title" << endl; sort(blib); for (i = 0; i < NUMITEMS; i++) { blib[i].print(); } cout << "\nMagazines sorted by Title" << endl; sort(mlib); for (i = 0; i < NUMITEMS; i++) { mlib[i].print(); } sorta(blib); cout << "\nBooks sorted by author" << endl; for (i = 0; i < NUMITEMS; i++) { blib[i].print(); } cout << "Done"; return 0; } ------------------- DBLIB.H follows -------------------- /* Database header file * Homework assignment #4 * By Amity Warren */ #ifndef Dblib_h #define Dblib_h #define NUMITEMS 8 typedef enum boolean {false = 0, true = 1}boolean; class Item { public: string title; string subject; Item(){}; Item(const Item &x){}; // implement this ~Item(){}; void print() {cout << "\nSubject : "<< subject<< endl << title;} }; class Book:public Item { public: string author; string ISBN; Book(){}; ~Book(){}; void print(); Book& operator=(const Book& b); }; class Magazine: public Item { public: string date; string ref; Magazine(){}; ~Magazine(){}; void print(); }; void Book :: print() { Item :: print(); cout << " by " << author << endl; cout << "ISBN : " << ISBN << endl; } Book& Book :: operator=(const Book& b) { title = b.title; subject = b.subject; author = b.author; ISBN = b.ISBN; return(*this); } void Magazine :: print() { Item :: print(); cout << "\nDate published : " << date << endl; cout << ref << endl; } template void sort(T* X); template int Switch(T* X); void sorta(Book* X); int Switcha(Book* b); template void sort(T* X) { int status = 1; while (status != 0) { status = Switch(X); } } template int Switch(T* X) { T temp; int switched = 0, i = 0; while (i < NUMITEMS - 1) { if (X[i].title > X[i + 1].title) { temp = X[i]; // swap two values X[i] = X[i + 1]; X[i + 1] = temp; switched++; } i++; // advance to next book in array } return switched; } #endif