Hi all,
I've been trying to create a function to display all lecturers in a particular department. The clue given to me by my tutor was to write two versions of this function, one at the universityClass level that invokes a second version at the departmentClass level.
After many failed attempts I wonder, can anybody explain the method to me? I worked out how to print of all the department names, but this seems to be a different kettle of fish!
Code:////////////////////////////////////////////////////////////////////////// // Title : Aggregation and Composition in a University System // Author : Dr Ian van der Linde // Date : 01-04-10 ////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////// // Pre-processor Directives ////////////////////////////////////////////////////////////////////////// #include <iostream> #include <cstdlib> #include <string.h> using namespace std; #define MAXDEPTSPERUNIV 10 #define MAXLECTSPERDEPT 5 #define MAXSTRINGLENGTH 25 ////////////////////////////////////////////////////////////////////////// // Class Definition ////////////////////////////////////////////////////////////////////////// class lecturerClass { private: char lecturerName[MAXSTRINGLENGTH]; public: lecturerClass() { strcpy(lecturerName,""); } char* getName() { return lecturerName; } void setName(char* newName) { strcpy(lecturerName, newName); } }; ////////////////////////////////////////////////////////////////////////// // Class Definition ////////////////////////////////////////////////////////////////////////// class departmentClass { private: char departmentName[MAXSTRINGLENGTH]; int numLecturers; lecturerClass* lecturer[MAXLECTSPERDEPT]; // Aggregation public: departmentClass(); void setDepartmentName(char *newName) { strcpy(departmentName, newName); } char* getDepartmentName() { return departmentName; } void addLecturer(lecturerClass &newLecturer); }; ////////////////////////////////////////////////////////////////////////// // Member Function Implementations ////////////////////////////////////////////////////////////////////////// departmentClass::departmentClass() { strcpy(departmentName, ""); numLecturers=0; } ////////////////////////////////////////////////////////////////////////// void departmentClass::addLecturer(lecturerClass &newLecturer) { lecturer[numLecturers] = &newLecturer; numLecturers++; } ////////////////////////////////////////////////////////////////////////// // Class Definition ////////////////////////////////////////////////////////////////////////// class universityClass { private: departmentClass department[MAXDEPTSPERUNIV]; // Composition int currentNumDepts; string uniName; public: void addLecturer(lecturerClass &lect, int deptcode); void intialiseDepartmentNames(); void setUniversityName( string ); string getUniversityName() const; string getDepartmentName( int ); }; ////////////////////////////////////////////////////////////////////////// // Member Function Implementations ////////////////////////////////////////////////////////////////////////// string universityClass::getDepartmentName( int i ) { return department[ i ].getDepartmentName(); } // end function getDepartmentName ////////////////////////////////////////////////////////////////////////// string universityClass::getUniversityName() const { return uniName; } // end function getUniversityName ////////////////////////////////////////////////////////////////////////// void universityClass::setUniversityName( string a ) { uniName = a; } // end function setUniversityName ////////////////////////////////////////////////////////////////////////// void universityClass::addLecturer(lecturerClass &lect, int deptcode) { department[deptcode].addLecturer(lect); } ////////////////////////////////////////////////////////////////////////// void universityClass::intialiseDepartmentNames() { department[0].setDepartmentName("Engineering"); department[1].setDepartmentName("Computing"); department[2].setDepartmentName("Psychology"); department[3].setDepartmentName("Divination"); department[4].setDepartmentName("Chemistry"); department[5].setDepartmentName("Biology"); department[6].setDepartmentName("Potions"); department[7].setDepartmentName("Economics"); department[8].setDepartmentName("Dark Arts"); department[9].setDepartmentName("Ninja"); currentNumDepts=10; } ////////////////////////////////////////////////////////////////////////// // Main Function Implementation ////////////////////////////////////////////////////////////////////////// int main(void) { // declare an integer variable int userChoice; // create an object of university class universityClass angliaRuskin; // set university class data member uniName angliaRuskin.setUniversityName( "Anglia Ruskin University" ); // get university class data member uniName cout << "Welcome to " << angliaRuskin.getUniversityName() << "!" << endl << endl; // initialise array of department names angliaRuskin.intialiseDepartmentNames(); cout << "Courses on offer:" << endl << endl; // begin a loop to count to 10 for ( int i = 0; i < MAXDEPTSPERUNIV; i++ ) { // return the names of each course in the array cout << i << "\t" << angliaRuskin.getDepartmentName( i ) << endl; } // end for loop // declare 3 lecturer class objects lecturerClass l1, l2, l3; // initialize them l1.setName("Ian van der Linde"); l2.setName("Antony Carter"); l3.setName("Mike Smith"); // add them to the array angliaRuskin.addLecturer(l1, 1); angliaRuskin.addLecturer(l2, 4); angliaRuskin.addLecturer(l3, 8); // ask the user a question cout << "Which course are you interested in?" << endl; // put user response into integer variable userChoice cin >> userChoice; } // end main