The idea of this program is to create an array of pointers to objects of the per
ID: 3757852 • 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.
Requirements:
Create class called person with the following property and methods:
name
salary
void setPerson() //input name and salary
string getName()
float getSalary()
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 function bsort, call the function order to the pointers contents
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
Program code:
#include<iosstream.h>
#include<string.h>
using namespace std;
class person
{
protected:
string pname;
float psalary;
public:
void setPerson()
{
cout<<"enter name: ";
cin>>pname;
cout<<"enter salary";
cin>>psalary;
}
string getName()
{
retrun pname;
}
float getSalary()
{
return psalary;
}
void bSort(person **pp,int n,bool s)
{
if(s==true)
{
void order(person **,person **);
int j,k;for(j=0;j<n-1;j++)
for(k=0;k<n;k++)
order(pp+j,pp+k);
}else
{
int j,k;for(j=0;j<n-1;j++)
for(k=0;k<n;k++)
order(pp+j,pp+k);
if((*pp1)->getSalary() > (*pp2)->getSalary() )
{
person* temp=*pp1;
*pp1=*pp2;
*pp2=temp;
}
}
void order(person** pp1,person** pp2)
{
if((*pp1)->getName() > (*pp2)->getName() )
{
person* temp=*pp1;
*pp1=*pp2;
*pp2=temp;
}
}
int main()
{
person* persons[10][10];
int n=0;
char ch;
do
{
persons[n]=new person;
persons[n]->setperson;();
n++;
cout<<"enter another(y/n)?";
cin>>ch;
}
while(choice=='y')
cout<<"unsorted names";
for(int j=0;j<n;j++)
{
cout<<persons[j];
}
bsort(persons,n,true);
cout<<"sorted list";
for(int l=o;l<n;l++)
{
cot<<persons[l]<<endl;
}
retrun 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.