#include using namespace std; #include template void sortByName(T *ta, int count); class Person { protected: string name; int age; public: Person() {} Person(string n, int a) { name = n; age = a; } string getName() { return name; } virtual void print() { cout << "Name: " << name << endl; cout<< "Age: " << age << endl; } }; class Teacher : public Person { private: int salary; public: Teacher() {} Teacher(string n, int a, int s) : Person(n, a) { salary = s; } int getSalary() { return salary; } void setSalary(int s) { salary = s; } virtual void print() { Person::print(); cout<< "Salary: " << salary << endl; } }; class Student : public Person { private: char grade; double fees; public: Student() {} Student(string n, int a, char g, double f) : Person(n, a) { grade = g; fees = f; } char getGrade() { return grade; } double getFees() { return fees; } void setgrade(char g) { grade = g; } void setFees(double f) { fees = f; } virtual void print() { Person::print(); cout<< "Grade: " << grade << endl; cout<< "Fees: " << fees << endl; } }; void main() { Person *personArr[4]; Student *sa[4]; Teacher *ta[4]; int sindx=0; int tindx=0; string name; int age; char grade; double fees; //int salary; char type; for (int i = 0; i < 4; i++) { cout << "Please specify the type the Person (t\\T or s\\S): "; cin >> type; if (type == 't' || type == 'T') { cout << "Enter the Teacher's name: "; cin >> name; cout << "Enter the Teacher's age: "; cin >> age; cout << "Enter the teacher's salary: "; int salary; cin >> salary; Teacher *t = new Teacher(name, age, salary); //personArr[i] = (Person *)tObj; personArr[i] = t; ta[tindx++]= t; } else if (type == 's' || type == 'S') { cout << "Enter the Student's name: "; cin >> name; cout << "Enter the Student's age: "; cin >> age; cout << "Enter the student's grade: "; cin >> grade; cout << "Enter the student's fees: "; cin >> fees; Student *s = new Student(name, age, grade, fees); personArr[i] = s; sa[sindx++] = s; } else { cout << "Invalid type; try again.\n"; i--; } cout << endl; } cout << "The entered info is:\n"; for (int i = 0; i < 4; i++) { personArr[i]->print(); cout << endl; } sortByName(ta, tindx); cout << "The sorted Teachers array is:\n"; for (int i = 0; i < tindx; i++) { ta[i]->print(); cout << endl; } sortByName(sa, tindx); cout << "The sorted Students array is:\n"; for (int i = 0; i < tindx; i++) { sa[i]->print(); cout << endl; } } template void sortByName(T ta[4], int count) { T tmp; for (int i = 0; i < count-1; i++) { for (int j = 0; j < count-i-1; j++) { if(ta[j]->Person::getName()>ta[j+1]->getName()) { tmp = ta[j]; ta[j]=ta[j+1]; ta[j+1] = tmp; } } } }