// File: main7.C // // Testing the Person class #include #include #include "person.h" void SortByAge(Person **, int) ; void SortByAge(Person **A, int n) { int i, j, min, mindex ; Person* temp ; for(i = 0 ; i < n - 1 ; i++) { min = A[i]->age ; mindex = i ; for( j = i + 1; j < n ; j++) { if (A[j]->age < min) { min = A[j]->age ; mindex = j ; } } temp = A[i] ; A[i] = A[mindex] ; A[mindex] = temp ; } } int main() { int i ; Person **A = new Person * [5] ; A[0] = new Person("John Smith", 30) ; A[1] = new Person("Jane Doe", 27) ; A[2] = new Person("Groucho Marx", 40) ; A[3] = new Person("Tiny Tim", 6) ; A[4] = new Person("Bugs Bunny", 36) ; cout << "Sorting Persons:" << endl ; SortByAge(A, 5) ; for (i=0 ; i < 5 ; i++) { A[i]->id() ; } Person **B = new Person * [5] ; B[0] = new Student("John Smith", 30, 1, 3.2) ; B[1] = new Student("Jane Doe", 27, 2, 3.4) ; B[2] = new Student("Groucho Marx", 40, 4, 2.1) ; B[3] = new Student("Tiny Tim", 6, 0, 3.0) ; B[4] = new Student("Bugs Bunny", 36, 0, 1.2) ; cout << "\nSorting Students as Persons:" << endl ; SortByAge(B, 5) ; for (i=0 ; i < 5 ; i++) { B[i]->id() ; } }