C++ alphabetical sorting and sort from highest to lowest. The question involves
ID: 3574991 • Letter: C
Question
C++ alphabetical sorting and sort from highest to lowest. The question involves classes. All of my class files are complete. I just need to modify the code so that the output is sorted alphabetically by last name, and then sorted from highest pay amount to lowest pay amount. Thus the payroll list will be output three times. How do I do this?!
Here's my driver file which I just called PayRollDriver.cpp. doesn't matter what you call it
#include
#include
using namespace std;
#include "Intern.h"
#include "HourlyEmp.h"
#include "CommissionEmp.h"
#include
#include
int main()
{
vector employeeList;
Person *emp = new Intern(52, "Smith");
employeeList.push_back(emp);
emp = new HourlyEmp(77, "Jones", 40.0, 25.0); //Hours worked, and hourly rate
employeeList.push_back(emp);
emp = new CommissionEmp(44, "Hughs", .10, 20000.00);//Commission rate, and total sales
employeeList.push_back(emp);
emp = new HourlyEmp(39, "Wilson", 30.0, 10.0); // Hours worked, and hourly rate
employeeList.push_back(emp);
emp = new HourlyEmp(61, "Carson", 60.0, 30.0); // Hours worked, and hourly rate
employeeList.push_back(emp);
emp = new CommissionEmp(50, "Jennings", .18, 25000.00);//Commission rate, and total sales
employeeList.push_back(emp);
emp = new HourlyEmp(61, "Cruz", 40.5, 31.50); // Hours worked, and hourly rate
employeeList.push_back(emp);
int n = employeeList.size();
for (int i = 0; i < n; i++)
{
cout << "Id: " << employeeList[i]->GetId() << " Last Name: " << employeeList[i]->GetLastName();
cout << " Pay Amount $" << employeeList[i]->ComputeWeeklySalary() << endl;
}
return 0;
}
Heres my Person.cpp file to implement the class:
#include "Person.h"
#include
Person::Person(int id, string lastName, double hoursWorked) :
m_id(id),
m_lastName(lastName),
m_hoursWorked(hoursWorked)
{}
double
Person::ComputeWeeklySalary()//represents interns
{
return 0.0;
}
Here's my Person.h header file for the base class definition:
#ifndef Person_H
#define Person_H
#include
using namespace std;
class Person
{
public:
Person::Person(int id, string lastName, double hoursWorked = 0.0);
int GetId() const { return m_id; }
string GetLastName() const { return m_lastName; }
virtual double getHoursWorked() const { return m_hoursWorked; }
virtual double ComputeWeeklySalary();//virtual function to compute weekly pay
private://common information
int m_id;
string m_lastName;
double m_hoursWorked;
};
#endif Employees_H
Here's my Intern.h file/ Class definition:
#include "Person.h"
#include
#include
class Intern : public Person
{
public:
Intern(int id, string lastName, double hoursWorked=0.0);
virtual double ComputeWeeklySalary();//function to compute weekly pay
};
#endif Intern_H
Here's my Intern.cpp implementation of the Intern class:
#include "Person.h"
#include
#include "Intern.h"
#include
Intern::Intern(int id, string lastName, double hoursWorked) :Person(id, lastName, hoursWorked) {}
double
Intern::ComputeWeeklySalary()//compute weekly salary function for commission employees
{
return 0.0;//interns don't get paid
}
#ifndef HourlyEmp_H
#define HourlyEmp_H
#include "Person.h"
#include
#include
Here's my driver file which I just called PayRollDriver.cpp. doesn't matter what you call it
#include
#include
using namespace std;
#include "Intern.h"
#include "HourlyEmp.h"
#include "CommissionEmp.h"
#include
#include
int main()
{
vector employeeList;
Person *emp = new Intern(52, "Smith");
employeeList.push_back(emp);
emp = new HourlyEmp(77, "Jones", 40.0, 25.0); //Hours worked, and hourly rate
employeeList.push_back(emp);
emp = new CommissionEmp(44, "Hughs", .10, 20000.00);//Commission rate, and total sales
employeeList.push_back(emp);
emp = new HourlyEmp(39, "Wilson", 30.0, 10.0); // Hours worked, and hourly rate
employeeList.push_back(emp);
emp = new HourlyEmp(61, "Carson", 60.0, 30.0); // Hours worked, and hourly rate
employeeList.push_back(emp);
emp = new CommissionEmp(50, "Jennings", .18, 25000.00);//Commission rate, and total sales
employeeList.push_back(emp);
emp = new HourlyEmp(61, "Cruz", 40.5, 31.50); // Hours worked, and hourly rate
employeeList.push_back(emp);
int n = employeeList.size();
for (int i = 0; i < n; i++)
{
cout << "Id: " << employeeList[i]->GetId() << " Last Name: " << employeeList[i]->GetLastName();
cout << " Pay Amount $" << employeeList[i]->ComputeWeeklySalary() << endl;
}
return 0;
}
Heres my Person.cpp file to implement the class:
#include "Person.h"
#include
Person::Person(int id, string lastName, double hoursWorked) :
m_id(id),
m_lastName(lastName),
m_hoursWorked(hoursWorked)
{}
double
Person::ComputeWeeklySalary()//represents interns
{
return 0.0;
}
Here's my Person.h header file for the base class definition:
#ifndef Person_H
#define Person_H
#include
using namespace std;
class Person
{
public:
Person::Person(int id, string lastName, double hoursWorked = 0.0);
int GetId() const { return m_id; }
string GetLastName() const { return m_lastName; }
virtual double getHoursWorked() const { return m_hoursWorked; }
virtual double ComputeWeeklySalary();//virtual function to compute weekly pay
private://common information
int m_id;
string m_lastName;
double m_hoursWorked;
};
#endif Employees_H
Here's my Intern.h file/ Class definition:
#include "Person.h"
#include
#include
class Intern : public Person
{
public:
Intern(int id, string lastName, double hoursWorked=0.0);
virtual double ComputeWeeklySalary();//function to compute weekly pay
};
#endif Intern_H
Here's my Intern.cpp implementation of the Intern class:
#include "Person.h"
#include
#include "Intern.h"
#include
Intern::Intern(int id, string lastName, double hoursWorked) :Person(id, lastName, hoursWorked) {}
double
Intern::ComputeWeeklySalary()//compute weekly salary function for commission employees
{
return 0.0;//interns don't get paid
}
#ifndef HourlyEmp_H
#define HourlyEmp_H
#include "Person.h"
#include
#include
class HourlyEmp : public Person {
public:
HourlyEmp(int id, string lastName, double hoursWorked = 0.0, double hourlyRate = 0.0);
void setHourlyRate(double hourlyRate) { m_hourlyRate = hourlyRate; } //set m_hourlyrate
double getHourlyRate() const { return m_hourlyRate; }//get m_hourly rate
virtual double ComputeWeeklySalary();//function to compute weekly pay
private:
double m_hourlyRate;
};
#endif HourlyEmp_H
Here's my HourlyEmp.cpp class implementation of the HourlyEmp class:
#include "Person.h"
#include
#include "CommissionEmp.h"
#include "HourlyEmp.h"
#include
HourlyEmp::HourlyEmp(int id, string lastName, double hoursWorked, double hourlyRate) :Person(id, lastName, hoursWorked),
m_hourlyRate(hourlyRate)
{
}
double
HourlyEmp::ComputeWeeklySalary()//function for hourly employee pay
{
double hoursWorked = getHoursWorked();
return m_hourlyRate * hoursWorked;
}
Here is my CommissionEmp.h/class definition of commission employees file:
#ifndef CommissionEmp_H
#define CommissionEmp_H
#include "Person.h"
#include
#include
class CommissionEmp : public Person {
public:
CommissionEmp::CommissionEmp(int id, string lastName, double hoursWorked=0.0, double commissionRate = 0.0, double totalSales = 0.0);
void setCommissionRate(double commissionRate) { m_commissionRate = commissionRate; }//set commission rate
double getCommissionRate()const { return m_commissionRate; }//get commission rate
void setTotalSales(double totalSales) { m_totalSales = totalSales; }//set total sales
double getTotalSales()const { return m_totalSales; }//get total sales
virtual double ComputeWeeklySalary();
private:
double m_commissionRate;
double m_totalSales;
};
#endif CommissionEmp_H
Here is my CommissionEmp.cpp / implementation file:
#include "Person.h"
#include
#include "CommissionEmp.h"
#include "HourlyEmp.h"
#include
CommissionEmp::CommissionEmp(int id, string lastName, double hoursWorked, double commissionRate, double totalSales) :Person(id, lastName, hoursWorked),
m_commissionRate(commissionRate), m_totalSales(totalSales) {}
double
CommissionEmp::ComputeWeeklySalary()//compute weekly salary function for commission employees
{
return m_commissionRate * m_totalSales;
}
Here's my HourlyEmp.h file / class definition:
class HourlyEmp : public Person {
public:
HourlyEmp(int id, string lastName, double hoursWorked = 0.0, double hourlyRate = 0.0);
void setHourlyRate(double hourlyRate) { m_hourlyRate = hourlyRate; } //set m_hourlyrate
double getHourlyRate() const { return m_hourlyRate; }//get m_hourly rate
virtual double ComputeWeeklySalary();//function to compute weekly pay
private:
double m_hourlyRate;
};
#endif HourlyEmp_H
Here's my HourlyEmp.cpp class implementation of the HourlyEmp class:
#include "Person.h"
#include
#include "CommissionEmp.h"
#include "HourlyEmp.h"
#include
HourlyEmp::HourlyEmp(int id, string lastName, double hoursWorked, double hourlyRate) :Person(id, lastName, hoursWorked),
m_hourlyRate(hourlyRate)
{
}
double
HourlyEmp::ComputeWeeklySalary()//function for hourly employee pay
{
double hoursWorked = getHoursWorked();
return m_hourlyRate * hoursWorked;
}
Here is my CommissionEmp.h/class definition of commission employees file:
#ifndef CommissionEmp_H
#define CommissionEmp_H
#include "Person.h"
#include
#include
class CommissionEmp : public Person {
public:
CommissionEmp::CommissionEmp(int id, string lastName, double hoursWorked=0.0, double commissionRate = 0.0, double totalSales = 0.0);
void setCommissionRate(double commissionRate) { m_commissionRate = commissionRate; }//set commission rate
double getCommissionRate()const { return m_commissionRate; }//get commission rate
void setTotalSales(double totalSales) { m_totalSales = totalSales; }//set total sales
double getTotalSales()const { return m_totalSales; }//get total sales
virtual double ComputeWeeklySalary();
private:
double m_commissionRate;
double m_totalSales;
};
#endif CommissionEmp_H
Here is my CommissionEmp.cpp / implementation file:
#include "Person.h"
#include
#include "CommissionEmp.h"
#include "HourlyEmp.h"
#include
CommissionEmp::CommissionEmp(int id, string lastName, double hoursWorked, double commissionRate, double totalSales) :Person(id, lastName, hoursWorked),
m_commissionRate(commissionRate), m_totalSales(totalSales) {}
double
CommissionEmp::ComputeWeeklySalary()//compute weekly salary function for commission employees
{
return m_commissionRate * m_totalSales;
}
Explanation / Answer
for(i=0; i<n; i++)
{
cin>>str[i];
}
for(i=1; i<n; i++)
{
for(j=1; j<n; j++)
{
if(strcmp(str[j-1], str[j])>0)
{
strcpy(t, str[j-1]);
strcpy(str[j-1], str[j]);
strcpy(str[j], t);
}
}
}
cout<<"Strings (Names) in alphabetical order : ";
for(i=0; i<n; i++)
{
cout<<str[i]<<" ";
}
int payment arr[n],temp;
for(int i=0;i<n;i++)
{
for(j=0;j<n-1;j++)
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
{
}
}
cout<<"Print payments in ascending Order";
cout<<"........................................";
for(i=0;i<n;i++)
{
cout<<arr[i]<<endl;
}
Add this code to project
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.