The idea of this program is to create an array of pointers to objects of the per
ID: 3801278 • Letter: T
Question
The idea of this program is to create an array of pointers to objects of the person class. The program can sort a group of person objects based on the alphabetical order of their names or the numerical order of their salary.
Requirements:
1. Create class called person with the following property and methods:
- name
- salary
- void setPerson() //input name and salary
- string getName()
- float getSalary()
2. Create a function called bsort to sort pointers to objects.
- void bsort(person **, int n, bool s) n is an the number of person objects stored in the array s is true, then sort by name;otherwise, sort by salary.
- order(person**, person**) //orders two pointers. In the the function bsort, call the function order to swap array elements for sorting.
- In the function bsort, call the function order to the pointers contents
3. In the main function
- Create an array of pointers to persons.
- Input person objects and put them into the array.
- Display the unsorted array of persons
- Call the function bsort to sort pointers (by name and by salary)
- Display the sorted list
Explanation / Answer
Output
Enter name: Likhita
Enter salary: 500000
Enter another (y/n)? y
Enter name: Nitin
Enter salary: 1000000
Enter another (y/n)? y
Enter name: Mukesh
Enter salary: 550000
Enter another (y/n)? n
Unsorted list:
Name Is : Likhita And Salary Is : 500000
Name Is : Nitin And Salary Is : 1000000
Name Is : Mukesh And Salary Is : 550000
Sorted list:
Name Is : Likhita And Salary Is : 500000
Name Is : Mukesh And Salary Is : 550000
Name Is : Nitin And Salary Is : 1000000
Program:
#include <iostream>
#include <string> //for string class
using namespace std;
////////////////////////////////////////////////////////////////
class person //class of persons
{
protected:
string name; //person's name
int salary;
public:
void setPerson() //set the name
{ cout << "Enter name: "; cin >> name;
cout << "Enter salary: "; cin >> salary;
}
void printValues() //display the name
{ cout << endl <<"Name Is : "<< name<< " And Salary Is : "<<salary; }
string getName() //return the name
{ return name; }
int getSalary() //return the name
{ return salary; }
};
////////////////////////////////////////////////////////////////
int main()
{
int j;
void bsort(person**, int); //prototype
person* persPtr[100]; //array of pointers to persons
int n = 0; //number of persons in array
char choice; //input char
do { //put persons in array
persPtr[n] = new person; //make new object
persPtr[n]->setPerson(); //set person's name
n++; //count new person
cout << "Enter another (y/n)? "; //enter another
cin >> choice; // person?
}
while( choice=='y' ); //quit on 'n'
cout << " Unsorted list:";
for(int j=0; j<n; j++) //print unsorted list
{ persPtr[j]->printValues(); }
bsort(persPtr, n); //sort pointers
cout << " Sorted list:";
for(j=0; j<n; j++) //print sorted list
{ persPtr[j]->printValues(); }
cout << endl;
return 0;
} //end main()
//--------------------------------------------------------------
void bsort(person** pp, int n) //sort pointers to persons
{
void order(person**, person**); //prototype
int j, k; //indexes to array
for(j=0; j<n-1; j++) //outer loop
for(k=j+1; k<n; k++) //inner loop starts at outer
order(pp+j, pp+k); //order the pointer contents
}
//--------------------------------------------------------------
void order(person** pp1, person** pp2) //orders two pointers
{ //if 1st larger than 2nd,
if( (*pp1)->getName() > (*pp2)->getName() )
{
person* tempptr = *pp1; //swap the pointers
*pp1 = *pp2;
*pp2 = tempptr;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.