In: Computer Science
8.20 Person Pointer Function Make a function that will accept a pointer to a vector of Person pointers as a parameter. Return a pointer to the Person with the highest salary. The function must be signatured like: Person* getBestPaid(vector*); The class definition is: class Person { public: double salary; string name; }; You may include code in your main() to test, but the tests in this assignment will ensure your code is correct. You may assume there will ways be at least 1 element in the vector for the purpose of this exercise.
//highsal.cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// class definition
class Person
{
public:
double salary;
string name;
// constructor for initializing the data
Person(double sal, string n)
{
salary = sal;
name = n;
}
};
// function to find best paid person
Person *getBestPaid(vector<Person *> persons)
{
// intial best persion paid it at 0 index
Person *p = persons[0];
// iterating through persons
for (auto &per : persons)
{
// comparing salary
// if greater then updating person
if (per->salary > p->salary)
p = per;
}
return p;
}
int main()
{
// vector if Person pointer
vector<Person *> persons;
// added few test data
persons.push_back(new Person(2132, "rakesh"));
persons.push_back(new Person(64578, "amit"));
persons.push_back(new Person(6734, "Sona"));
persons.push_back(new Person(878734, "Kranti"));
persons.push_back(new Person(3442, "Somya"));
persons.push_back(new Person(2333, "Rohit"));
// calling the function and printig the result
Person *bestPaidPerson = getBestPaid(persons);
cout << "Best Paid person" << endl
<< "Name: " << bestPaidPerson->name << "\tSalary: " << bestPaidPerson->salary << endl;
}
// OUTPUT

Please do let me know if u have any concern...